-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
97 lines (80 loc) · 1.42 KB
/
queue.c
File metadata and controls
97 lines (80 loc) · 1.42 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
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"queue.h"
static void CopyToNode(Item item, Node* pn);
static void CopyToItem(Node* pi,Item * item );
void InitializeQueue( Queue * pq)
{
pq->front = NULL;
pq->rear = NULL;
pq->items = 0;
}
bool QueueIsFull(const Queue* pq)
{
return(pq->items == MAXQUEUE);
}
bool QueueIsEmpty(const Queue* pq)
{
return(pq->items == 0);
}
int QueueItemCount(const Queue* pq)
{
return pq->items;
}
bool EnQueue(Item item,Queue* pq)
{
Node* pnew;
if (QueueIsFull(pq))
return false;
pnew = (Node*)malloc(sizeof(Node));
if (pnew == NULL)
{
fprintf(stderr, "Unable to allocate memory!\n");
exit(1);
}
CopyToNode(item, pnew);
pnew->next = NULL;
if (QueueIsEmpty(pq))
{
pq->front = pnew;
}
else
{
pq->rear->next = pnew;
}
pq->rear = pnew;
pq->items++;
return true;
}
bool DeQueue(Item* pitem, Queue* pq)
{
Node* pt;
if (QueueIsEmpty(pq))
return false;
CopyToItem(pq->front, pitem);
pt = pq->front;
pq->front = pq->front->next;
free(pt);
pq->items--;
if (pq->items == 0)
{
pq->rear = NULL;
}
return true;
}
void EmptyTheQueue(Queue* pq)
{
Item dummy;
while (!QueueIsEmpty(pq))
{
DeQueue(&dummy, pq);
}
}
static void CopyToNode(Item item, Node* pn) {
pn->item = item;
}
static void CopyToItem(Node* pn, Item* pi)
{
*pi = pn->item;
}