-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueLinkedList.c
139 lines (133 loc) · 2.58 KB
/
QueueLinkedList.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
//Linked List
//final
#include<stdio.h>
#include<stdlib.h>
struct Queue{
int data;
struct Queue *next;
};
struct Queue *head = NULL;
void EnQueue(void);
int QueueSize(void);
void displayList(void);
void DeQueue(void);
void Front(void);
int IsEmpty(void);
int main()
{
int ch;
while(1)
{
printf("Enter Your Choice:\n");
printf("1 to insert element\n2 to delete Element\n3 to find size of queue\n4 to check if queue is empty or full\n5 to print front element\n6 to Display elements\n7 to exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:EnQueue();
break;
case 2:DeQueue();
break;
case 3:printf("%d\n",QueueSize());
break;
case 4:printf("%d",IsEmpty());
printf("\n");
break;
case 5:Front();
break;
case 6:displayList();
break;
case 7:exit(1);
break;
default:printf("Invalid Option, try again:\n");
break;
}
}
}
void EnQueue()
{
struct Queue *temp = (struct node*)malloc(sizeof(struct Queue));
printf("Enter data :\n");
scanf("%d",&temp -> data);
temp -> next = NULL;
if(head == NULL)
{
head = temp;
}
else
{
struct Queue *p = head;
while (p -> next != NULL)
{
p = p -> next;
}
p -> next = temp;
}
printf("Data %d is successfully inserted\n",temp->data);
}
int QueueSize()
{
int count = 0;
struct Queue *temp = head;
if (head == NULL)
{
printf("List is Empty:\n");
}
else
{
while (temp != NULL)
{
temp = temp -> next;
count++;
}
}
return count;
}
void displayList()
{
struct Queue *temp = head;
int length = QueueSize();
if(temp == NULL){
printf("List is Empty:\n\n");
}
else
{
while(length--)
{
printf("%d ",temp -> data);
temp = temp -> next;
}
printf("\n\n");
}
}
void DeQueue()
{
if (head == NULL)
{
printf("List is Empty\n\n");
}
else
{
struct Queue *temp = head;
head = temp -> next;
temp -> next = NULL;
free(temp);
}
}
int IsEmpty()
{
if (head == NULL)
{
return 1;
}
else
{
return 0;
}
}
void Front()
{
if (!IsEmpty())
printf("Front element is %d \n",head -> data);
else
printf("Queue is Empty\n");
}