-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueuelinkedlist.h
66 lines (60 loc) · 1.73 KB
/
queuelinkedlist.h
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
#define TRUE 1
#define FALSE 0
typedef struct queue
{
int info; // information of the node
struct queue *next; // address of the next node
}*NODEPTR;
// function prototypes
void initQueue(NODEPTR *); // to initialize queue
int enqueue(NODEPTR *,int); // to append an informtation to the queue
int dequeue(NODEPTR *,int *); // to serve an information out of the queue
// function definitions
// to initialize queue
void initQueue(NODEPTR *pQueue)
{
*pQueue = NULL;
}
// to append an informtation to the queue
int enqueue(NODEPTR *pQueue,int toAppend)
// Post-condition: *pQueue points to the so-called last node
{
NODEPTR newNode; // node to append
newNode = malloc(sizeof(struct queue)); // allocate space
if(newNode == NULL) // cannot allocate space for a new node
return FALSE;
newNode->info = toAppend;
if(*pQueue == NULL) // empty queue
newNode->next = newNode;
else if(*pQueue == (*pQueue)->next) // only one node
{
newNode->next = *pQueue;
(*pQueue)->next = newNode;
}
else // more than one nodes
{
newNode->next = (*pQueue)->next;
(*pQueue)->next = newNode;
}
*pQueue = newNode;
return TRUE;
}
// to serve an information out of the queue
int dequeue(NODEPTR *pQueue,int *served)
{
NODEPTR delNode; // node to serve
if(*pQueue == NULL) // empty queue
return FALSE;
if(*pQueue == (*pQueue)->next) // only one node in the queue
{
delNode = *pQueue;
initQueue(pQueue); // function call to initialize queue
}
else // more than one node in the queue
{
delNode = (*pQueue)->next;
(*pQueue)->next = delNode->next;
}
*served = delNode->info; // information that was served
free(delNode); // empty space for the served node
}