Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include<bits/stdc++.h>
- using namespace std;
- struct QueueNode
- {
- int data;
- QueueNode *next;
- QueueNode(int a)
- {
- data = a;
- next = NULL;
- }
- };
- struct MyQueue {
- QueueNode *front;
- QueueNode *rear;
- void push(int);
- int pop();
- MyQueue() {front = rear = NULL;}
- };
- int main()
- {
- int T;
- cin>>T;
- while(T--)
- {
- MyQueue *sq = new MyQueue();
- int Q;
- cin>>Q;
- while(Q--){
- int QueryType=0;
- cin>>QueryType;
- if(QueryType==1)
- {
- int a;
- cin>>a;
- sq->push(a);
- }else if(QueryType==2){
- cout<<sq->pop()<<" ";
- }
- }
- cout<<endl;
- }
- }
- // } Driver Code Ends
- /* Structure of a node in Queue
- struct QueueNode
- {
- int data;
- QueueNode *next;
- QueueNode(int a)
- {
- data = a;
- next = NULL;
- }
- };
- And structure of MyQueue
- struct MyQueue {
- QueueNode *front;
- QueueNode *rear;
- void push(int);
- int pop();
- MyQueue() {front = rear = NULL;}
- }; */
- //Function to push an element into the queue.
- void MyQueue:: push(int x)
- {
- // Your Code
- if(front==NULL && rear==NULL)
- {
- QueueNode* newnode=new QueueNode(x);
- front=newnode;
- rear=newnode->next;
- }
- else
- {
- QueueNode* tmp=front;
- while(tmp->next!=rear)
- {
- tmp=tmp->next;
- }
- QueueNode* newnode=new QueueNode(x);
- tmp->next= newnode;
- newnode->next=rear;
- }
- }
- //Function to pop front element from the queue.
- int MyQueue :: pop()
- {
- // Your Code
- int ans=-1;
- if(front==NULL)
- {
- return -1;
- }
- else
- {
- QueueNode* tmp=front;
- ans=tmp->data;
- front=front->next;
- delete(tmp);
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment