Lecture 14 Examples


image from book

Open table as spreadsheet

Programs

Demonstrates the use of

alist.h

This file contains the definition of the class aList.

anewlist.h

This file contains the definition of the class aNewList. This class differs from the class aList in that it permits insertion and deletion.

date.h

This header contains the definition of the date class which demonstrates input and output by overloading << and >>

date_queue.cpp

This program simulates a long queue.

dblinked.cpp

Demonstrates the use of a doubly linked list.

dblinked.h

This header file works with the program: dblinked.cpp. It contains the definition of the class List which implements a doubly linked list.

factors.cpp

This program displays the prime factorization of numbers entered by the user. It is based upon the header file stack.h which contains a general definition of a stack.

gcd.cpp

This program demonstrates the concept of recursion.

linked.cpp

Demonstrates the use of a linked list.

linked.h

This files contains the definition of the class: List that implements a linked list.

list1.cpp

Demonstrates the use of dynamic memory allocation to create a list. In this example the list contained longs but this could have been any data type.

list2.cpp

Demonstrates the use of dynamic memory allocation to create a list. In this example the list contained longs but this could have been any data type. This program differs from list1.cpp in that it permits the insertion and the deletion of elements.

long_queue.cpp

This file contains the methods for the class Queue that is contained in the file: long_queue.h.

long_queue.h

The file contains the definition of the class: Queue whose methods are defined in the file long_gueue.cpp.

new_queue.h.

This header creates a queue of any data type.

new_stack.cpp

This program simulates a generalized stack

queue1.cpp

This program simulates a queue based on a linked list.

queue1.h

This header contains the definition of a queue based upon a linked list.

queue2.h

This header contains the definition of a queue that adds the feature of a pointer to the back of the queue so that the queue is more efficient when adding nodes to the queue.

queue3.h

This header contains the definition of a circular queue.

stack1.h

Contains the definition of a stack using a linked list.

stack1.cpp

Demonstrates how a stack can be implemented using a linked list.

stack.cpp

This program contains the definition of a class that simulates a generalized stack.

stacks.h

This header file contains a class definition of a generalized stack class based upon an array. The stack type and the size of the stack must be provided by the calling program.

templateQueue.h

This header creates a template queue for any data type.

templateStack.h

 

templateQueueList.cpp

This program simulates a queue based on a linked list and templates.

templateQueueList.h

This file contains the definition of a template queue based upon a list.

templateStackList.cpp

Demonstrates how a template stack can be implemented using a linked list.

templateStackList.h

This header file contains the definition of a template stack that is based upon a linked list.

useTemplateQueue.cpp

This program simulates a long queue using templates.

useTemplateStack.cpp

This file contains the definition of the template stack.

useQueue.cpp

This program simulates a long queue.

