forked from bhaveshkalra/DSA-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue using linked list.c
More file actions
121 lines (114 loc) · 1.76 KB
/
queue using linked list.c
File metadata and controls
121 lines (114 loc) · 1.76 KB
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*front = NULL, *rear = NULL;
int isEmpty()
{
if(front == NULL)
{
return 1;
}
else
{
return 0;
}
}
void insert(int item)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if(temp == NULL)
{
printf("Queue is not Allocated\n");
return;
}
temp->data = item;
temp->link = NULL;
if(front == NULL)
{
front = temp;
}
else
{
rear->link = temp;
}
rear = temp;
}
int delete_node()
{
struct node *temp;
int item;
if(isEmpty())
{
printf("Queue is Empty\n");
exit(1);
}
temp = front;
item = temp->data;
front = front->link;
free(temp);
return item;
}
int front()
{
if(isEmpty())
{
printf("Queue is Empty\n");
exit(1);
}
return front->data;
}
void display()
{
struct node *ptr;
if(isEmpty())
{
printf("Queue is Empty\n");
return;
}
printf("Queue Elements (or Nodes):\n");
for(ptr = front; ptr != NULL; ptr = ptr->link)
{
printf("%d ", ptr->data);
}
printf("\n\n");
}
int main()
{
int option, item;
while(1)
{
printf("1. Insert an Element in the Queue\n");
printf("2. Delete an Element from the Queue\n");
printf("3. Display Element at the Front position\n");
printf("4. Display All Elements of the queue\n");
printf("5. Exit\n");
printf("Enter your option:\t");
scanf("%d", &option);
switch(option)
{
case 1:
printf("Enter the Element to Add in Queue:\t");
scanf("%d", &item);
insert(item);
break;
case 2:
printf("The Deleted Element from the Queue:\t%d\n", delete_node());
break;
case 3:
printf("Element at the Front:\t%d\n", front());
break;
case 4:
display();
break;
case 5:
exit(1);
default:
printf("Wrong option\n");
}
}
return 0;
}