-
Notifications
You must be signed in to change notification settings - Fork 0
/
queueLinkedlist.c
83 lines (83 loc) · 1.61 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
#include <stdio.h>
#include <stdlib.h>
struct queue
{
int data;
struct queue *next;
};
struct queue *front = 0, *rear = 0, *newnode, *ptr;
struct queue *enqueue(struct queue *front);
struct queue *dequeue(struct queue *front);
void display(struct queue *front);
int main()
{
int option;
printf("Choose from menu\n");
printf("1.enqueue\n");
printf("2.dequeue\n");
printf("3.display\n");
while (1)
{
printf("Choose option\n");
scanf("%d", &option);
switch (option)
{
case 1:
front = enqueue(front);
break;
case 2:
front = dequeue(front);
break;
case 3:
display(front);
break;
default:
printf("EXIT\n");
exit(1);
}
}
return 0;
}
struct queue *enqueue(struct queue *front)
{
newnode = (struct queue *)malloc(sizeof(struct queue));
printf("Enter data\n");
scanf("%d", &newnode->data);
newnode->next = 0;
if (front == 0)
{
front = rear = newnode;
}
else
{
rear->next = newnode;
rear = newnode;
}
return front;
}
struct queue *dequeue(struct queue *front)
{
// deletion from beg;
ptr = front;
front = front->next;
free(ptr);
printf("Element deleted\n");
return front;
}
void display(struct queue *front)
{
if (rear == 0)
{
printf("Queue is empty\n");
}
else
{
ptr = front;
while (ptr != 0)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}
}