image from book

 // program_id    aList.h // written_by    Don Voils // date_written  9/12/2006 // Description   This file contains the definition of //                the class aList. // #ifndef ALIST #define ALIST class aList {    private:       datatype *ptr;       long top;       long max;    public:      aList(long maximum)      {        max = maximum;        ptr = new datatype[max];        top = -1;      }      ~aList()      {        delete [] ptr;      }      void add_to(datatype value)      {        if(!is_full())        {           ++top;           *(ptr+top) = value;        }        else        {           max += 10;           datatype* new_ptr = new datatype[max];           for(long index=0;index<=top;++index)             *(new_ptr+index)= *(ptr+index);           ++top;           *(new_ptr+top) = value;           delete [] ptr;           ptr = new_ptr;        }     }     datatype show_element(long position)     {        datatype value;        if((position-1) > top)          throw position;        else          value = *(ptr + (position-1));        return value;     }     void show_all()     {       cout << "The elements in the list are the following:"                  << endl;       for(short index=0;index<=top;++index)          cout << *(ptr+index) << endl;     }     bool is_empty()     {       return(top==-1);     }     bool is_full()     {       return(top==(max-1));     } }; #endif // program_id   aNewList.h // written_by   Don Voils // date_written 9/12/2006 // // Description  This file contains the definition of the //              class aNewList. This class differs from //              the class aList in that it permits //              insertion and deletion. // #ifndef ANEWLIST #define ANEWLIST class aNewList {    private:       datatype *ptr;       long top;       long max;    public:      aNewList(long maximum)      {        max = maximum;        ptr = new datatype[max];        top = -1;      }      ~aNewList()      {        delete [] ptr;      }      void add_to(datatype value)      {        if(!is_full())        {           ++top;           *(ptr+top) = value;        }        else        {          max += 10;          datatype* new_ptr = new datatype[max];          for(long index=0;index<=top;++index)            *(new_ptr+index)= *(ptr+index);          ++top;          *(new_ptr+top) = value;          delete [] ptr;          ptr = new_ptr;        }      }      datatype show_element(long position)      {        datatype value;        if((position-1) > top)          throw position;        else          value = *(ptr + (position-1));        return value;      }      void show_all()      {        cout << "The elements in the list are the following:"                  << endl;        for(short index=0;index<=top;++index)           cout << *(ptr+index) << endl;      }      void insert_it(long numb,datatype value)      {       --numb;             if(top < max)       {               for(short index=short(top); index > numb;--index)                 *(ptr + (index+1)) = *(ptr + index);               ++top;               *(ptr + (numb+1)) = value;       }       else                cout << endl << endl << "Error list is full"                     << endl << endl;      }      void delete_it(long numb)      {        --numb;              for(short index=short(numb);index<short(top);++index)                            *(ptr + index) = *(ptr + (index+1));              --top;      }            bool is_empty()      {              return(top==-1);      }            bool is_full()      {              return(top==(max-1));      } }; #endif // program_id        date.h // author            don voils // date written     9/5/2006 // // description   This header contains the definition //               of the date class which demonstrates //               input and output by overloading << and >> // #ifndef DATE    #define DATE class Date { private:   int theMonth,      theDay,      theYear;  public:   int getMonth()   {       return theMonth;   }   int getDay()   {       return theDay;   }   int getYear()   {       return theYear;   }   friend ostream &operator << (ostream &stream, Date ab);   friend istream &operator >> (istream &stream, Date &ab); };   ostream &operator << (ostream &stream, Date ab)   {     stream << ab.theMonth << "/";     stream << ab.theDay << "/";     stream << ab.theYear << endl;     return stream;   }   istream &operator >> (istream &stream, Date &ab)   {     char slash;     stream >> ab.theMonth >> slash >> ab.theDay >> slash >> ab.theYear;     return stream;   } #endif // program_id       date_queue.cpp // author           don voils // date written    9/17/2000 // // description  This program simulates a long queue. // #include<iostream> using namespace std; void clear_screen(); // // The date class is included to demonstrate the generality of // the class: Queue contained in the header: new_queue.h that is // included below. // #include"date.h" // // The variable capacity is only 10 so that the queue is easy to test. // const long capacity = 10; // // The variable QueueType is now a general data type in the // header new_queue.h. The typedef is done out here so that // that its data type could be changed relatively easy // typedef Date QueueType; #include"new_queue.h" // // Below is the test program to determine whether the queue class // works or not. // void main() {      short which;      char resp;      Date item;      Queue a_queue;      do      {          clear_screen();          cout << "Select an option:" << endl << endl             << "1. Add to the queue" << endl << endl             << "2. Is the queue empty?" << endl << endl             << "3. Is the queue full?" << endl << endl             << "4. What is the front element in the queue?" << endl << endl             << "5. Remove the front element of the queue" << endl << endl             << "6. Display all of the elements of the queue" << endl << endl             << "7. Exit the program." << endl << endl             << endl <<endl << "Which option? ";          cin >> which;          switch(which)          {               case 1:                    clear_screen();                    if(!a_queue.isFullQ())                    {                       cout << "Enter a date to add to the queue (mm/dd/yyyy): ";                                                   cin >> item;                                             a_queue.addQ(item);                                                   cout << endl                                                        << "The queue is not full so an element was added."                                                        << endl;                                                   cout << endl << endl << "Continue? (Y/N)";                       cin >> resp;                    }                    else                    {                                            cout << "The queue is full and so no element was added."                                                        << endl;                                                  cout << endl << endl << "Continue? (Y/N)";                       cin >> resp;                    }                    break;               case 2:                    clear_screen();                                              if(a_queue.isEmptyQ())                                                 cout << "The queue is empty.";                    else                                                 cout << "The queue is not empty.";                    cout << endl << endl << "Continue? (Y/N)";                    cin >> resp;                    break;               case 3:                    clear_screen();                    if(a_queue.isFullQ())                                                cout << "The queue is full.";                    else                                                cout << "The queue is not full.";                    cout << endl << endl << "Continue? (Y/N)";                    cin >> resp;                    break;               case 4:                    clear_screen();                    if(!a_queue.isEmptyQ())                       cout << "The Front element in the queue is: "                                                       << a_queue.frontQ();                    else                                    cout << "The queue is empty.";                    cout << endl << endl << "Continue? (Y/N)";                    cin >> resp;                    break;               case 5:                    clear_screen();                    if(!a_queue.isEmptyQ())                    {                       a_queue.removeQ();                       cout << "The front element has been removed. ";                    }                    else                       cout << "There were no elements to remove";                    cout << endl << endl << "Continue? (Y/N)";                    cin >> resp;                    break;               case 6:                    clear_screen();                    a_queue.displayQ();                    cout << endl << "Continue? (Y/N)";                    cin >> resp;                    break;               case 7:                    break;               default:                    break;          }      }while(which != 7);      clear_screen(); } inline void clear_screen() {      for(short index=1;index<=250;++index)                       cout << endl; } // program_id           dblinked.cpp // written_by           don voils // date_written        10/24/2006 // // description     Demonstrates the use of a //                 doubly linked list. // // #include<iostream> #include<iomanip> using namespace std; typedef long datatype; #include"dblinked.h" void clear_screen(); void main() {   short which;   char resp;   datatype item,   value;   List list1;   do   {     clear_screen();     cout << "Select an option:" << endl << endl          << "1. Insert at the front" << endl << endl          << "2. Insert at the end" << endl << endl          << "3. Insert in the middle" << endl << endl          << "4. Is the list empty?" << endl << endl          << "5. Delete at the front" << endl << endl          << "6. Delete at the end" << endl << endl          << "7. Delete in the middle" << endl << endl          << "8. Display all of the elements of the list" << endl << endl          << "9. Exit the program." << endl << endl          << endl <<endl << "Which option? ";     cin >> which;     switch(which)     {       case 1:         clear_screen();         cout << "Enter the value to add to the front of the list: ";         cin >> item;         list1.insert_front(item);         break;       case 2:         clear_screen();         cout << "Enter the value to add to the end of the list: ";         cin >> item;         list1.insert_end(item);         break;       case 3:         clear_screen();         cout << "Enter the value in the list to be inserted after: ";         cin >> value;         cout << endl << endl              << "Enter the value to add to the list after "              << value << ": ";         cin >> item;         list1.insert_middle(item,value);         break;       case 4:         clear_screen();         if(list1.empty())            cout << "The list is empty.";         else            cout << "The list is not empty.";         cout << endl << endl << "Continue? (Y/N)";         cin >> resp;         break;       case 5:         clear_screen();         list1.delete_front();         break;       case 6:         clear_screen();         list1.delete_end();         break;       case 7:         clear_screen();         cout << "Enter the value in the list to be deleted: ";         cin >> value;         list1.delete_middle(value);         break;       case 8:         clear_screen();         list1.display();         cout << endl << "Continue? (Y/N)";         cin >> resp;         break;       case 9:         break;       default:         break;     }   }while(which != 9);   clear_screen(); } void clear_screen() {      for(short index=1;index<=250;++index)                 cout << endl; } // program_id    dblinked.h // written_by    Don Voils // date_written 10/24/2006 // // description  This header file works with the program: //              dblinked.cpp. It contains the definition //              of the class List which implements a doubly //              linked list. // #ifndef DBLINKED #define DBLINKED class node {    public:      datatype data;      node * next;      node * previous;      node(datatype value,node* ptr1, node * ptr2)      {        data = value;        next = ptr1;        previous = ptr2;      } }; class List {   private:     node* first;   public:     List()     {       first = NULL;     }     ~List()     {       node * ptr;       while(first!=NULL)       {           ptr = first;           first = first->next;           delete ptr;       }     }     bool empty()     {       return (first==NULL);     }     void insert_front(datatype new_value)     {       node * newptr = new node(new_value,first,NULL);       if(!empty())         first->previous = newptr;       first = newptr;     }     void insert_end(datatype new_value)     {       node * newptr = new node(new_value,NULL,NULL);       if(!empty())       {          node * preptr = first;                        while((preptr->next)!=NULL)                            preptr = preptr->next;                        preptr->next = newptr;          newptr->previous = preptr;       }       else          first = newptr;      }      void insert_middle(datatype new_value,datatype value)      {        if(!empty())        {           node * preptr = first;           while(((preptr->next)!=NULL) && ((preptr->data)!=value))                  preptr = preptr->next;           if((preptr->data)==value)           {              node * newptr = new node(new_value,preptr->next,preptr);              if(preptr->next!=NULL)                (preptr->next)->previous = newptr;              preptr->next = newptr;           }        }      }      void delete_front()      {        if(!empty())        {                         node *ptr = first;           if((ptr->next)!=NULL)              (ptr->next)->previous=NULL;           first = ptr->next;                          delete ptr;        }       }       void delete_end()       {          if(!empty())          {          if((first->next)==NULL)                  {                     delete first;                     first = NULL;                  }                  else                  {                      node *preptr = first;                      while((preptr->next)!=NULL)                              preptr = preptr->next;                      (preptr->previous)->next = NULL;                      delete preptr;                  }          }        }        void delete_middle(datatype value)        {      if(!empty())      {        if((first->next)==NULL)        {           if((first->data)==value)           {              delete first;              first = NULL;           }        }        else        {                         node *preptr = first;                         while((preptr->next)!=NULL                  && (preptr->data)!=value)                         preptr = preptr->next;                         if((preptr->data)==value)                         {                            if(preptr == first)                                 first = preptr->next;                            if((preptr->previous)!=NULL)                                 (preptr->previous)->next = preptr->next;                            if(preptr->next!=NULL)                                 (preptr->next)->previous = preptr->previous;                            delete preptr;                         }        }                }       }       void display()       {      if(!empty())      {         cout << "The elements in the list are:" << endl;               cout << setw(12) <<"Previous" << setw(12) << "Node"                    << setw(12) <<"Next" << endl;               node * ptr = first;               while(ptr != NULL)               {                 if(ptr->previous != NULL)                    cout << setw(12) << ptr->previous->data;                 else                   cout << setw(12) << "NULL";                   cout << setw(12) << ptr->data;                   if(ptr->next != NULL)          cout << setw(12) << ptr->next->data << endl;                  else          cout << setw(12) << "NULL" << endl;                  ptr = ptr->next;                }             }       else                cout << "The list is empty." << endl << endl;     } }; #endif // program_id    factors.cpp // written_by    don voils // date_written  3/2/2006 // // description   This program displays the prime factorization //               of numbers entered by the user. It is based //               upon the header file stack.h which contains //               a general definition of a stack. // #include<iostream> using namespace std; const long capacity = 128; typedef long StackElementType; #include"stacks.h" long factor(long a); void clear_screen(); void main() {      Stack s;      long numb;      char resp;      do      {           clear_screen();           cout << "What is the number you want to factor? ";           cin >> numb;           clear_screen();           long n = numb;           try           {                do                {                     s.Push(factor(n));                     long divisor = s.Top();                     n = n / divisor;                }while(n>1);                cout << "The following is the prime factorization of "                                          << numb <<endl                     << "   ";                while(!s.isEmpty())                {                     cout << s.Top();                     s.Pop();                     if(!s.isEmpty())                           cout << " * ";                 }           }           catch(int)           {                clear_screen();                cerr << "Number is either prime or too large for functon.";                       while(!s.isEmpty())                {                     s.Pop();                }                         }           cout << endl << endl << "Another factorization? (Y/N) ";           cin >> resp;           clear_screen();      }while(resp=='y' || resp=='Y'); } long factor(long n) {      long primes[15] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};      long dividend;      short index;      for(index = 0;index < 15; ++index)      {            dividend = n / primes[index];            if(dividend * primes[index] == n)                 break;      }      if(index >= 15)      {           throw 1;      }      return primes[index]; } void clear_screen() {      for(short index = 1;index <= 250;++index)           cout << endl; } // program_id        GCD.CPP // author            don voils // date written     9/24/2006 // // description: This program demonstrates the concept //              of recursion. // #include<iostream> using namespace std; int gcd(int numb1, int numb2); void main(void) {    int int1,       int2;    cout << "What is the first number? ";    cin >> int1;    cout << endl << endl << "What is the second number? ";    cin >> int2;    cout << endl << endl << "The GCD of " << int1 << " and "       << int2 << " is " << gcd(int1,int2); } int gcd(int numb1, int numb2) {     return (numb1<0) ? gcd(-numb1,numb2):((numb2==0)?               numb1:gcd(numb2,numb1%numb2)); } // program_id           linked.cpp // written_by           don voils // date_written        10/22/2006 // // description         Demonstrates the use of a linked list. // // #include<iostream> using namespace std; typedef long datatype; #include"linked.h" void clear_screen(); void main() {   short which;   char resp;   datatype item,            value;   List list1;   do   {     clear_screen();     cout << "Select an option:" << endl << endl          << "1. Insert at the front" << endl << endl          << "2. Insert at the end" << endl << endl          << "3. Insert in the middle" << endl << endl          << "4. Is the list empty?" << endl << endl          << "5. Delete at the front" << endl << endl          << "6. Delete at the end" << endl << endl          << "7. Delete in the middle" << endl << endl          << "8. Display all of the elements of the list" << endl << endl          << "9. Exit the program." << endl << endl          << endl <<endl << "Which option? ";     cin >> which;     switch(which)     {       case 1:         clear_screen();         cout << "Enter the value to add to the front of the list: ";         cin >> item;         list1.insert_front(item);         cout << endl << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 2:         clear_screen();         if(list1.empty())            cout << "The list is empty so you must first add to the beginning."                 << endl;         else         {            cout << "Enter the value to add to the end of the list: ";            cin >> item;            list1.insert_end(item);         }         cout << endl << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 3:         clear_screen();         if(list1.empty())            cout << "The list is empty so you must first add to the beginning."                 << endl;         else         {            cout << "Enter the value in the list to be inserted after: ";            cin >> value;            cout << endl << endl                 << "Enter the value to add to the list after "                 << value << ": ";            cin >> item;            list1.insert_middle(item,value);         }         cout << endl << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 4:         clear_screen();         if(list1.empty())           cout << "The list is empty.";         else           cout << "The list is not empty.";         cout << endl << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 5:         clear_screen();         if(list1.empty())           cout << "The list is empty. So there are no elements to delete."                << endl << endl;         else         {            list1.delete_front();            cout << endl << "The element at the front has been deleted."                 << endl;         }         cout << endl << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 6:         clear_screen();         if(list1.empty())           cout << "The list is empty. So there are no elements to delete."                << endl << endl;         else         {            list1.delete_end();            cout << endl << "The element at the end has been deleted."                 << endl;         }         cout << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 7:         clear_screen();         if(list1.empty())           cout << "The list is empty. So there are no elements to delete."                << endl << endl;         else         {            cout << "Enter the value to be deleted after: ";            cin >> value;            list1.delete_middle(value);            cout << endl << "The element after " << value                 << " has been deleted." << endl;         }                  cout << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 8:         clear_screen();         if(list1.empty())           cout << "The list is empty. So there are no elements to show."                << endl << endl;         else           list1.display();         cout << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 9:         break;       default:         break;     }   }while(which != 9);   clear_screen(); } void clear_screen() {   for(short index=1;index<=250;++index)           cout << endl; } // program_id     linked.h // written_by     don voils // date_written  10/7/2006 // // description  This file contains the definition //              of the class List that implements //              a linked list. // #ifndef LINKED #define LINKED class node {    public:      datatype data;      node * next;      node(datatype value,node* ptr)      {        data = value;        next = ptr;      } }; class List {    private:      node* first;    public:      List()      {        first = NULL;      }      ~List()      {        node * ptr;        while(first!=NULL)        {         ptr = first;         first = first->next;         delete ptr;        }      }      bool empty()      {        return (first==NULL);      }      void insert_front(datatype new_value)      {        node * newptr = new node(new_value,first);        first = newptr;      }      void insert_end(datatype new_value)      {        node * newptr = new node(new_value,NULL);        if(first!=NULL)        {                   node * preptr = first;                   while((preptr->next)!=NULL)                   preptr = preptr->next;                   preptr->next = newptr;        }        else                   first = newptr;        }        void insert_middle(datatype new_value,datatype value)        {           if(!empty())           {              node * preptr = first;              while(((preptr->next)!=NULL) && ((preptr->data)!=value))              preptr = preptr->next;              if((preptr->data)==value)              {              node * newptr = new node(new_value,preptr->next);              preptr->next = newptr;              }           }        }        void delete_front()        {          if(!empty())          {             node *ptr = first;             first = first->next;             delete ptr;          }        }        void delete_end()        {          if(!empty())          {             if((first->next)==NULL)             {                delete first;                first = NULL;             }             else             {                node *ptr;                node *preptr = first;                while(((preptr->next)->next)!=NULL)                  preptr = preptr->next;                ptr = preptr->next;                preptr -> next = NULL;                delete ptr;             }          }        }        void delete_middle(datatype value)        {          if(!empty())          {             if((first->next)!=NULL)             {                node *ptr;                node *preptr = first;                while((preptr->next)!=NULL             && (preptr->data)!=value)                preptr = preptr->next;                ptr = preptr->next;                if(ptr!=NULL && ((preptr->data)==value))                {                   preptr->next = ptr->next;                   delete ptr;                }             }          }        }        void display()        {          cout << "The elements in the list are:" << endl;          node * ptr = first;          while(ptr != NULL)          {            cout << ptr->data << endl;            ptr = ptr->next;          }        } }; #endif // program_id     list1.cpp // written_by     don voils // date_written   10/17/2006 // // descripton: Demonstrates the use of dynamic //             memory allocation to create a list. //             In this example the list contained //             longs but this could have been any //             data type. // #include<iostream> using namespace std; typedef long datatype; void clear_screen(); #include"aList.h" void main() {   short which;   char resp;   long item;   aList list1(5);   do   {     clear_screen();     cout << "Select an option:" << endl << endl          << "1. Add to the list" << endl << endl          << "2. Is the list empty?" << endl << endl          << "3. Is the list full?" << endl << endl          << "4. Show a particular element?" << endl << endl          << "5. Display all of the elements of the list" << endl << endl          << "6. Exit the program." << endl << endl          << endl <<endl << "Which option? ";     cin >> which;     switch(which)     {       case 1:         clear_screen();         cout << "Enter a long to add to the list: ";                      cin >> item;                      list1.add_to(item);                      cout << endl << "The element was added." << endl;         cout << endl << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 2:         clear_screen();                if(list1.is_empty())                            cout << "The list is empty.";         else            cout << "The list is not empty.";         cout << endl << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 3:         clear_screen();         if(list1.is_full())                          cout << "The list is full.";         else                          cout << "The list is not full.";         cout << endl << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 4:         clear_screen();         if(list1.is_empty())                          cout << "The list is empty.";         else         {            cout << "Enter the number of the position to show: ";            cin >> item;            clear_screen();                          try                          {               if(item <= 0)                  throw item;               else               {                  try                  {                     long element = list1.show_element(item);                     cout << "The element in position " << item                                              << " is " << element << endl << endl;                                    }                                    catch(long position)                                    {                                       cout << position                          << " is beyond the end of the list. "                          << endl;                                      }                                    }            }            catch(long item)            {               cout << "The element position " << item                                << " is not within the list. " << endl;            }         }         cout << endl << endl << "Continue? (Y/N) ";         cin >> resp;         break;       case 5:         clear_screen();                       if(list1.is_empty())            cout << "The list is empty.";         else           list1.show_all();           cout << endl << endl << "Continue? (Y/N) ";           cin >> resp;           break;         case 6:           break;         default:           break;     }   }while(which != 6);   clear_screen(); } void clear_screen() {      for(short index=1;index<=250;++index)                 cout << endl; } // program_id     list2.cpp // written_by     don voils // date_written   10/17/2006 // // descripton: Demonstrates the use of dynamic //             memory allocation to create a list. //             In this example the list contained //             longs but this could have been any //             data type. This program differs from //             list1.cpp in that it permits the //             insertion and the deletion of elements. // #include<iostream> using namespace std; typedef long datatype; void clear_screen(); #include"aNewList.h" void main() {   short which;   char resp;   datatype item;   long position;   aNewList list1(5);   do   {       clear_screen();       cout << "Select an option:" << endl << endl            << "1. Add to the list" << endl << endl            << "2. Insert an element in the list" << endl << endl            << "3. Delete an element in the list" << endl << endl            << "4. Is the list empty?" << endl << endl            << "5. Is the list full?" << endl << endl            << "6. Show a particular element?" << endl << endl            << "7. Display all of the elements of the list" << endl << endl            << "8. Exit the program." << endl << endl            << endl <<endl << "Which option? ";        cin >> which;        switch(which)        {        case 1:           clear_screen();                         cout << "Enter a long to add to the list: ";                         cin >> item;                         list1.add_to(item);                         cout << endl << "The element was added." << endl;           cout << endl << endl << "Continue? (Y/N) ";           cin >> resp;           break;       case 2:           clear_screen();           if(list1.is_empty())           {                             cout << "The list is empty. You need to add items"                                  << " before using this option. "                                  << endl;           }           else           {              if(list1.is_full())                 cout << "The list is full." << endl;              else              {                cout << "What item do you want to insert in "                                 << "the list? ";                cin >> item;                            cout << endl << endl                                 << "After what position do you want to "                                 << " insert the item? ";                cin >> position;                              if(position < 1)                                cout << endl                                     << "The postion must be greater than 0."                                     << endl;                              else                              {                                 try                                 {                                    long elementFound = list1.show_element(position);                                    list1.insert_it(position,item);                                    cout << endl << "The element was inserted."                                         << endl;                                 }                                 catch(long position)                                 {                                    cout << position                                         << " is not a position in the list."                                         << endl;                                 }                              }         }          }          cout << endl << endl << "Continue? (Y/N) ";          cin >> resp;          break;       case 3:                 clear_screen();                 if(list1.is_empty())                   cout << "The list is empty. There must be elements"                        << endl                        << "in the list before you can delete any." << endl;                 else                 {                   cout << "Enter the position of the element to "                        << "delete ";                   cin >> position;                   if(position < 1)                      cout << "The position must be greater than 0. "                           << endl;                   else                   {                      try                      {                        long elementFound = list1.show_element(position);                        list1.delete_it(position);                        cout << endl << "The element was deleted."                             << endl;                      }                      catch(long position)                      {                        cout << position                             << " is not a position in the list."                             << endl;                      }                   }                 }                 cout << endl << endl << "Continue? (Y/N) ";                cin >> resp;                break;             case 4:               clear_screen();               if(list1.is_empty())                 cout << "The list is empty." << endl;               else                 cout << "The list is not empty." << endl;              cout << endl << endl << "Continue? (Y/N) ";              cin >> resp;              break;          case 5:             clear_screen();             if(list1.is_full())                cout << "The list is full." << endl;             else                cout << "The list is not full." << endl;              cout << endl << endl << "Continue? (Y/N) ";              cin >> resp;             break;          case 6:             clear_screen();             if(list1.is_empty())               cout << "The list is empty." << endl                    << "You need to have elements in the list "                    << "before this option."                    << endl;             else             {                long positionToShow;                cout << "Enter the number of the position to show: ";                cin >> positionToShow;                if(positionToShow < 1)                {                   cout << "You must use a position greater than 0."                        << endl;                }                else                {                   clear_screen();                   try                   {                      long elementFound =                              list1.show_element(positionToShow);                      cout << "The element in position "                           << positionToShow                           << " is " << elementFound << endl                           << endl;                   }                   catch(long position)                   {                      cout << position                           << " is beyond the end of the list."                           << endl;                   }                }             }             cout << endl << "Continue? (Y/N) ";             cin >> resp;             break;           case 7:             clear_screen();             if(list1.is_empty())                cout << "The list is empty." << endl;               else               {                  list1.show_all();               }               cout << endl << "Continue? (Y/N) ";               cin >> resp;               break;             case 8:               break;             default:               break;       }    }while(which != 8);    clear_screen(); } void clear_screen() {   for(short index=1;index<=250;++index)      cout << endl; } // program_id   long_queue.cpp // written_by   Don Voils // date_written 9/17/2006 // // Description  This file contains the methods for the class Queue //              that is contained in the file: long_queue.h. // #include<iostream> using namespace std; #include "long_queue.h" Queue::Queue() {   myFront = myBack = 0; } bool Queue::isEmptyQ() {   return (myFront == myBack); } bool Queue::isFullQ() {   return (myFront == ((myBack + 1) % capacity)); } void Queue::addQ(const long& value) {   if (!isFullQ())   {       theArray[myBack] = value;       myBack = (myBack + 1) % capacity;   } } long Queue::frontQ() {   return theArray[myFront]; } void Queue::removeQ() {      if(!isEmptyQ())                myFront = (myFront + 1) % capacity;      else               cout << "The queue was empty so no elements were removed."                    << endl; } void Queue::displayQ() {      if(!isEmptyQ())      {         cout << "The following are the elements in the queue:" << endl;         long index;                for(index = myFront; index != myBack; index = ((index +1) % capacity))                   cout << theArray[index] << endl;      }      else                cout << "The queue has no elements." << endl; } // program_id    long_queue.h // written_by    don voils // date_written  9/17/2006 // // description   The class defined in this header simulates //               a long gueue. // #ifndef QUEUE    #define QUEUE const long capacity = 10; class Queue {   /***** Data Members *****/    private:      long theArray[capacity];      long myFront;      long myBack;   /***** Function Members *****/    public:      Queue();      bool isEmptyQ();      bool isFullQ();      void addQ(const long& value);      long frontQ ();      void removeQ();      void displayQ(); }; // end of class declaration #endif // program_id     long_stack.cpp // author         don voils // date written  9/3/2006 // //  program description  This program simulates a long stack. // #include<iostream> using namespace std; void clear_screen(); // // The variable capacity is only 10 so that the stack is easy to test. // const long capacity = 10; class stack {    private:      long myTop;      long theStack[capacity]; // // The member function declarations are the prototypes // because the definitions should be separate and in // another file. //   public:      stack();      bool isEmpty();      bool isFull();      void Push(const long &item);      long Top();      void Pop();      void Display(); }; // // Member function definitions. // // Notice that the simple member functions are declared as inline. //    inline stack::stack()    {       myTop=-1;    }    bool stack::isEmpty()    {      bool isEmpty = true;      if(myTop > -1)          isEmpty = false;      return isEmpty;    }    bool stack::isFull()    {      bool isFull = true;      if(myTop < capacity - 1)          isFull = false;      return isFull;    }    inline long stack::Top()    {       return theStack[myTop];    } // // Notice that before an element is added, it is determined // whether the stack is full. If it is, an error messages // is sent. // // Notice that since item is not changed it is modified by const // also notice the reference operator. Why was that used? //    void stack::Push(const long &item)    {      if(isFull())            cerr << "The stack is full.";      else      {            ++myTop;            theStack[myTop] = item;      }    } // // Notice that before an element is removed, it is determined // whether the stack is empty. If it is, an error messages // is sent. //    void stack::Pop()    {      if(isEmpty())            cerr << "The stack is empty.";      else            --myTop;    }    void stack::Display()    {      if(isEmpty())            cout << "The stack is empty. " << endl;      else      {            cout<< "From the top down the stack elements are:"                             << endl << endl;            for(long index=myTop; index >=0; --index)                 cout << theStack[short(index)] << endl;      }    } // // Below is the test program to determine whether the stack class // works or not. // void main() {      short which;      char resp;      long item;      stack theStack;      do      {           clear_screen();           cout << "Select an option:" << endl << endl                << "1. Add to the stack" << endl << endl                << "2. Is the stack empty?" << endl << endl                << "3. Is the stack full?" << endl << endl                << "4. What is the top element in the stack?" << endl << endl                << "5. Pop the top element of the stack" << endl << endl                << "6. Display all of the elements of the stack" << endl << endl                << "7. Exit the program." << endl << endl                << endl <<endl << "Which option? ";           cin >> which;           switch(which)           {                case 1:                     clear_screen();                     if(theStack.isFull())                        cout << "The stack is full." << endl;                     else                     {                        cout << "Enter a long to add to the stack: ";                        cin >> item;                        theStack.Push(item);                     }                     break;                case 2:                     clear_screen();                     if(theStack.isEmpty())                           cout << "The stack is empty.";                     else                           cout << "The stack is not empty.";                     cout << endl << endl << "Continue? (Y/N) ";                     cin >> resp;                     break;                case 3:                     clear_screen();                     if(theStack.isFull())                           cout << "The stack is full.";                     else                           cout << "The stack is not full.";                     cout << endl << endl << "Continue? (Y/N) ";                     cin >> resp;                     break;                case 4:                     clear_screen();                     if(theStack.isEmpty())                        cout << "The stack is empty.";                     else                        cout << "The top element in the stack is: "                           << theStack.Top();                     cout << endl << endl << "Continue? (Y/N) ";                     cin >> resp;                     break;                case 5:                     clear_screen();                     if(theStack.isEmpty())                        cout << "The stack is empty.";                     else                     {                        theStack.Pop();                        cout << "The top element has been removed. ";                     }                     cout << endl << endl << "Continue? (Y/N) ";                     cin >> resp;                     break;                case 6:                     clear_screen();                     theStack.Display();                     cout << endl << "Continue? (Y/N) ";                     cin >> resp;                     break;                case 7:                     break;                default:                     break;          }      }while(which != 7);      clear_screen(); } inline void clear_screen() {      for(short index=1;index<=250;++index)                 cout << endl; } // program_id    new_queue.h // author:       Don Voils // date_written: 9/17/2006 // // descripton: This header creates a queue of any data type. // #ifndef NEW_QUEUE    #define NEW_QUEUE class Queue {   /***** Data Members *****/    private:       QueueType my_Array[capacity];       long myFront,            myBack;   /***** Function Members *****/    public:      Queue();      bool isEmptyQ() const;      bool isFullQ() const;      void addQ(const QueueType& value);      QueueType frontQ () const;      void removeQ();      void displayQ() const; }; // end of class declaration Queue::Queue() {   myFront = myBack = 0; } bool Queue::isEmptyQ() const {   return (myFront == myBack); } bool Queue::isFullQ() const {   return (myFront == ((myBack + 1) % capacity)); } void Queue::addQ(const QueueType& value) {   if (!isFullQ())   {      my_Array[myBack] = value;      myBack = (myBack + 1) % capacity;   } } QueueType Queue::frontQ() const {   return my_Array[myFront]; } void Queue::removeQ() {      if(!isEmptyQ())                      myFront = (myFront + 1) % capacity;      else            cout << "The queue was empty so no elements were removed."                 << endl; } void Queue::displayQ() const {      if(!isEmptyQ())      {         cout << "The following are the elements in the queue:" << endl;         long index;              for(index = myFront; index != myBack; index = ((index +1) % capacity))                        cout << my_Array[index] << endl;     }     else          cout << "The queue has no elements." << endl; } #endif // program_id       new_stack.cpp // author          don voils // date written    9/3/2006 // // description  This program simulates a generalized stack. // #include<iostream> using namespace std; #include"date.h" void clear_screen(); // // The variable theCapacity is only 10 so that the stack is easy to test. // const long theCapacity = 10; typedef Date stackElementType; class Stack {      private:           long myTop;           stackElementType theArray[theCapacity]; // // The member function declarations are the prototypes // because the definitions should be separate and in // another file. //      public:           Stack();           bool isEmpty();           bool isFull();           void Push(const stackElementType &item);           stackElementType Top();           void Pop();           void display(); }; // // Member function definitions. // // Notice that the simple member functions are declared as inline. //    inline Stack::Stack()    {         myTop=-1;    }    bool Stack::isEmpty()    {         bool isEmpty = true;         if(myTop > -1)            isEmpty = false;         return isEmpty;    }    bool Stack::isFull()    {         bool isFull = true;         if(myTop < theCapacity - 1)            isFull = false;         return isFull;    }    inline stackElementType Stack::Top()    {         return theArray[myTop];    } // // Notice that before an element is added it is determined // whether the stack is full. If it is, an error messages // is sent. // // // Notice that since item is not changed it is modified by const // also notice the reference operator.    void Stack::Push(const stackElementType &item)    {         if(isFull())            cerr << "The stack is full.";         else         {            ++myTop;            theArray[myTop] = item;         }    } // // Notice that before an element is removed it is determined // whether the stack is emplty. If it is, an error messages // is sent. //    void Stack::Pop()    {         if(isEmpty())         {            cerr << "The stack is empty.";         }         else            --myTop;    }    void Stack::display()    {         cout<< "From the top down the stack elements are:" << endl << endl;         for(long index=myTop; index >=0; --index)           cout << theArray[short(index)] << endl;    } // // Below is the test program to determine whether the stack class // works or not. // void main() {      short which;      char resp;      stackElementType item;      Stack aStack;      do      {        clear_screen();        cout << "Select an option:" << endl << endl             << "1. Add to the stack" << endl << endl             << "2. Is the stack empty?" << endl << endl             << "3. Is the stack full?" << endl << endl             << "4. What is the top element in the stack?" << endl << endl             << "5. Pop the top element of the stack" << endl << endl             << "6. Display all of the elements of the stack" << endl << endl             << "7. Exit the program." << endl << endl             << endl <<endl << "Which option? ";        cin >> which;        switch(which)        {           case 1:                clear_screen();                cout << "Enter an element to add to the stack: ";                cin >> item;                aStack.Push(item);                break;           case 2:                clear_screen();                if(aStack.isEmpty())                   cout << "The stack is empty.";                else                   cout << "The stack is not empty.";                cout << endl << endl << "Continue? (Y/N) ";                cin >> resp;                break;           case 3:                clear_screen();                if(aStack.isFull())                   cout << "The stack is full.";                else                   cout << "The stack is not full.";                cout << endl << endl << "Continue? (Y/N) ";                cin >> resp;                break;           case 4:                clear_screen();                if(aStack.isEmpty())                   cout << "The stack is empty" << endl;                else                  cout << "The top element in the stack is: "                     << aStack.Top() << endl;                cout << endl << endl << "Continue? (Y/N) ";                cin >> resp;                break;           case 5:                clear_screen();                if(aStack.isEmpty())                   cout << "The stack is empty" << endl;                else                {                  aStack.Pop();                  cout << "The top element has been removed. " << endl;                }                cout << endl << endl << "Continue? (Y/N) ";                cin >> resp;                break;           case 6:                clear_screen();                if(aStack.isEmpty())                   cout << "The stack is empty" << endl;                else                   aStack.display();                cout << endl << "Continue? (Y/N) ";                cin >> resp;                break;           case 7:                break;           default:                break;        }      }while(which != 7);      clear_screen(); } inline void clear_screen() {      for(short index=1;index<=250;++index)             cout << endl; } //  program_id      queue1.cpp //  author          don voils //  date written   10/24/2006 // //  description: This program simulates a queue //               based on a linked list. // #include<iostream> using namespace std; void clear_screen(); typedef long datatype; #include"queue1.h" void main() {   short which;   char resp;   datatype item;   Queue a_queue;   do   {     clear_screen();     cout << "Select an option:" << endl << endl          << "1. Add to the queue" << endl << endl          << "2. Is the queue empty?" << endl << endl          << "3. What is the front element in the queue?" << endl << endl          << "4. Remove the front element of the queue" << endl << endl          << "5. Display all of the elements of the queue" << endl << endl          << "6. Exit the program." << endl << endl          << endl <<endl << "Which option? ";     cin >> which;     switch(which)     {        case 1:          clear_screen();          cout << "Enter an element to add to the queue: ";          cin >> item;          a_queue.Push(item);          break;        case 2:          clear_screen();          if(a_queue.empty())             cout << "The queue is empty.";          else             cout << "The queue is not empty.";          cout << endl << endl << "Continue? (Y/N)";          cin >> resp;          break;        case 3:          clear_screen();          if(!a_queue.empty())             cout << "The Front element in the queue is: "                  << a_queue.Top();          else             cout << "The queue is empty.";             cout << endl << endl << "Continue? (Y/N)";             cin >> resp;             break;        case 4:          clear_screen();          if(!a_queue.empty())          {             a_queue.Pop();             cout << "The front element has been removed. ";          }          else             cout << "There were no elements to remove";          cout << endl << endl << "Continue? (Y/N)";          cin >> resp;          break;        case 5:          clear_screen();          a_queue.display();          cout << endl << "Continue? (Y/N)";          cin >> resp;          break;        case 6:          break;        default:          break;     }   }while(which != 6);   clear_screen(); } void clear_screen() {   for(short index=1;index<=250;++index)        cout << endl; } // program_id   queue1.h // written_by   Don Voils // date_written 9/1/2006 // // description  This header contains the definition //              of a queue based upon a linked list. // #ifndef QUEUE #define QUEUE class node {    public:      datatype data;      node * next;      node(datatype value,node* ptr)      {        data = value;        next = ptr;      } }; class Queue {    private:      node* myTop;    public:      Queue()      {        myTop = NULL;      }      ~Queue()      {        node * ptr;        while(myTop!=NULL)        {          ptr = myTop;          myTop = myTop->next;          delete ptr;        }      }      bool empty()      {        return (myTop==NULL);      }      void Push(datatype new_value)      {        node * newptr = new node(new_value,NULL);        if(myTop!=NULL)        {           node * preptr = myTop;           while((preptr->next)!=NULL)             preptr = preptr->next;           preptr->next = newptr;        }        else           myTop = newptr;      }      void Pop()      {        if(!empty())        {           node *ptr = myTop;           myTop = myTop->next;           delete ptr;        }      }      datatype Top()      {        return myTop->data;      }      void display()      {         cout << "The elements in the list are:" << endl;         node * ptr = myTop;         while(ptr != NULL)         {          cout << ptr->data << endl;          ptr = ptr->next;         }     } }; #endif // program_id    queue2.h // written_by   don Voils // date_written 9/1/2006 // // description  This header contains the definition //              of a queue that adds the feature of //              a pointer to the back of the queue //              so that the queue is more efficient //              when adding nodes to the queue. #ifndef QUEUE #define QUEUE class node {    public:      datatype data;      node * next;      node(datatype value,node* ptr)      {        data = value;        next = ptr;      } }; class Queue {    private:      node* myTop,          * myBack;    public:      Queue()      {        myTop = myBack = NULL;      }      ~Queue()      {        node * ptr;        while(myTop!=NULL)        {          ptr = myTop;          myTop = myTop->next;          delete ptr;        }     }     bool empty()     {       return (myTop==NULL);     }     void Push(datatype new_value)     {       node * newptr = new node(new_value,NULL);       if(!empty())               {          node * preptr = myBack;          preptr->next = myBack = newptr;              }       else          myTop = myBack = newptr;     }     void Pop()     {       if(!empty())       {          node *ptr = myTop;          myTop = myTop->next;          delete ptr;       }     }     datatype Top()     {       return myTop->data;     }     void display()     {        cout << "The elements in the list are:" << endl;        node * ptr = myTop;        while(ptr != NULL)        {         cout << ptr->data << endl;         ptr = ptr->next;        }     } }; #endif // program_id   queue3.h // written_by   Don Voils // date_written 9/1/2006 // // description  This header contains the definition //              of a circular queue. // #ifndef QUEUE #define QUEUE class node {    public:      datatype data;      node * next;      node(datatype value,node* ptr)      {        data = value;        next = ptr;      } }; class Queue {    private:      node* myTop;    public:      Queue()      {        myTop = NULL;      }      ~Queue()      {        node * ptr;        while(myTop!=NULL)        {          ptr = myTop;          if(myTop != myTop->next)             myTop = myTop->next;          else             myTop = NULL;          delete ptr;        }     }     bool empty()     {       return (myTop==NULL);     }     void Push(datatype new_value)     {       node * newptr = new node(new_value,0);       if(!empty())       {          node * preptr = myTop;          while((preptr->next)!= myTop)            preptr = preptr->next;          preptr->next = newptr;          newptr->next = myTop;       }       else       {          myTop = newptr;          newptr->next = newptr;       }     }     void Pop()     {        if(!empty())        {           if(myTop==(myTop->next))           {              node *ptr = myTop;              myTop = NULL;              delete ptr;           }           else           {              node * preptr = myTop->next;              while((preptr->next)!= myTop)                preptr = preptr->next;                preptr->next = myTop->next;                node *ptr = myTop;                myTop = myTop->next;                delete ptr;            }         }      }      datatype Top()      {         return myTop->data;      }      void display()      {         cout << "The elements in the list are:" << endl;         if(!empty())         {            node * ptr = myTop;            do            {              cout << ptr->data << endl;              ptr = ptr->next;            }while(ptr != myTop);         }      } }; #endif //  program_id     stack1.cpp //  written_by     don voils //  date_written   10/24/2006 // //  description Demonstrates how a stack can be implemented //              using a linked list. // #include<iostream> using namespace std; typedef long datatype; #include"stack1.h" void clear_screen(); void main() {   short which;   char resp;   datatype item;   Stack a_stack;   do   {      clear_screen();      cout << "Select an option:" << endl << endl           << "1. Add to the stack" << endl << endl           << "2. Is the stack empty?" << endl << endl           << "3. What is the top element in the stack?" << endl << endl           << "4. Pop the top element of the stack" << endl << endl           << "5. Display all of the elements of the stack" << endl << endl           << "6. Exit the program." << endl << endl           << endl <<endl << "Which option? ";      cin >> which;      switch(which)      {         case 1:           clear_screen();           cout << "Enter an element to add to the stack: ";           cin >> item;           a_stack.Push(item);           break;         case 2:           clear_screen();           if(a_stack.empty())              cout << "The stack is empty.";           else              cout << "The stack is not empty.";           cout << endl << endl << "Continue? (Y/N)";           cin >> resp;           break;         case 3:           clear_screen();           cout << "The top element in the stack is: "                << a_stack.Top();           cout << endl << endl << "Continue? (Y/N)";           cin >> resp;           break;         case 4:           clear_screen();           a_stack.Pop();           cout << "The top element has been removed. "              << endl << endl << "Continue? (Y/N)";           cin >> resp;           break;         case 5:           clear_screen();           a_stack.display();           cout << endl << "Continue? (Y/N)";           cin >> resp;           break;         case 6:           break;         default:           break;     }   }while(which != 6);   clear_screen(); } void clear_screen() {   for(short index=1;index<=250;++index)        cout << endl; } //  program_id     stack1.h //  written_by     don voils //  date_written   10/24/2006 // //  description:  Contains the definition of a stack using a linked list. // #ifndef STACK #define STACK class node {    public:      datatype data;      node * next;      node(datatype value,node* ptr)      {        data = value;        next = ptr;      } }; class Stack {    private:      node* myTop;    public:      Stack()      {        myTop = NULL;      }      ~Stack()      {         node * ptr;         while(myTop!=NULL)         {           ptr = myTop;           myTop = myTop->next;           delete ptr;         }      }      bool empty()      {        return (myTop==NULL);      }      void Push(datatype new_value)      {        node * newptr = new node(new_value,myTop);        myTop = newptr;      }      void Pop()      {        if(!empty())        {           node *ptr = myTop;           myTop = myTop->next;           delete ptr;        }      }      datatype Top()      {        return myTop->data;      }      void display()      {         cout << "The elements in the list are:" << endl;         node * ptr = myTop;         while(ptr != NULL)         {           cout << ptr->data << endl;           ptr = ptr->next;         }      } }; #endif // program_id       stack.cpp // author           don voils // date written    9/3/2006 // // description  This program simulates a generalized stack. // #include<iostream> using namespace std; void clear_screen(); // // The variable theCapacity is only 10 so that the stack is easy to test. // const long theCapacity = 10; typedef long stackElementType; class stack {      private:           long myTop;           stackElementType theArray[theCapacity]; // // The member function declarations are the prototypes // because the definitions should be separate and in // another file. //      public:           stack();           bool isEmpty();           bool isFull();           void Push(const stackElementType &item);           stackElementType Top();           void Pop();           void display(); }; // // Member function definitions. // // Notice that the simple member functions are declared as inline. //    inline stack::stack()    {            myTop=-1;    }    bool stack::isEmpty()    {           bool isEmpty = true;           if(myTop > -1)               isEmpty = false;           return isEmpty;    }    bool stack::isFull()    {            bool isFull = true;            if(myTop < theCapacity-1)                isFull = false;            return isFull;    }    inline stackElementType stack::Top()    {             return theArray[myTop];    } // // Notice that before an element is added, it is determined // whether the stack is full. If it is, an error messages // is sent. // // // Notice that since item is not changed, it is modified by const // also notice the reference operator.    void stack::Push(const stackElementType &item)    {           if(isFull())           {                 cout << "The stack is full." << endl << endl                                             << "Continue? ";                 char resp;                 cin >> resp;           }           else           {                 ++myTop;                 theArray[myTop] = item;           }    } // // Notice that before an element is removed, it is determined // whether the stack is emplty. If it is, an error messages // is sent. //       void stack::Pop()       {            if(isEmpty())            {                 cerr << "The stack is empty.";            }            else                 --myTop;       }       void stack::display()       {            cout<< "From the top down the stack elements are:" << endl << endl;            for(long index=myTop; index >=0; --index)              cout << theArray[short(index)] << endl;       } // // Below is the test program to determine whether the stack class // works or not. // void main() {      short which;      char resp;      stackElementType item;      stack aStack;      do      {           clear_screen();           cout << "Select an option:" << endl << endl                              << "1. Add to the stack" << endl << endl                              << "2. Is the stack empty?" << endl << endl                              << "3. Is the stack full?" << endl << endl                              << "4. What is the top element in the stack?" << endl << endl                              << "5. Pop the top element of the stack" << endl << endl                              << "6. Display all of the elements of the stack" << endl << endl                              << "7. Exit the program." << endl << endl                              << endl <<endl << "Which option? ";           cin >> which;           switch(which)           {                case 1:                     clear_screen();                     cout << "Enter an element to add to the stack: ";                     cin >> item;                     aStack.Push(item);                     break;                case 2:                     clear_screen();                     if(aStack.isEmpty())                          cout << "The stack is empty.";                     else                          cout << "The stack is not empty.";                     cout << endl << endl << "Continue? (Y/N) ";                     cin >> resp;                     break;                case 3:                     clear_screen();                     if(aStack.isFull())                           cout << "The stack is full.";                     else                           cout << "The stack is not full.";                     cout << endl << endl << "Continue? (Y/N) ";                     cin >> resp;                     break;                case 4:                     clear_screen();                     if(aStack.isEmpty())                           cout << "The stack has no elements." << endl;                     else                           cout << "The top element in the stack is: "                                                                  << aStack.Top();                     cout << endl << endl << "Continue? (Y/N) ";                     cin >> resp;                     break;                case 5:                     clear_screen();                     if(aStack.isEmpty())                           cout << "The stack has no elements." << endl;                     else                     {                          aStack.Pop();                          cout << "The top element has been removed. "                                                               << endl;                     }                     cout << endl << endl << "Continue? (Y/N) ";                     cin >> resp;                     break;                case 6:                     clear_screen();                                                 if(aStack.isEmpty())                          cout << "The stack has no elements. "<< endl;                     else                          aStack.display();                     cout << endl << "Continue? (Y/N) ";                     cin >> resp;                     break;                case 7:                     break;                default:                     break;          }       }while(which != 7);       clear_screen(); } inline void clear_screen() {       for(short index=1;index<=250;++index)                 cout << endl; } // program_id   stacks.h // written_by   don voils // date_written 3/2/2006 // // description  This header file contains a class definition //              of a generalized stack class based upon an array. //              The stack type and the size of the stack must be //              provided by the calling program. // #ifndef STACK    #define STACK class Stack {     private:          long myTop;          StackElementType theStack[capacity]; // // The member function declarations are the prototypes // because the definitions should be separate and in // another file. //      public:           Stack();           bool isEmpty();           bool isFull();           void Push(const StackElementType &item);           StackElementType Top();           void Pop();           void Display(); }; // // Member function definitions. // // Normally these would be in another file. // // Notice that the simple member functions are declared as inline. //      inline Stack::Stack()      {           myTop=-1;      }      bool Stack::isEmpty()      {           bool isEmpty = true;           if(myTop > -1)               isEmpty = false;           return isEmpty;      }      bool Stack::isFull()      {           bool isFull = true;           if(myTop < capacity - 1)               isFull = false;           return isFull;      }      inline StackElementType Stack::Top()      {           return theStack[myTop];      } // // Notice that before an element is added it is determined // whether the stack is full. If it is, an error messages // is sent. // // // Notice that since item is not changed it is modified by const // also notice the reference operator.       void Stack::Push(const StackElementType &item)       {            if(isFull())                  cerr << "The stack is full.";            else            {                  ++myTop;                  theStack[myTop] = item;            }    } // // Notice that before an element is removed it is determined // whether the stack is emplty. If it is, an error messages // is sent. //      void Stack::Pop()      {           if(isEmpty())           {                 cerr << "The stack is empty.";           }           else                 --myTop;      }      void Stack::Display()      {           cout<< "From the top down the stack elements are:" << endl << endl;           for(short index= (short)myTop; index >=0; --index)             cout << theStack[index] << endl;      } #endif // program_id    templateQueue.h // author:       Don Voils // date_written: 9/17/2006 // // descripton: This header creates a template queue for any data type. // #ifndef TEMPLATEQUEUE    #define TEMPLATEQUEUE template<typename QueueType> class Queue {    /***** Data Members *****/     private:        QueueType my_Array[capacity];        long myFront,           myBack;    /***** Function Members *****/     public:       Queue();       bool isEmptyQ() const;       bool isFullQ() const;       void addQ(const QueueType& value);       QueueType frontQ () const;       void removeQ();       void displayQ() const; }; // end of class declaration template<typename QueueType> Queue<typename QueueType>::Queue() {   myFront = myBack = 0; } template<typename QueueType> bool Queue<typename QueueType>::isEmptyQ() const {   return (myFront == myBack); } template<typename QueueType> bool Queue<typename QueueType>::isFullQ() const {   return (myFront == ((myBack + 1) % capacity)); } template<typename QueueType> void Queue<typename QueueType>::addQ(const QueueType& value) {   if (!isFullQ())   {       my_Array[myBack] = value;       myBack = (myBack + 1) % capacity;   } } template<typename QueueType> QueueType Queue<typename QueueType>::frontQ() const {   return my_Array[myFront]; } template<typename QueueType> void Queue<typename QueueType>::removeQ() {      if(!isEmptyQ())                myFront = (myFront + 1) % capacity;      else                cout << "The queue was empty so no elements were removed."                     << endl; } template<typename QueueType> void Queue<typename QueueType>::displayQ() const {      if(!isEmptyQ())      {         cout << "The following are the elements in the queue:" << endl;         long index;                for(index = myFront; index != myBack; index = ((index +1) % capacity))                   cout << my_Array[index] << endl;      }      else                cout << "The queue has no elements." << endl; } #endif // program_id       templateQueueList.cpp // author          don voils // date written    9/24/2006 // // description    This program simulates a queue //                based on a linked list and templates. // #include<iostream> using namespace std; void clear_screen(); #include"templateQueueList.h" void main() {   short which;   char resp;   long item;   Queue<long> a_queue;   do   {     clear_screen();     cout << "Select an option:" << endl << endl          << "1. Add to the queue" << endl << endl          << "2. Is the queue empty?" << endl << endl          << "3. What is the front element in the queue?" << endl << endl          << "4. Remove the front element of the queue" << endl << endl          << "5. Display all of the elements of the queue" << endl << endl          << "6. Exit the program." << endl << endl          << endl <<endl << "Which option? ";     cin >> which;     switch(which)     {        case 1:          clear_screen();          cout << "Enter an element to add to the queue: ";          cin >> item;          a_queue.Push(item);          break;        case 2:          clear_screen();          if(a_queue.empty())             cout << "The queue is empty." << endl;          else             cout << "The queue is not empty." << endl;          cout << endl << endl << "Continue? (Y/N) ";          cin >> resp;          break;        case 3:          clear_screen();          if(!a_queue.empty())             cout << "The Front element in the queue is: "                << a_queue.Top() << endl;          else            cout << "The queue is empty." << endl;          cout << endl << endl << "Continue? (Y/N) ";          cin >> resp;          break;        case 4:          clear_screen();          if(!a_queue.empty())          {             a_queue.Pop();             cout << "The front element has been removed. " << endl;          }          else             cout << "There were no elements to remove" << endl;          cout << endl << endl << "Continue? (Y/N) ";          cin >> resp;          break;        case 5:          clear_screen();          if(a_queue.empty())             cout << "The queue is empty." << endl;          else             a_queue.display();          cout << endl << "Continue? (Y/N) ";          cin >> resp;          break;        case 6:          break;        default:          break;     }   }while(which != 6);   clear_screen(); } void clear_screen() {   for(short index=1;index<=250;++index)        cout << endl; } // program_id    templateQueueList.h // written_by    Don Voils // date_written 6/9/2006 // // Description  This file contains the definition //              of a template queue based upon //              a list. // #ifndef QUEUE #define QUEUE template<typename datatype> class node {    public:      datatype data;      node * next;      node(datatype value,node* ptr)      {        data = value;        next = ptr;      } }; template<typename datatype> class Queue {    private:      node<datatype>* myTop;    public:      Queue()      {        myTop = NULL;      }      ~Queue()      {        node<datatype> * ptr;        while(myTop!=NULL)        {          ptr = myTop;          myTop = myTop->next;          delete ptr;        }     }     bool empty()     {       return (myTop==NULL);     }     void Push(datatype new_value)     {       node<datatype> * newptr = new node<datatype>(new_value,NULL);       if(myTop!=NULL)       {           node<datatype> * preptr = myTop;           while((preptr->next)!=NULL)           preptr = preptr->next;           preptr->next = newptr;       }       else           myTop = newptr;     }     void Pop()     {       if(!empty())       {          node<datatype> *ptr = myTop;          myTop = myTop->next;          delete ptr;       }     }     datatype Top()     {       return myTop->data;     }     void display()     {        cout << "The elements in the list are:" << endl;        node<datatype> * ptr = myTop;        while(ptr != NULL)        {          cout << ptr->data << endl;          ptr = ptr->next;        }     } }; #endif // program_id    templateStack.h // written_by    Don Voils // date_written 9/1/2006 // // Description  This file contains the definition of the //              template stack. // #ifndef STACK #define STACK // // The variable theCapacity is only 10 so that the stack is easy to test. // const long theCapacity = 10; template<typename stackElementType> class stack {      private:           long myTop;           stackElementType theArray[theCapacity]; // // The member function declarations are the prototypes // because the definitions should be separate and in // another file. //      public:           stack();           bool isEmpty();           bool isFull();           void Push(const stackElementType &item);           stackElementType Top();           void Pop();           void Output();           void display(); };    template<typename stackElementType>    stack<typename stackElementType>::stack()    {         myTop=-1;    }    template<typename stackElementType>    bool stack<typename stackElementType>::isEmpty()    {         bool isEmpty = true;         if(myTop > -1)            isEmpty = false;         return isEmpty;    }    template<typename stackElementType>    bool stack<typename stackElementType>::isFull()    {         bool isFull = true;         if(myTop < theCapacity - 1)            isFull = false;         return isFull;    }    template<typename stackElementType>    stackElementType stack<typename stackElementType>::Top()    {         return theArray[myTop];    }    template<typename stackElementType>    void stack<typename stackElementType>::Output()    {         long index;         for(index=0;index<=myTop;++index)              cout << theArray[index] << endl;    } // // Notice that before an element is added it is determined // whether the stack is full. If it is, an error messages // is sent. // // // Notice that since item is not changed it is modified by const // also notice the reference operator.    template<typename stackElementType>    void stack<typename stackElementType>::Push(const stackElementType &item)    {         if(isFull())            cerr << "The stack is full.";         else         {            ++myTop;            theArray[myTop] = item;         }    } // // Notice that before an element is removed it is determined // whether the stack is emplty. If it is, an error messages // is sent. //    template<typename stackElementType>    void stack<typename stackElementType>::Pop()    {         if(isEmpty())         {            cerr << "The stack is empty.";         }         else           --myTop;   }   template<typename stackElementType>   void stack<typename stackElementType>::display()   {        cout<< "From the top down the stack elements are:" << endl << endl;        for(long index=myTop; index >=0; --index)          cout << theArray[short(index)] << endl;   } #endif //  program_id     templateStackList.cpp //  written_by     Don Voils //  date_written  10/24/2006 // //  description Demonstrates how a template stack can be implemented //              using a linked list. // // // // // #include<iostream> using namespace std; #include"templateStackList.h" void clear_screen(); void main() {   short which;   char resp;   long item;   Stack<long> a_stack;   do   {      clear_screen();      cout << "Select an option:" << endl << endl           << "1. Add to the stack" << endl << endl           << "2. Is the stack empty?" << endl << endl           << "3. What is the top element in the stack?" << endl << endl           << "4. Pop the top element of the stack" << endl << endl           << "5. Display all of the elements of the stack" << endl << endl           << "6. Exit the program." << endl << endl           << endl <<endl << "Which option? ";      cin >> which;      switch(which)      {         case 1:           clear_screen();           cout << "Enter an element to add to the stack: ";           cin >> item;           a_stack.Push(item);           cout << endl << endl << "Continue? (Y/N) ";           cin >> resp;           break;         case 2:           clear_screen();           if(a_stack.empty())              cout << "The stack is empty.";           else              cout << "The stack is not empty.";           cout << endl << endl << "Continue? (Y/N) ";           cin >> resp;           break;         case 3:           clear_screen();           if(a_stack.empty())              cout << "The stack is empty.";           else              cout << "The top element in the stack is: "                   << a_stack.Top();           cout << endl << endl << "Continue? (Y/N) ";           cin >> resp;           break;         case 4:           clear_screen();           if(a_stack.empty())              cout << "The stack is empty.";           else           {              a_stack.Pop();              cout << "The top element has been removed. ";           }           cout << endl << endl << "Continue? (Y/N) ";           cin >> resp;           break;         case 5:           clear_screen();           if(a_stack.empty())              cout << "The stack is empty.";           else              a_stack.display();           cout << endl << endl << "Continue? (Y/N) ";           cin >> resp;           break;         case 6:           break;         default:           break;     }   }while(which != 6);   clear_screen(); } void clear_screen() {   for(short index=1;index<=250;++index)        cout << endl; } // program_id   templateStackList.h // written_by   Don Voils // date_written 10/24/2006 // // Description  This header file contains the definition of a //              template stack that is based upon a linked list. // #ifndef TEMPLATESTACKLIST #define TEMPLATESTACKLIST template<typename datatype> class node {    public:      datatype data;      node<typename datatype> * next;      node<typename datatype>(datatype value,node* ptr)      {        data = value;        next = ptr;      } }; template<typename datatype> class Stack {    private:      node<typename datatype>* myTop;    public:      Stack()      {        myTop = NULL;      }      ~Stack()      {         node<typename datatype> * ptr;         while(myTop!=NULL)         {           ptr = myTop;           myTop = myTop->next;           delete ptr;         }     }     bool empty()     {       return (myTop==NULL);     }     void Push(datatype new_value)     {       node<typename datatype> * newptr =                new node<typename datatype>(new_value,myTop);       myTop = newptr;     }     void Pop()     {       if(!empty())       {          node<typename datatype> *ptr = myTop;          myTop = myTop->next;          delete ptr;       }     }     datatype Top()     {       return myTop->data;     }     void display()     {       cout << "The elements in the list are:" << endl;       node<typename datatype> * ptr = myTop;       while(ptr != NULL)       {        cout << ptr->data << endl;        ptr = ptr->next;       }     } }; #endif // program_id       useQueue.cpp // author           don voils // date written    9/17/2006 // // description     This program simulates a long queue. // #include<iostream> using namespace std; void clear_screen(); #include "long_queue.h" // // Below is the test program to determine whether the queue class // works or not. // void main() {      short which;      char resp;      long item;      Queue a_queue;      do      {          clear_screen();          cout << "Select an option:" << endl << endl                            << "1. Add to the queue" << endl << endl                            << "2. Is the queue empty?" << endl << endl                            << "3. Is the queue full?" << endl << endl                            << "4. What is the front element in the queue?" << endl << endl                            << "5. Remove the front element of the queue" << endl << endl                            << "6. Display all of the elements of the queue" << endl << endl                            << "7. Exit the program." << endl << endl                            << endl <<endl << "Which option? ";          cin >> which;          switch(which)          {               case 1:                    clear_screen();                    cout << "Enter a long to add to the queue: ";                    cin >> item;                    if(!a_queue.isFullQ())                    {                                                           a_queue.addQ(item);                         cout << endl                                                               << "The queue is not full so an "                                                               << "element was added."                                                               << endl;                         cout << endl << endl << "Continue? (Y/N)";                         cin >> resp;                    }                    else                    {                         cout << "The queue is full and so no element "                                                                << "was added." << endl;                         cout << endl << endl << "Continue? (Y/N)";                         cin >> resp;                    }                    break;               case 2:                    clear_screen();                                              if(a_queue.isEmptyQ())                                                  cout << "The queue is empty.";                                              else                        cout << "The queue is not empty.";                    cout << endl << endl << "Continue? (Y/N)";                    cin >> resp;                    break;               case 3:                    clear_screen();                    if(a_queue.isFullQ())                         cout << "The queue is full.";                    else                         cout << "The queue is not full.";                    cout << endl << endl << "Continue? (Y/N)";                    cin >> resp;                    break;               case 4:                    clear_screen();                    if(!a_queue.isEmptyQ())                       cout << "The Front element in the queue is: "                            << a_queue.frontQ();                    else                                   cout << "The queue is empty.";                    cout << endl << endl << "Continue? (Y/N)";                    cin >> resp;                    break;               case 5:                    clear_screen();                    if(!a_queue.isEmptyQ())                    {                       a_queue.removeQ();                       cout << "The front element has been removed. ";                    }                    else                      cout << "There were no elements to remove";                    cout << endl << endl << "Continue? (Y/N)";                    cin >> resp;                    break;               case 6:                    clear_screen();                    a_queue.displayQ();                    cout << endl << "Continue? (Y/N)";                    cin >> resp;                    break;               case 7:                    break;               default:                    break;            }        }while(which != 7);        clear_screen(); } inline void clear_screen() {        for(short index=1;index<=250;++index)                   cout << endl; } // program_id       useTemplateQueue.cpp // author           don voils // date written    9/17/2006 // // description  This program simulates a long queue //              using templates. // #include<iostream> using namespace std; void clear_screen(); // // The date class is included to demonstrate the generality of // the class: Queue contained in the header: newQueue.h that is // included below. // #include"date.h" // // The variable capacity is only 10 so that the queue is easy to test. // const long capacity = 10; #include"templateQueue.h" // // Below is the test program to determine whether the queue class // works or not. // void main() {      short which;      char resp;      Date item;      Queue<Date> aQueue;      do      {         clear_screen();         cout << "Select an option:" << endl << endl                            << "1. Add to the queue" << endl << endl                            << "2. Is the queue empty?" << endl << endl                            << "3. Is the queue full?" << endl << endl                            << "4. What is the front element in the queue?" << endl << endl              << "5. Remove the front element of the queue" << endl << endl              << "6. Display all of the elements of the queue" << endl << endl              << "7. Exit the program." << endl << endl              << endl <<endl << "Which option? ";         cin >> which;         switch(which)         {              case 1:                   clear_screen();                   if(!aQueue.isFullQ())                   {                      cout << "Enter a date to add to the "                                                        << "queue (mm/dd/yyyy): ";                                                  cin >> item;                            aQueue.addQ(item);                                                  cout << endl << "The queue is not full so an "                                                       << "element was added." << endl;                                                  cout << endl << endl << "Continue? (Y/N)";                        cin >> resp;                   }                   else                   {                                                  cout << "The queue is full and so no element "                                                       << "was added." << endl;                                                  cout << endl << endl << "Continue? (Y/N)";                        cin >> resp;                     }                     break;                case 2:                     clear_screen();                                                  if(aQueue.isEmptyQ())                                                       cout << "The queue is empty.";                     else                                                       cout << "The queue is not empty.";                     cout << endl << endl << "Continue? (Y/N)";                     cin >> resp;                     break;                case 3:                     clear_screen();                     if(aQueue.isFullQ())                          cout << "The queue is full.";                     else                          cout << "The queue is not full.";                     cout << endl << endl << "Continue? (Y/N)";                     cin >> resp;                     break;                case 4:                     clear_screen();                     if(!aQueue.isEmptyQ())                        cout << "The Front element in the queue is: "                             << aQueue.frontQ();                     else                                     cout << "The queue is empty.";                     cout << endl << endl << "Continue? (Y/N)";                     cin >> resp;                     break;                case 5:                     clear_screen();                     if(!aQueue.isEmptyQ())                     {                        aQueue.removeQ();                        cout << "The front element has been removed. ";                     }                     else                        cout << "There were no elements to remove";                     cout << endl << endl << "Continue? (Y/N)";                     cin >> resp;                     break;                case 6:                     clear_screen();                     aQueue.displayQ();                     cout << endl << "Continue? (Y/N)";                     cin >> resp;                     break;                case 7:                     break;               default:                     break;            }        }while(which != 7);        clear_screen(); } inline void clear_screen() {       for(short index=1;index<=250;++index)                  cout << endl; } // program_id        useTemplateStack.cpp // author            don voils // date written     6/30/2006 // // description  This program simulates a template stack. // #include<iostream> using namespace std; // Include the header of whatever data type is // to be used in the template stack. // #include"date.h" void clear_screen(); // Include the header of the template stack class. // #include"templateStack.h" // // Below is the test program to determine whether the stack class // works or not. // void main() {      short which;      char resp;      Date item;      stack<Date> aStack;      do      {         clear_screen();         cout << "Select an option:" << endl << endl              << "1. Add to the stack" << endl << endl              << "2. Is the stack empty?" << endl << endl              << "3. Is the stack full?" << endl << endl              << "4. What is the top element in the stack?" << endl << endl              << "5. Pop the top element of the stack" << endl << endl              << "6. Display all of the elements of the stack" << endl << endl              << "7. Exit the program." << endl << endl              << endl <<endl << "Which option? ";          cin >> which;          switch(which)          {              case 1:                   clear_screen();                   cout << "Enter an element to add to the stack: (mm/dd/yyyy) ";                   cin >> item;                   aStack.Push(item);                   break;              case 2:                   clear_screen();                   if(aStack.isEmpty())                         cout << "The stack is empty.";                   else                         cout << "The stack is not empty.";                   cout << endl << endl << "Continue? (Y/N)";                   cin >> resp;                   break;              case 3:                   clear_screen();                   if(aStack.isFull())                         cout << "The stack is full.";                   else                         cout << "The stack is not full.";                   cout << endl << endl << "Continue? (Y/N)";                   cin >> resp;                   break;              case 4:                   clear_screen();                   if(aStack.isEmpty())                      cout << "The stack is empty" << endl;                   else                      cout << "The top element in the stack is: "                           << aStack.Top() << endl;                   cout << endl << endl << "Continue? (Y/N)";                   cin >> resp;                   break;              case 5:                   clear_screen();                   if(aStack.isEmpty())                      cout << "The stack is empty" << endl;                   else                   {                     aStack.Pop();                     cout << "The top element has been removed. " << endl;                   }                   cout << endl << endl << "Continue? (Y/N)";                   cin >> resp;                   break;              case 6:                   clear_screen();                   if(aStack.isEmpty())                      cout << "The stack is empty" << endl;                   else                      aStack.display();                   cout << endl << "Continue? (Y/N)";                   cin >> resp;                   break;              case 7:                   break;              default:                  break;        }      }while(which != 7);      clear_screen(); } void clear_screen() {      for(short index=1;index<=250;++index)             cout << endl; } 




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net