-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw3_linked_list_queue.c
73 lines (68 loc) · 1.64 KB
/
hw3_linked_list_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
#include <stdio.h>
#include <stdlib.h>
typedef struct circleQueue
{
char item;
struct circleQueue *next;
} cir_Queue;
void push(cir_Queue** control);
void pop(cir_Queue** popper);
int main()
{
cir_Queue* master = NULL;
char action = '\0';
do
{
printf("What Do you want to do? \
\n\r [i for insert, r for remove, q for quit] ");
scanf(" %c", &action);
if (action == 'i')
push(&master);
else if (action == 'r')
pop(&master);
else if (action == 'q')
printf("Bye. \n");
else
printf("That's not a valid input. \n");
}
while (action != 'q');
return 0;
}
void push(cir_Queue** control)
{
cir_Queue* newNode = NULL;
newNode = malloc(sizeof(cir_Queue));
printf("Enter character to be inserted: ");
scanf(" %c", &(newNode->item));
if (*control == NULL)
{
(*control) = newNode;
(*control)->next = (*control);
}
else
{
newNode->next = (*control)->next;
(*control)->next = newNode;
(*control) = newNode;
}
}
void pop(cir_Queue** popper)
{
cir_Queue* sendToVoid = NULL;
if (*popper == NULL)
printf("Cannot remove, list is empty.\n");
else if ((*popper)->next == *popper)
{
printf("Removing %c from list. \n", (*popper)->next->item);
sendToVoid = *popper;
*popper = NULL;
free(sendToVoid);
}
else
{
printf("Removing %c from the list. \n", (*popper)->next->item);
sendToVoid = (*popper)->next;
(*popper)->next = sendToVoid->next;
free(sendToVoid);
}
}