#include <iostream> using std::cin; using std::endl; #include "list.h" int main() { List< int > L; int inputmax,front,back,tmpF,tmpB; bool ok; cout << "How much number do you want to input? "; cin >> inputmax; for (int i=0 ; i<inputmax; i++) { cout << "please enter "<< i+1 <<"th number =>"; cin >> back; if (i==0) { //第一次直接加入 L.insertAtBack(back); L.print(); } else if (i==1) { //第二次比較大加在前 比較小加在後 L.getFront(front); if (front>back) { //加在前 L.insertAtFront(back); L.print(); } else { //加在後 L.insertAtBack(back); L.print(); } } else { L.getFront(tmpF); //取得最前面的資料 L.getBack(tmpB); //取得最後面的資料 // cout << tmpF << " " << tmpB; if (back<tmpF) { L.insertAtFront(back); L.print(); } else if (tmpB<back) { L.insertAtBack(back); L.print(); } else { L.insert(back); L.print(); } } } return 0; }
http://pastie.org/255668
list.h
#ifndef LIST_H #define LIST_H #include <iostream> using std::cout; #include <new> #include "listnode.h" template< class NODETYPE > class List { public: List(); ~List(); void insertAtFront( const NODETYPE & ); void insertAtBack( const NODETYPE & ); void insert(const NODETYPE &); bool getFront( NODETYPE & ); bool getBack ( NODETYPE & ); bool removeFromFront( NODETYPE & ); bool removeFromBack( NODETYPE & ); bool isEmpty() const; void print() const; private: ListNode< NODETYPE > *firstPtr; ListNode< NODETYPE > *lastPtr; ListNode< NODETYPE > *getNewNode( const NODETYPE & ); }; template< class NODETYPE > List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) {} template< class NODETYPE > List< NODETYPE >::~List() { if ( !isEmpty() ) { cout << "Destroying nodes ...\n"; ListNode< NODETYPE > *currentPtr = firstPtr; ListNode< NODETYPE > *tempPtr; while ( currentPtr != 0 ) { // delete remaining nodes tempPtr = currentPtr; cout << " " << tempPtr->data ; currentPtr = currentPtr->nextPtr; delete tempPtr; } } cout << "\nAll nodes destroyed\n"; } template< class NODETYPE > //insertAtFront void List< NODETYPE >::insertAtFront( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); if ( isEmpty() ) firstPtr = lastPtr = newPtr; else { newPtr->nextPtr = firstPtr; firstPtr = newPtr; } } template< class NODETYPE > //insert void List< NODETYPE >::insert( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); ListNode< NODETYPE > *tempPtr = firstPtr; if ((newPtr->data)<(firstPtr->nextPtr->data)){ newPtr->nextPtr = firstPtr->nextPtr; firstPtr->nextPtr = newPtr; } else { while (!((newPtr->data)<(tempPtr->nextPtr->data))) { tempPtr = tempPtr->nextPtr; } newPtr->nextPtr = tempPtr->nextPtr; tempPtr->nextPtr = newPtr; } } template< class NODETYPE > //insertAtBack void List< NODETYPE >::insertAtBack( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); if ( isEmpty() ) firstPtr = lastPtr = newPtr; else { lastPtr->nextPtr = newPtr; lastPtr = newPtr; } } template< class NODETYPE > //romoveFromFront bool List< NODETYPE >::removeFromFront( NODETYPE &value ) { if ( isEmpty() ) return false; else { ListNode< NODETYPE > *tempPtr = firstPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else firstPtr = firstPtr->nextPtr; value = tempPtr->data; delete tempPtr; return true; } } template< class NODETYPE > //romoveFromBack bool List< NODETYPE >::removeFromBack( NODETYPE &value ) { if ( isEmpty() ) return false; else { ListNode< NODETYPE > *tempPtr = lastPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else { ListNode< NODETYPE > *currentPtr = firstPtr; while ( currentPtr->nextPtr != lastPtr ) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; currentPtr->nextPtr = 0; } value = tempPtr->data; delete tempPtr; return true; } } template< class NODETYPE > //getFront bool List< NODETYPE >::getFront( NODETYPE &value ) { ListNode< NODETYPE > *tempPtr = firstPtr; value = tempPtr->data; return true; } template< class NODETYPE > //getBack bool List< NODETYPE >::getBack( NODETYPE &value ) { ListNode< NODETYPE > *tempPtr = lastPtr; value = tempPtr->data; return true; } template< class NODETYPE > //check isEmpty bool List< NODETYPE >::isEmpty() const { return firstPtr == 0; } template< class NODETYPE > //getNewNode ListNode< NODETYPE > *List< NODETYPE >::getNewNode( const NODETYPE &value ) { return new ListNode< NODETYPE >( value ); } template< class NODETYPE > //print Link void List< NODETYPE >::print() const { if ( isEmpty() ) { cout << "The list is empty\n\n"; return; } ListNode< NODETYPE > *currentPtr = firstPtr; cout << "list: "; while ( currentPtr != 0 ) { cout << currentPtr->data << ' '; currentPtr = currentPtr->nextPtr; } cout << "\n"; } #endif !!listnode.h #ifndef LISTNODE_H #define LISTNODE_H template< class NODETYPE > class List; template< class NODETYPE > class ListNode { friend class List< NODETYPE >; public: ListNode( const NODETYPE & ); NODETYPE getData() const; //getDate private: NODETYPE data; //save data ListNode< NODETYPE > *nextPtr; }; template< class NODETYPE > ListNode< NODETYPE >::ListNode( const NODETYPE &info ) : data( info ), nextPtr( 0 ) {} template< class NODETYPE > NODETYPE ListNode< NODETYPE >::getData() const { return data; } #endif
http://pastie.org/255677
沒有留言:
張貼留言