1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
/* The structure of the node of the queue is struct QueueNode { int data; QueueNode *next; }; and the structure of the class is class Queue { private: QueueNode *front; QueueNode *rear; public : void push(int); int pop(); }; */ /* The method push to push element into the queue*/ void Queue:: push(int x) { // Your Code QueueNode *tmp = new QueueNode; tmp->data=x; if(front==NULL) { front=tmp; rear=tmp; } else { rear->next=tmp; rear=rear->next; } } /*The method pop which return the element poped out of the queue*/ int Queue :: pop() { // Your Code if(front==NULL) return -1; int v=front->data; front=front->next; return v; } |