-
Notifications
You must be signed in to change notification settings - Fork 0
/
46_queueLinked.c
69 lines (57 loc) · 1.28 KB
/
46_queueLinked.c
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
#include<stdlib.h>
#include<stdio.h>
// declared globally
struct node *f = NULL;
struct node *r = NULL;
struct node{
int data;
struct node * next;
};
void Traversal(struct node *ptr){
while (ptr != NULL){
printf("Element is: %d\n", ptr -> data);
ptr = ptr -> next;
}
}
void enqueue(int value){
struct node *ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL){
printf("Queue is full\n");
}
else{
ptr -> data = value;
ptr -> next = NULL;
if(f == NULL){
f = r = ptr;
}
else{
r -> next = ptr;
r = ptr;
}
}
}
int dequeue(){
if (f == NULL){
printf("Queue is Empty\n");
return -1;
}
else{
struct node *ptr = f;
f = f -> next;
int val = ptr -> data;
free(ptr);
return val;
}
}
int main(){
dequeue();
enqueue(20);
enqueue(25);
enqueue(30);
Traversal(f);
printf("The dequeued element is %d\n", dequeue());
printf("The dequeued element is %d\n", dequeue());
printf("The dequeued element is %d\n", dequeue());
printf("The dequeued element is %d\n", dequeue());
return 0;
}