-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue-ll.c
More file actions
78 lines (77 loc) · 2.07 KB
/
queue-ll.c
File metadata and controls
78 lines (77 loc) · 2.07 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
75
76
77
78
#include<stdio.h>
#include<stdlib.h>
// In queue we maintain 2 pointers front and rear
//Front ptr always points to the first element in the list and rear always points to the second element
//Queue works on FIFO(First In First Out)
//Element is added from the rear(back) side and is deleted from the front side
typedef struct Node
{
int data;
struct Node* next;
} node;
node* front= NULL;
node* rear= NULL;
node* newnode(int data){
node * temp= (node*) malloc(sizeof(node));
temp->data= data;
temp->next= NULL;
return temp;
}
void enqueue(int data){ //Insertion at the end
node* temp= newnode(data);
if(rear==NULL){
rear= temp;
front= temp;
}
else{ //Traversal is only needed when I don't trust my rear pointer
//Rear pointer will always point to last node
rear->next= temp;
rear= temp;
}
}
void dequeue(){
//Case-1: When the front is null
if(front ==NULL){
rear= NULL;
return;
}
node* temp= front;
front= front->next;
free(temp);
if(front==NULL){ //Cross-checking that after the deletion rear must set to 0 in case when there is no element present
rear= NULL;
}
}
void display(){
node* temp= front;
while(temp!=NULL){
printf("%d ", temp->data);
temp= temp->next;
}
}
int main(){
int input=0;
int data;
while(1){
printf("\nEnter the number for the corresponding operation::");
printf("\n Press 1 to enqueue(insert) data inside the queue");
printf("\n Press 2 to dequeue(delete) data inside the queue");
printf("\n Press 3 to display the queue");
printf("\n Press 4 to exit\n");
scanf("%d", &input);
if(input==1){
printf("Enter data::");
scanf(" %d", &data);
enqueue(data);
}
else if(input==2){
dequeue();
}
else if(input==3){
display();
}
else if(input==4){
break;
}
}
}