-
Notifications
You must be signed in to change notification settings - Fork 2
/
linked_queues.c
111 lines (101 loc) · 2.45 KB
/
linked_queues.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct queue{
int data;
struct queue *next;
};
struct queue *front = NULL;
struct queue *rear = NULL;
void enqueue(int val){
struct queue *ptr;
ptr = (struct queue*)malloc(sizeof(struct queue));
ptr->data = val;
ptr->next = NULL;
if(rear == NULL){
rear = ptr;
front = ptr;
return;
}
rear->next = ptr;
rear = ptr;
}
int dequeue(){
struct queue *ptr;
int temp;
ptr = (struct queue*)malloc(sizeof(struct queue));
ptr = front;
if(front == NULL){
printf("\nQueue is empty!");
return 0;
}
temp = front->data;
front = front->next;
return temp;
}
int peak(){
if(front == NULL){
return -1;
}
else{
return (front->data);
}
}
void display(){
struct queue *ptr;
ptr = (struct queue*)malloc(sizeof(struct queue));
if(front == NULL){
printf("\nQueue is empty!");
}
else{
ptr = front;
printf("\nThe elements of the queue are;\t");
while(ptr != (rear->next)){
printf("%d ",ptr->data);
ptr = ptr->next;
}
}
}
void main(){
int option,val;
do{
printf("\n******************Menu!**************");
printf("\n1.Enqueue");
printf("\n2.Dequeue");
printf("\n3.Peak");
printf("\n4.Display");
printf("\n5.Exit!");
printf("\nEnter your choice:\t");
scanf("%d",&option);
switch(option){
case 1:
printf("\nEnter the element you wanna enqueue:\t");
scanf("%d",&val);
enqueue(val);
break;
case 2:
val = dequeue();
if(val != 0){
printf("\nThe element dequeud is %d",val);
}
break;
case 3:
val = peak();
if(val != -1){
printf("\n The elemenet at the peak is %d",val);
}
else{
printf("\nQueue is empty!");
}
break;
case 4:
display();
break;
case 5:
break;
default:
printf("\nInvalid chocie!");
}
}while(option != 5);
}