-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq.h
117 lines (103 loc) · 3.2 KB
/
q.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Body files including this header must also use the following includes:
* #include <stdbool.h>
* #include <stdlib.h>
* #include <stdio.h>
*
* Body files including this header must be compiled using C99. Add -std=c99 to gcc options
*/
#ifndef CSE330P1_Q_H
#define CSE330P1_Q_H
#include "tcb.h"
//This function is called when a system call fails. It displays a message about the error on stderr
//(#include <stdio.h>in test cases and other body files)
void error(const char *msg)
{
//print error message
perror(msg);
//teminate the program (not sure if needed)
//exit(0);
}
//tests if queue pointed to by head is empty. Returns true/false. #include <stdbool.h> in test cases and other body files. Compile in C99 mode.
bool isEmpty(struct TCB_t* head)
{
//create boolean flag to return with status of queue
bool isEmpty = false;
//if head->next AND head->prev both point to head, the queue is empty.
if(head->next == head
&& head->prev == head)
{
isEmpty = true;
}
//return isEmpty
return isEmpty;
}
// returns a pointer to a new q-element, uses memory allocation
struct TCB_t* NewItem()
{
//create item variable
struct TCB_t* new_item;
//malloc item (#include <stdlib.h> in test cases and other body files)
new_item = malloc(sizeof(struct TCB_t));
//return item
return new_item;
}
// creates a empty queue, consisting of one dummy element, returns the head pointer.
struct TCB_t* newQueue()
{
//create head variable
struct TCB_t* head;
//malloc head (#include <stdlib.h> in test cases and other body files)
head = malloc(sizeof(struct TCB_t));
//make head->next point at head
head->next = head;
//make head->prev point at head
head->prev = head;
//return head
return head;
}
// adds a queue item, pointed to by “item”, to the queue pointed to by head.
void AddQueue(struct TCB_t* head, struct TCB_t* new_item)
{
/*
* h -> h ; h <- h
* h -> new1 -> h; new1 <- h <- new1
* h -> new1 -> new2 -> h; new2 <- h <- new1 <- new2
*/
//create pointer at end of list
struct TCB_t* last = head->prev;
//make last element->next point to new_item
last->next = new_item;
//make new_item->prev point to last element
new_item->prev = last;
//make new_item->next point to head
new_item->next = head;
//make heads prev item new_item
head->prev = new_item;
}
// deletes first item in queue and returns a pointer to the deleted item. If the queue is already empty, flag error.
// Returns NULL if queue is empty
struct TCB_t* DelQueue(struct TCB_t* head)
{
//create variable that will point to popped item
struct TCB_t* popped_item = NULL;
//if head is empty, print error
if(isEmpty(head))
{
error("ERROR: Empty queue.");
}
//else, pop the first element
else
{
//make popped_item point to the first element in the queue
popped_item = head->next;
//Advance second element in queue to first position
head->next = head->next->next;
//link back new first element to head
head->next->prev = head;
}
//end if
//return popped_item
return popped_item;
}
#endif //CSE330P1_Q_H