forked from amritanand-py/cps02
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN 101 - The Queue Data Structure.cpp
80 lines (76 loc) · 1.32 KB
/
N 101 - The Queue Data Structure.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "math.h"
struct queueNode{
int data;
struct queueNode* next;
};
typedef struct queueNode queueNode;
queueNode* head = NULL;
queueNode* newNode(int val)
{
queueNode* t = (queueNode*) malloc(sizeof(queueNode));
t->data = val;
t->next = NULL;
return t;
}
void push(int val)
{
queueNode* p,*p1;
p=newNode(val);
if(head==NULL)
head=p;
else
{
p1=head;
while(p1->next!=NULL)
{
p1=p1->next;
}
p1->next=p;
}
}
void pop()
{
queueNode* p1;
p1=head;
head=head->next;
p1->next=NULL;
}
int top()
{
return head->data;
}
int empty()
{
return (head==NULL);
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
char s[50];
int x;
scanf(" %s", s);
if (s[0] == 't')
{
if (empty()) printf("Invalid\n");
else
printf("%d\n", top());
}
else if (s[0] == 'p' && s[1] == 'o')
{
if (empty()) printf("Invalid\n");
else pop();
}
else
{
scanf(" %d", &x);
push(x);
}
}
return 0;
}