-
Notifications
You must be signed in to change notification settings - Fork 22
/
Queue_ll.c
113 lines (102 loc) · 1.76 KB
/
Queue_ll.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
#include <stdio.h>
#include <stdlib.h>
typedef struct queue
{
int data;
struct queue *next;
} queue;
queue *front = NULL;
queue *rear = NULL;
void enQueue()
{
queue *temp = (queue *)malloc(sizeof(queue));
if (temp == NULL)
{
printf("\nQUEUE OVERFLOW");
return;
}
scanf("%d", &temp->data);
temp->next = NULL;
if (front == NULL && rear == NULL)
{
front = rear = temp;
rear->next = front;
return;
}
rear->next = temp;
rear = temp;
rear->next = front;
return;
}
void deQueue()
{
queue *ptr = front;
if (front == NULL && rear == NULL)
{
printf("\nQUEUE UNDERFLOW");
return;
}
else if (front == rear)
{
printf("Deleted data: %d", front->data);
front = rear = NULL;
free(ptr);
return;
}
printf("Deleted data: %d", front->data);
front = front->next;
rear->next = front;
free(ptr);
return;
}
void print(queue *ptr)
{
if (rear == NULL)
{
printf("\nList is empty");
return;
}
else
{
ptr = front;
printf("\nThe List elements are:\n");
do
{
printf("%d ", ptr->data);
ptr = ptr->next;
} while(ptr != front);
}
}
int main()
{
int n;
char choice1 = 'y';
char choice2 = 'y';
printf("Enter the range: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
enQueue();
print(front);
while (choice1 == 'y' || choice2 == 'y')
{
printf("\nDo you want to pop out an element? y/n: ");
fflush(stdin);
scanf("%c", &choice1);
if (choice1 == 'y')
{
deQueue();
print(front);
}
printf("\nDo you want to push an element? y/n: ");
fflush(stdin);
scanf("%c", &choice2);
if (choice2 == 'y')
{
printf("\nEnter data: ");
enQueue();
print(front);
}
}
return 0;
}