-
Notifications
You must be signed in to change notification settings - Fork 0
/
BCQUEUE.c
166 lines (143 loc) · 3.32 KB
/
BCQUEUE.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MAX_SIZE_QUEUE 1000
#define TRUE 1
#define FALSE 0
#define OPERATION_GET_SIZE 1
#define OPERATION_CHECK_EMPTY 2
#define OPERATION_ENQUEUE 3
#define OPERATION_DEQUEUE 4
#define OPERATION_GET_FRONT_ITEM 5
#define OPERATION_GET_BACK_ITEM 6
#define EMPTY_QUEUE -1
typedef struct node
{
int item;
struct node *next;
} node;
typedef struct queue
{
int size;
node *front;
node *back;
} queue;
void initQueue(queue *workingQueue);
int getSize(queue *workingQueue);
bool isEmpty(queue *workingQueue);
void enqueue(queue *workingQueue, int newItem);
void dequeue(queue *workingQueue);
int getFront(queue *workingQueue);
int getBack(queue *workingQueue);
int main()
{
int numberOfTestCases;
scanf("%i", &numberOfTestCases);
queue *workingQueue = malloc(sizeof(queue));
initQueue(workingQueue);
while (numberOfTestCases--)
{
int operation;
scanf("%i", &operation);
if (operation == OPERATION_GET_SIZE)
{
printf("%i\n", getSize(workingQueue));
}
if (operation == OPERATION_CHECK_EMPTY)
{
if (isEmpty(workingQueue))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
if (operation == OPERATION_ENQUEUE)
{
int newItem;
scanf("%i", &newItem);
enqueue(workingQueue, newItem);
}
if (operation == OPERATION_DEQUEUE)
{
if (!isEmpty(workingQueue))
{
dequeue(workingQueue);
}
}
if (operation == OPERATION_GET_FRONT_ITEM)
{
if (!isEmpty(workingQueue))
{
printf("%i\n", getFront(workingQueue));
}
else
{
printf("%i\n", EMPTY_QUEUE);
}
}
if (operation == OPERATION_GET_BACK_ITEM)
{
if (!isEmpty(workingQueue))
{
printf("%i\n", getBack(workingQueue));
}
else
{
printf("%i\n", EMPTY_QUEUE);
}
}
}
free(workingQueue);
}
void initQueue(queue *workingQueue)
{
workingQueue->size = 0;
workingQueue->front = NULL;
workingQueue->back = NULL;
}
int getSize(queue *workingQueue)
{
return workingQueue->size;
}
bool isEmpty(queue *workingQueue)
{
return (workingQueue->size == 0);
}
void enqueue(queue *workingQueue, int newItem)
{
node *newNode = malloc(sizeof(node));
newNode->item = newItem;
newNode->next = NULL;
if (isEmpty(workingQueue))
{
workingQueue->front = newNode;
workingQueue->back = newNode;
}
else
{
workingQueue->back->next = newNode;
workingQueue->back = workingQueue->back->next;
}
(workingQueue->size)++;
}
void dequeue(queue *workingQueue)
{
if (!isEmpty(workingQueue))
{
(workingQueue->size)--;
node *currentFrontNode = workingQueue->front;
workingQueue->front = workingQueue->front->next;
free(currentFrontNode);
}
}
int getFront(queue *workingQueue)
{
return workingQueue->front->item;
}
int getBack(queue *workingQueue)
{
return workingQueue->back->item;
}