Queue implementation C++ -
hi creating queue assignment have , keep getting output of " 4 4 4 4 4 ". not sure if doing queue wrong on paper or if messed in program. want confirm if queue come out output. included enqueue , dequeue files. thank you
#include <iostream> #include <cstdlib> using namespace std; const int max_size = 100; class queueoverflowexception { public: queueoverflowexception() { cout << "queue overflow" << endl; } }; class queueemptyexception { public: queueemptyexception() { cout << "queue empty" << endl; } }; class arrayqueue { private: int data[max_size]; int front; int rear; public: arrayqueue() { front = -1; rear = -1; } void enqueue(int element) { // don't allow queue grow more // max_size - 1 if ( size() == max_size - 1 ) throw new queueoverflowexception(); data[rear] = element; // mod used rear indicator // can wrap around rear = ++rear % max_size; } int dequeue(int n) { if ( isempty() ) throw new queueemptyexception(); int ret = data[front]; // mod used front indicator // can wrap around front = ++front % max_size; return ret; } int front() { if ( isempty() ) throw new queueemptyexception(); return data[front]; } int size() { return abs(rear - front); } bool isempty() { return ( front == rear ) ? true : false; } }; int main() { arrayqueue q; int x =2; int y = 4; q.enqueue(x); q.enqueue(y); q.dequeue(x); q.enqueue(x+5); q.enqueue(16); q.enqueue(x); q.enqueue(y-3); cout << "queue: "; while(!q.isempty()) { q.dequeue(y); cout<<" " << y; } }
your code have following bugs.
- in enqueue, first rear should incremented , value should inserted.
- dequeue function should not take argument.
- in main function calling dequeue function in wrong manner. dequeue function returns integer should either store return value in variable or output directly output screen using cout.
correct code is:
#include <iostream> #include <cstdlib> using namespace std; const int max_size = 100; class queueoverflowexception { public: queueoverflowexception() { cout << "queue overflow" << endl; } }; class queueemptyexception { public: queueemptyexception() { cout << "queue empty" << endl; } }; class arrayqueue { private: int data[max_size]; int front; int rear; public: arrayqueue() { front = -1; rear = -1; } void enqueue(int element) { // don't allow queue grow more // max_size - 1 if ( size() == max_size - 1 ) throw new queueoverflowexception(); // mod used rear indicator // can wrap around rear = ++rear % max_size; data[rear] = element; } int dequeue() { if ( isempty() ) throw new queueemptyexception(); int ret = data[front]; // mod used front indicator // can wrap around front = ++front % max_size; return ret; } int front() { if ( isempty() ) throw new queueemptyexception(); return data[front]; } int size() { return abs(rear - front); } bool isempty() { return ( front == rear ) ? true : false; } }; int main() { arrayqueue q; int x =2; int y = 4; q.enqueue(); q.enqueue(y); q.dequeue(); q.enqueue(x+5); q.enqueue(16); q.enqueue(x); q.enqueue(y-3); cout << "queue: "; while(!q.isempty()) { cout<<q.dequeue(); } }
Comments
Post a Comment