-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue using linked list
152 lines (143 loc) · 2.89 KB
/
queue using linked list
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
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int value_info;
struct Node *ptr;
}*front,*rear,*temp,*front1;
int frontvalue();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
{
int no, ch, e;
printf("\n LIST OF CHOICES: \n");
printf("\t 1. Enqueue \n");
printf("\t 2. Dequeue \n");
printf("\t 3. Front \n");
printf("\t 4. Empty \n");
printf("\t 5. Exit \n");
printf("\t 6. Display \n\n");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf(" Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontvalue();
if (e != 0)
printf("\n Front element : %d \n", e);
else
printf("There is no front element \n");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
default:
printf("Invalid choice! \n");
break;
}
}
}
void create()
{
front = rear = NULL;
}
void queuesize()
{
printf("\n Size of the queue is %d", count);
}
void enq(int data)
{
if (rear == NULL)
{
rear = (struct Node *)malloc(1*sizeof(struct Node));
rear->ptr = NULL;
rear->value_info = data;
front = rear;
}
else
{
temp=(struct Node *)malloc(1*sizeof(struct Node));
rear->ptr = temp;
temp->value_info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
void display()
{
front1 = front;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
printf("%d ", front1->value_info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->value_info);
}
void deq()
{
front1 = front;
if (front1 == NULL)
{
printf("\n Cannot display elements from an empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequeued value is %d", front->value_info);
free(front);
front = front1;
}
else
{
printf("\n Dequeued value is %d", front->value_info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
int frontvalue()
{
if ((front != NULL) && (rear != NULL))
return(front->value_info);
else
return 0;
}
void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Empty queue!");
else
printf("\n Queue is not empty!");
}