Skip to content

Commit 9dceb65

Browse files
Add files via upload
1 parent a109a76 commit 9dceb65

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

hw3_linked_list_queue.c

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
typedef struct circleQueue
5+
{
6+
char item;
7+
struct circleQueue *next;
8+
} cir_Queue;
9+
10+
void push(cir_Queue** control);
11+
void pop(cir_Queue** popper);
12+
13+
int main()
14+
{
15+
cir_Queue* master = NULL;
16+
char action = '\0';
17+
do
18+
{
19+
printf("What Do you want to do? \
20+
\n\r [i for insert, r for remove, q for quit] ");
21+
scanf(" %c", &action);
22+
if (action == 'i')
23+
push(&master);
24+
else if (action == 'r')
25+
pop(&master);
26+
else if (action == 'q')
27+
printf("Bye. \n");
28+
else
29+
printf("That's not a valid input. \n");
30+
}
31+
while (action != 'q');
32+
return 0;
33+
}
34+
35+
void push(cir_Queue** control)
36+
{
37+
cir_Queue* newNode = NULL;
38+
newNode = malloc(sizeof(cir_Queue));
39+
printf("Enter character to be inserted: ");
40+
scanf(" %c", &(newNode->item));
41+
if (*control == NULL)
42+
{
43+
(*control) = newNode;
44+
(*control)->next = (*control);
45+
}
46+
else
47+
{
48+
newNode->next = (*control)->next;
49+
(*control)->next = newNode;
50+
(*control) = newNode;
51+
}
52+
}
53+
54+
void pop(cir_Queue** popper)
55+
{
56+
cir_Queue* sendToVoid = NULL;
57+
if (*popper == NULL)
58+
printf("Cannot remove, list is empty.\n");
59+
else if ((*popper)->next == *popper)
60+
{
61+
printf("Removing %c from list. \n", (*popper)->next->item);
62+
sendToVoid = *popper;
63+
*popper = NULL;
64+
free(sendToVoid);
65+
}
66+
else
67+
{
68+
printf("Removing %c from the list. \n", (*popper)->next->item);
69+
sendToVoid = (*popper)->next;
70+
(*popper)->next = sendToVoid->next;
71+
free(sendToVoid);
72+
}
73+
}

0 commit comments

Comments
 (0)