-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
172 lines (141 loc) · 4.58 KB
/
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
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
167
168
169
170
171
172
/*
@Author: Arshad Shah
@Date: Tuesday, October 20, 2020 11:23 AM
@Email: [email protected]
@Filename: queue.c
@Last modified by: Arshad Shah
@Last modified time: Tuesday, October 20, 2020 11:24 AM
*/
// Fig. 12.13: fig12_13.c
// Operating and maintaining a queue
#include <stdio.h>
#include <stdlib.h>
// self-referential structure
struct queueNode {
char data; // define data as a char
struct queueNode *nextPtr; // queueNode pointer
};
typedef struct queueNode QueueNode;
// function prototypes
void printQueue(QueueNode* currentPtr);
int isEmpty(QueueNode* headPtr);
char dequeue(QueueNode* *headPtr, QueueNode* *tailPtr);
void enqueue(QueueNode* *headPtr, QueueNode* *tailPtr, char value);
void instructions(void);
// function main begins program execution
int main(void)
{
QueueNode* headPtr = NULL; // initialize headPtr
QueueNode* tailPtr = NULL; // initialize tailPtr
char item; // char input by user
instructions(); // display the menu
printf("%s", "? ");
unsigned int choice; // user's menu choice
scanf("%u", &choice);
// while user does not enter 3
while (choice != 3) {
switch(choice) {
// enqueue value
case 1:
printf("%s", "Enter a character: ");
scanf("\n%c", &item);
enqueue(&headPtr, &tailPtr, item);
printQueue(headPtr);
break;
// dequeue value
case 2:
// if queue is not empty
if (headPtr != NULL)) {
item = dequeue(&headPtr, &tailPtr);
printf("%c has been dequeued.\n", item);
}
printQueue(headPtr);
break;
default:
puts("Invalid choice.\n");
instructions();
break;
} // end switch
printf("%s", "? ");
scanf("%u", &choice);
}
puts("End of run.");
}
// display program instructions to user
void instructions(void)
{
printf ("Enter your choice:\n"
" 1 to add an item to the queue\n"
" 2 to remove an item from the queue\n"
" 3 to end\n");
}
// insert a node at queue tail
void enqueue(QueueNode* *hPtr, QueueNode* *tPtr, char value)
{
QueueNode* newPtr;
newPtr= malloc(sizeof(QueueNode));
if (newPtr != NULL) { // is space available
newPtr->data = value;
newPtr->nextPtr = NULL;
// if empty, insert node at head
if (isEmpty(*hPtr)) {
*hPtr = newPtr;
}
else {
(*tPtr)->nextPtr = newPtr;
}
*tPtr = newPtr;
}
else {
printf("%c not inserted. No memory available.\n", value);
}
}
// remove node from queue head
char dequeue(QueueNode* *hPtr, QueueNode* *tPtr)
{
char value = (*hPtr)->data;
QueueNode* tempPtr = *hPtr;
*hPtr = (*hPtr)->nextPtr;
// if queue is empty
if (*hPtr == NULL) {
*tPtr = NULL;
}
free(tempPtr);
return value;
}
// return 1 if the queue is empty, 0 otherwise
int isEmpty(QueueNode* headPtr)
{
return headPtr == NULL;
}
// print the queue
void printQueue(QueueNode* currentPtr)
{
// if queue is empty
if (currentPtr == NULL) {
puts("Queue is empty.\n");
}
else {
puts("The queue is:");
// while not end of queue
while (currentPtr != NULL) {
printf("%c --> ", currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
puts("NULL\n");
}
}
/**************************************************************************
* (C) Copyright 1992-2015 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/