-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularqueuelinkedlist.c
141 lines (122 loc) · 1.84 KB
/
circularqueuelinkedlist.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include<stdio.h>
#include<stdlib.h>
void enqueue();
void dequeue();
void display();
int isempty();
void peek();
struct node
{
int data;
struct node* link;
};
struct node* front = NULL;
struct node* rear = NULL;
int main()
{
int choice;
int referee;
while(1)
{
printf("1.enqueue\n");
printf("2.dequeue\n");
printf("3.display\n");
printf("4.isempty\n");
printf("5.peek\n");
printf("6.exit\n");
printf("enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: enqueue();
break;
case 2:dequeue();
break;
case 3:display();
break;
case 4: referee = isempty();
printf("the result is %d\n",referee);
break;
case 5: peek();
break;
case 6: exit(1);
break;
}
}
return 0;
}
void enqueue()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the element to insert\n");
scanf("%d",&temp->data);
temp->link = NULL;
if(front == NULL && rear == NULL)
{
front = temp;
rear = front;
temp->link = rear;
}
else
{
temp->link = front;
rear->link = temp;
rear = temp;
}
}
void dequeue()
{
struct node* temp = front;
if(front == NULL && rear == NULL)
{
printf("the queue is empty\n");
}
if(front == rear)
{
front= NULL;
rear = NULL;
free(temp);
}
else
{
//struct node* temp = front;
rear->link = temp->link;
front = temp->link;
free(temp);
}
}
int isempty()
{
if(front == NULL && rear == NULL)
{
return 1;
}
else
{
return 0;
}
}
void display()
{
struct node* temp = front;
if(front == NULL && rear == NULL)
{
printf("the list is empty\n");
}
else
{
printf("queue:");
while(temp->link !=front)
{
printf("%d ",temp->data);
temp =temp->link;
}
printf("%d\n",temp->data);
}
}
void peek()
{
printf("the latest element to enter the queue is %d\n",rear->data);
}
// WORKS FINE.