-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQLL.C
102 lines (102 loc) · 2.14 KB
/
QLL.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
#include <stdio.h>
#include <stdlib.h>
struct NODE
{
int data;
struct NODE *next;
};
struct NODE *enQ(struct NODE *head, int n)
{
struct NODE *new_node = (struct NODE *)malloc(sizeof(struct NODE));
if (new_node == NULL)
{
printf("\nThe queue is full!");
return head;
}
else
{
new_node->data = n;
if (head == NULL)
{
new_node->next = NULL;
head = new_node;
return head;
}
else
{
struct NODE *tra = head;
while (tra->next != NULL)
{
tra = tra->next;
}
tra->next = new_node;
new_node->next = NULL;
return head;
}
}
}
struct NODE *deQ(struct NODE *head)
{
if (head == NULL)
{
printf("\nThe Queue is empty!");
return head;
}
else
{
struct NODE *temp = head;
head = temp->next;
temp->next = NULL;
free(temp);
temp = NULL;
return head;
}
}
void printlist(struct NODE *head)
{
struct NODE *h = head;
printf("\n");
if (head == NULL)
{
printf("The list is empty!");
}
else
{
while (h != NULL)
{
printf("%d\t", h->data);
h = h->next;
}
}
}
int main()
{
struct NODE *head = NULL;
int num, opt;
printf("--------Perform Operations on Queue now----------");
do
{
printf("\nWhich operation do you wish to perform? Enter option number:\n1. Add element to Queue\n2. Delete element from Queue\t");
scanf("%d", &opt);
switch (opt)
{
case 1:
{
printf("\nEnter element:\t");
scanf("%d", &num);
head = enQ(head, num);
break;
}
case 2:
{
head = deQ(head);
break;
}
default:
break;
}
} while (opt != 0);
printf("\nThe existing element in the Queue are:");
printlist(head);
return 0;
}