Exemple #1
0
// and now a simple client to drive it all
int main()
{
   int choice = 0;   // user selection from menu, initialized to get into loop
   LIST l;           // the linked list itself
   NODE *e;          // for creating and removing nodes from the list
   int key;          // for access by key

   // until user says to quit...
   while( choice != QUIT )
   {
      // print the menu
      cout << "\n0)\tinitialize list\n1)\tInsert front\n2)\tInsert rear\n"
           << "3)\tInsert by key\n4)\tRemove front\n5)\tRemove rear\n"
           << "6)\tRemove by key\n7)\tPrint list\n"
           << "8)\tCount of elements\n9)\tQuit\n\nEnter choice : " << flush;
      cin  >> choice;
      switch( choice )
      {
         case INIT:        l.init();   // initialize an empty list
                           break;

         case INSERTFRONT: e = new NODE;  // insert at front
                           cout << "Enter key and data : " << flush;
                           cin  >> e->key >> e->data;
                           l.insertFront( e );
                           break;

         case INSERTREAR:  e = new NODE;  // insert at rear
                           cout << "Enter key and data : " << flush;
                           cin  >> e->key >> e->data;
                           l.insertEnd( e );
                           break;

         case INSERTSORTED:e = new NODE;  // insert sorted by key
                           cout << "Enter key and data : " << flush;
                           cin  >> e->key >> e->data;
                           l.insertSorted( e );
                           break;

         case REMOVEFRONT: e = l.getFirst(); // remove front element
                           if( e == NULL )   // unless list is empty...
                           {
                              cout << "Not found" << endl;
                           }
                           else
                           {
                              cout << "key " << e->key << ", data " << e->data << endl;
                              delete e;
                           }
                           break;

         case REMOVEREAR:  e = l.getLast();  // remove last element
                           if( e == NULL )
                           {
                              cout << "Not found" << endl;
                           }
                           else
                           {
                              cout << "key " << e->key << ", data " << e->data << endl;
                              delete e;
                           }
                           break;

         case REMOVEBYKEY: cout << "Enter key : " << flush; // remove an element by key
                           cin  >> key;
                           e = l.getNode( key );
                           if( e == NULL )
                           {
                              cout << "Not found" << endl;
                           }
                           else
                           {
                              cout << "key " << e->key << ", data " << e->data << endl;
                              delete e;
                           }
                           break;

         case PRINTALL:    cout << "-------------------------" << endl;  // print the list
                           l.printAll();
                           cout << "-------------------------" << endl;
                           break;

         case HOWMANY:     cout << "LIST has " << l.getCount() << " elements\n"; // count
         case QUIT:        break;

         default:          cout << "I don't understand, sorry...\n" << flush;
      }
   }
   return 0;
}