//CIS 610-251: Queue and LinkList //Due OCt 14,1999 //Object: Show Insert, Remove and Empty operations in Queue #include #define MAX 100 class Queue { private: int * queue; public: int Remove(void); bool Insert(int); void Empty(); bool isEmpty(); Queue(); }; Queue::Queue() { queue=new int[MAX]; queue[0]=2;//front queue[1]=2;//rear } //EnQueue an integer n. Successful insertion return true; bool Queue::Insert(int n) { if (queue[1]==MAX-1) { if (queue[0]==2) return false; else queue[1]=2;queue[queue[1]]=n; } else { if (queue[1]+1 == queue[0]) return false; else { queue[1]++; queue[queue[1]]=n; } } return true; } //DeQueue an integer and return it. int Queue::Remove() { if (!isEmpty()) { if(queue[0]==MAX-1) { queue[0]=2; return queue[queue[0]]; } else { queue[0]++; return queue[queue[0]]; } } return -1; } //true if Queue is Empty bool Queue::isEmpty(void) { return (queue[0]==queue[1])? true : false; } //render queue empty void Queue::Empty(void) { queue[0]=queue[1]=2; } //Main Program loop starts here main() { Queue q; int i=0; cout << "\nStack of 97 integers Initialized\nEnter items to EnQueue. -1 to exit\n"; while(i!=-1) { cout << "\nEnter # to EnQueue:"; cin >> i; if (q.Insert(i)) cout << "\nSuccessfully EnQueued."; else cout << "\nOverflow...Removing First Item to make space: " << q.Remove(); } return 0; }