Commands
Coding Problem Keys
Commands
Problem Statement
Series of N commands are passed as input. If the commands is 1, the next input given is added to the queue. If the command is -1, an element is removed from the queue and printed. Fill in the missing lines of code to implement the functions add and poll so that the program runs successfully.
Boundary Condition(S)
1 <= N <= 10000
Input Format
The first line contains the value of N.
The next N line contains polled integers separated by a space.
Output Format
The first line contains polled integers separated by a space.
Example Input/Output 1
Input
5
1 45
1 67
-1
1 89
-1
Output
45 67
Example Input/Output 2
Input
7
1 67
-1
1 87
1 23
1 89
-1
-1
Output
67 87 23
Note: Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: C Language
#include <stdio.h> struct Node { int data; struct Node *next; }; struct Node *head = NULL; struct Node *tail = NULL;
void add(int n){
struct Node *a=(struct Node*)malloc(sizeof(struct Node*));
a->data=n;
a->next=NULL;
if(head==NULL){
head=a;
tail=a;
return;
}
tail->next=a;
tail=a;
return;
}
int poll(){
int n=head->data;
head=head->next;
return n;
}
// Published By PKJCODERS
int main() { int N, value, operation; scanf("%d",&N); while(N--) { scanf("%d",&operation); if(operation==1) { scanf("%d",&value); add(value); } else { printf("%d ",poll()); } } }
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments