-
Notifications
You must be signed in to change notification settings - Fork 0
/
queuell.c
84 lines (74 loc) · 1.04 KB
/
queuell.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
#include <stdio.h>
#include <stdlib.h>
struct link
{
int data;
struct link *next;
};
typedef struct link *node;
typedef struct
{
node front, rear;
}QUEUE;
int isEmpty(QUEUE *q)
{
if(q->rear == NULL)
return 1;
else
return 0;
}
void enqueue(QUEUE *q, int el)
{
node ptr= (node)malloc(sizeof(struct link));
ptr->data = el;
ptr->next = NULL;
if(q->rear == NULL)
{
q->front = ptr;
q->rear = ptr;
}
else
{
(q->rear)->next = ptr;
q->rear = ptr;
}
}
dequeue(QUEUE *q)
{
printf("\nElement dequeued - %d\n", q->front->data);
q->front = q->front->next;
}
void display(QUEUE *q)
{
node temp;
temp = q->front;
if(isEmpty(q))
{
printf("\nQueue is empty!!\n");
return;
}
printf("\nQueue - \n");
while(temp!=NULL)
{
printf("%d <- ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
QUEUE *q;
q = (QUEUE*)malloc(sizeof(QUEUE));
q->front =NULL;
q->rear = NULL;
enqueue(q, 1);
enqueue(q, 2);
enqueue(q, 3);
enqueue(q, 4);
display(q);
dequeue(q);
dequeue(q);
display(q);
enqueue(q, 5);
display(q);
}