-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13.queueUsingLL.c
111 lines (100 loc) · 1.55 KB
/
13.queueUsingLL.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
/*
Author : abhijithvijayan
Title : Queue using Linked List
*/
#include <stdio.h>
#include <stdlib.h>
int item;
void view();
struct Node
{
int data;
struct Node *link;
};
struct Node *front;
struct Node *rear;
int userInput()
{
printf("Enter the number:");
scanf("%d", &item);
return item;
}
void enQueue()
{
item = userInput();
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = item;
temp->link = NULL;
if (front == NULL && rear == NULL)
front = rear = temp;
else
{
rear->link = temp;
rear = temp;
}
view();
}
int deQueue()
{
struct Node *newptr = front;
if (front == NULL && rear == NULL)
{
printf("The queue is already empty!! Underflow\n");
return 0;
}
else if (front == rear)
{
front = NULL;
rear = NULL;
}
else
{
front = front->link;
}
free(newptr);
view();
}
int isEmpty()
{
if (front == NULL && rear == NULL)
return 1;
return 0;
}
void view()
{
printf("\nThe Queue is\n");
if (!isEmpty())
{
struct Node *newptr = front;
while (newptr != NULL)
{
printf("%d ", newptr->data);
newptr = newptr->link;
}
printf("\n\n");
}
else
printf("----EMPTY----\n");
}
void main()
{
system("clear");
front = NULL;
rear = NULL;
int ch;
do
{
printf("\n---Operations Menu---\n");
printf("1.Insert\n2.Delete\n\nEnter your choice# ");
scanf("%d", &ch);
switch (ch)
{
case 1:
enQueue();
break;
case 2:
deQueue();
break;
}
} while (ch < 3);
}