0% found this document useful (0 votes)
19 views

07_Queue

Uploaded by

Destiny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

07_Queue

Uploaded by

Destiny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

...\source\repos\DynamicDataTypes\DynamicDataTypes\Queue.

c 1
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 typedef struct queueNode { /* self-referential structure */
5 char data;
6 struct queueNode* nextPtr;
7 } QueueNode_t;
8
9 /* function prototypes */
10 void printQueue(QueueNode_t*);
11 int isEmpty(QueueNode_t*);
12 char dequeue(QueueNode_t**, QueueNode_t**);
13 void enqueue(QueueNode_t**, QueueNode_t**, char);
14 void instructions(void);
15
16 int main()
17 {
18 QueueNode_t *headPtr = NULL, *tailPtr = NULL;
19 int choice;
20 char item;
21
22 instructions();
23 printf("? ");
24 scanf("%d", &choice);
25
26 while (choice != 3) {
27
28 switch (choice) {
29
30 case 1:
31 printf("Enter a character: ");
32 scanf("\n%c", &item);
33 enqueue(&headPtr, &tailPtr, item);
34 printQueue(headPtr);
35 break;
36 case 2:
37 if (!isEmpty(headPtr)) {
38 item = dequeue(&headPtr, &tailPtr);
39 printf("%c has been dequeued.\n" , item);
40 }
41
42 printQueue(headPtr);
43 break;
44
45 default:
46 printf("Invalid choice.\n\n");
47 instructions();
48 break;
49 }
50
51 printf("? ");
52 scanf("%d", &choice);
53 }
...\source\repos\DynamicDataTypes\DynamicDataTypes\Queue.c 2
54
55 printf("End of run.\n");
56 return 0;
57 }
58
59 void instructions(void)
60 {
61 printf("Enter your choice:\n"
62 " 1 to add an item to the queue\n"
63 " 2 to remove an item from the queue\n"
64 " 3 to end\n");
65 }
66
67 void enqueue(QueueNode_t** headPtr, QueueNode_t** tailPtr, char value)
68 {
69 QueueNode_t* newPtr;
70
71 newPtr = malloc(sizeof(QueueNode_t));
72
73 if (newPtr != NULL) {
74 newPtr->data = value;
75 newPtr->nextPtr = NULL;
76
77 if (isEmpty(*headPtr))
78 *headPtr = newPtr;
79 else
80 (*tailPtr)->nextPtr = newPtr;
81
82 *tailPtr = newPtr;
83 }
84 else
85 printf("%c not inserted. No memory available.\n" ,
86 value);
87 }
88
89 char dequeue(QueueNode_t** headPtr, QueueNode_t** tailPtr)
90 {
91 char value;
92 QueueNode_t* tempPtr;
93
94 value = (*headPtr)->data;
95 tempPtr = *headPtr;
96 *headPtr = (*headPtr)->nextPtr;
97
98 if (*headPtr == NULL)
99 *tailPtr = NULL;
100
101 free(tempPtr);
102 return value;
103 }
104
105 int isEmpty(QueueNode_t* headPtr)
106 {
...\source\repos\DynamicDataTypes\DynamicDataTypes\Queue.c 3
107 return headPtr == NULL;
108 }
109
110 void printQueue(QueueNode_t* currentPtr)
111 {
112 if (currentPtr == NULL)
113 printf("Queue is empty.\n\n");
114 else {
115 printf("The queue is:\n");
116
117 while (currentPtr != NULL) {
118 printf("%c --> ", currentPtr->data);
119 currentPtr = currentPtr->nextPtr;
120 }
121
122 printf("NULL\n\n");
123 }
124 }

You might also like