-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_queue_singlell.c
More file actions
104 lines (92 loc) · 2.05 KB
/
linear_queue_singlell.c
File metadata and controls
104 lines (92 loc) · 2.05 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
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *next;
}node;
typedef struct list {
node *head;
int front;
int rear;
} list;
list* create_list() {
list *lst = (list*)malloc(sizeof(list));
lst->head = NULL;
lst->front = -1;
lst->rear = -1;
return lst;
}
void enqueue(list *lst, int ele) {
node *new_node = (node*)malloc(sizeof(node));
new_node->val = ele;
new_node->next = lst->head;
lst->head = new_node;
lst->rear++;
if(lst->front == 0) {
lst->front++;
}
}
int dequeue(list *lst) {
if((lst->front == -1) || (lst->front > lst->rear)) {
printf("Queue empty! canot dequeue\n");
return -1;
}
node *del = lst->head;
if (lst->head == NULL) {
return -1;
}
int ele = del->val;
lst->head = del->next;
free(del);
lst->front++;
if(lst->front == lst->rear+1)
{
lst->front = -1;
lst->rear =-1;
}
return ele;
}
void display(list *lst) {
node *curr = lst->head;
if(lst->head == NULL) {
printf("No items in list to display.");
return;
}
printf("Queue items : \n");
while(curr != NULL){
printf("%d\n",curr->val);
curr = curr->next;
}
}
int main()
{
list *lst = create_list();
int choice, del, ele;
for(;;)
{
printf("\n\nEnter choice :\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1 :
printf("Enter element to be enqueued : ");
scanf("%d",&ele);
enqueue(lst, ele);
break;
case 2 :
dequeue(lst);
break;
case 3 :
display(lst);
break;
case 4 :
exit(0);
default :
printf("Invalid choice\n");
}
}
}