-
Notifications
You must be signed in to change notification settings - Fork 2
/
Queue.c
98 lines (94 loc) · 1.9 KB
/
Queue.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
//dynamic implementation of queue
#include <stdio.h>
#include <stdlib.h>
void insert();
void delete ();
void display();
struct node
{
int data;
struct node *next;
};
struct node *front = NULL;
struct node *rear = NULL;
int main()
{
int choice;
while (1)
{
printf("\n 1.INSERT");
printf("\n 2.DELETE");
printf("\n 3.DISPLAY");
printf("\n 4.EXIT");
printf("\n ENTER YOUR CHOICE : ");
scanf("%d",&choice);
fflush(stdin);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("no such option \n");
break;
}
}
}
void insert()
{
struct node *ptr = (struct node*)malloc(sizeof(struct node));
printf("\nENTER THE DATA : ");
scanf("%d", &ptr->data);
ptr->next = NULL;
if (front == NULL && rear == NULL)
{
front = ptr;
rear = ptr;
}
else
{
rear->next = ptr;
rear = ptr;
}
}
void delete ()
{
struct node *ptr = front;
if (front == NULL && rear == NULL)
printf("queue underflow \n");
else if (front == rear)
front = rear = NULL;
else
front = front->next;
if (ptr !=NULL)
{
printf("deleted element is %d ", ptr->data);
}
free(ptr);
}
void display()
{
struct node *ptr = front;
if (front == NULL && rear == NULL)
printf("QUEUE is empty\n");
else
{
printf("\n---------------------------------\n");
printf("\tprinting stack elements : \n");
while (ptr != NULL)
{
printf(" %d \n", ptr->data);
ptr = ptr->next;
}
printf("\n---------------------------------\n");
}
}