forked from guptautkarsh64/Hacktober-Fest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue Linked List.cpp
98 lines (83 loc) · 1.68 KB
/
Queue Linked List.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *next;
};
struct Node *f;
struct Node *r;
void enqueue(int val){
struct Node *n=(struct Node *)malloc(sizeof(struct Node));
if (n==NULL){
printf("Queue is full");
return;
}
else{
n->data=val;
if (f==NULL){
f=r=n;
f->next=NULL;
r->next=NULL;
}else{
r->next=n;
r=n;
r->next=NULL;
}
}
}
void dequeue(){
struct Node* ptr=f;
if (f==NULL){
printf("-1");
return;
}
else{
f=f->next;
free(ptr);
}
}
void display(struct Node *ptr){
while (ptr!=NULL){
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
void peekfront(struct Node *ptr){
printf("%d\n",ptr->data);
}
void sizequeue(struct Node *ptr){
int count=0;
while (ptr!=NULL){
count=count+1;
ptr=ptr->next;
}
printf("%d\n",count);
}
int main(){
//enqueue-1 dequeue-2 display-3 peekfront-4 size-5
for (int i=0;i<15;i++){
int choice=1;
int x;
scanf("%d\n",&choice);
switch(choice){
case 1:
scanf("%d\n",&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display(f);
break;
case 4:
peekfront(f);
break;
case 5:
sizequeue(f);
break;
}
}
return 0;
}