-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
82 lines (66 loc) · 1010 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
struct queue *front = NULL, *rear = NULL;
int enqueue(int a)
{
struct queue *new_queue = NULL;
new_queue = (struct queue *) malloc(sizeof(struct queue));
if (new_queue)
{
new_queue->next = NULL;
new_queue->var = a;
if (rear != NULL)
rear->next = new_queue;
rear = new_queue;
if (front == NULL)
front = new_queue;
return 0;
}
else
return 1;
}
int dequeue()
{
struct queue *temp = NULL;
int ret = 0;
ret = IsEmpty();
if (ret)
return -1;
else
{
temp = front;
ret = temp->var;
if ( front->next != NULL)
front = front->next;
else
{
front = NULL;
rear = NULL;
}
free(temp);
return ret;
}
}
int IsEmpty()
{
if (front == NULL && rear == NULL)
return 1;
else
return 0;
}
void show()
{
struct queue *temp = NULL;
if ( !IsEmpty())
{
while (front)
{
temp = front;
front = front->next;
printf("data = %d\t", temp->var);
}
}
else
printf("Queue is empty\n");
}