-
Notifications
You must be signed in to change notification settings - Fork 15
/
queue_using_linked_list.c
92 lines (86 loc) · 2.15 KB
/
queue_using_linked_list.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
void enqueue(struct node **);
void dequeue(struct node **);
void display(struct node *);
void peek(struct node *);
int main(){
struct node *front=0,*rear=0;
int choice;
printf("Press 1 for Enqueue\nPress 2 for Dequeue\nPress 3 for Display\nPress 4 for Peek\nPress 5 for Exit\n");
do{
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: enqueue(&rear);
if(front==0){
front=rear;
}
break;
case 2: if(front==0 && rear==0){
printf("Queue is Empty");
break;
}
else{
dequeue(&front);
if(front==0){
rear=0;
}
break;
}
case 3: if(front==0){
printf("Queue is Empty");
break;
}
else{
display(front);
break;
}
case 4: if(front==0){
printf("Queue is Empty");
break;
}
else{
peek(front);
break;
}
case 5: break;
default: printf("Invalid Choice");
}
}while(choice!=5);
}
void enqueue(struct node **rear){
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d",&newnode->data);
newnode->next=0;
if((*rear)==0){
(*rear)=newnode;
}
else{
(*rear)->next=newnode;
(*rear)=newnode;
}
}
void dequeue(struct node **front){
struct node *temp=(*front);
printf("The dequeued element is %d",(*front)->data);
(*front)=(*front)->next;
free(temp);
}
void display(struct node *front){
struct node *temp=front;
printf("The elements in the stack are: ");
while(temp!=0){
printf("%d ",temp->data);
temp=temp->next;
}
}
void peek(struct node *front){
printf("The top most element is %d",front->data);
}