-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlili_queue.c
108 lines (94 loc) · 1.26 KB
/
lili_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
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct queue
{
node *r;
node *f;
}queue;
void insert_queue(queue *,int);
int delete_queue(queue *);
int display(queue *);
int main()
{
queue *q;
int ch,n,a;
q=(queue*)malloc(sizeof(queue));
q->r=NULL;
q->f=NULL;
while(1)
{
printf("\n Enter your choice \n 1 for enqueue\n 2 for dequeue\n 3 for display \n 4 to exit the program\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Enter the number to be inserted : ");
scanf("%d",&n);
insert_queue(q,n);
break;
case 2:a=delete_queue(q);
if(a!=-1)
printf("Element removed is :- %d",a);
break;
case 3:display(q);
break;
case 4:return 0;
break;
default:printf("Invalid choice");
}
}
}
void insert_queue(queue *q,int m)
{
node *new;
new=(node*)malloc(sizeof(node));
new->data=m;
new->next=NULL;
if(q->r==NULL)
{
q->r=new;
q->f=new;
}
else
{
q->r->next=new;
q->r=new;
}
}
int delete_queue(queue *q)
{
node *new;
int t;
if(q->f==NULL)
{
printf("underflow");
return -1;
}
new=q->f;
q->f=q->f->next;
new->next=NULL;
t=new->data;
if(new==q->r)
q->r=NULL;
free(new);
return t;
}
int display(queue *q)
{
node *p;
p=q->f;
if(p==NULL)
{ printf("queue is empty");
return 0;
}
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
return 0;
}