-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.c
More file actions
74 lines (69 loc) · 1.27 KB
/
Copy pathQueue.c
File metadata and controls
74 lines (69 loc) · 1.27 KB
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
#include<stdio.h>
#include<stdlib.h>
struct QueueNode{
int value;
struct QueueNode *next;
};
struct QueueNode *Front = NULL;
struct QueueNode *Rear = NULL;
void Enqueue(int val);
int Dequeue();
void Print();
int main(){
Enqueue(5);
Enqueue(10);
Enqueue(15);
Enqueue(20);
Print();
Dequeue();
Dequeue();
Print();
}
void Enqueue(int val){
struct QueueNode *ptr;
ptr = (struct QueueNode*) malloc (sizeof(struct QueueNode));
ptr->next = NULL;
ptr->value = val;
if(Front == NULL){
Rear = ptr;
Front = ptr;
}else{
// struct QueueNode *curr = Rear;
// while(curr->next != NULL){
// curr = curr->next;
// }
// curr->next = ptr;
// Rear = ptr;
while(Rear->next!=NULL){
Rear = Rear->next;
}
Rear->next = ptr;
Rear = ptr;
}
}
int Dequeue(){
if(Front == NULL){
printf("***Queue is Empty***\n");
}else{
struct QueueNode *curr = Front;
printf("Deleted: %d\n",curr->value);
Front = Front->next;
free(curr);
}
}
void Print(){
// printf("Front = %d\n",Front->value);
// printf("Rear = %d\n",Rear->value);
printf("Elements in Queue are: \n");
if(Front == NULL){
printf("***Queue is Empty***\n");
}else{
struct QueueNode *curr = Front;
while(curr != NULL){
printf("%d -> ",curr->value);
curr = curr->next;
}
printf("NULL\n");
}
printf("\n");
}