-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrelab 9.c
152 lines (124 loc) · 2.86 KB
/
Prelab 9.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
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int key;
struct Node* next;
} Node;
//head.next is first element, tail.next is last element
typedef struct List {
int count;
Node* head;
Node* tail;
} List;
List* initIntegerList();
int insertAtHead(int, List*);
int insertAtTail(int, List*);
int removeHead(List*);
int getListSize(List*);
void printHead(List*);
void moveHeadToTail(List*);
void freeList(List** list);
void printList(List* list);
int main() {
List* list = initIntegerList();
if (list == NULL) {
printf("list not initialized\n");
return 1;
}
printf("Adding 20 random numbers:\n");
for (int i=0; i<10; i++) {
insertAtHead(rand() % 100, list);
insertAtTail(rand() % 100, list);
printList(list);
}
printf("Moving head to tail 5 times:\n");
for (int i=0; i<5; i++) {
moveHeadToTail(list);
printList(list);
}
printf("Removing head:\n");
removeHead(list);
printList(list);
freeList(&list);
return 0;
}
List* initIntegerList() {
List* newList = malloc(sizeof(List));
if(newList == NULL) {return NULL;}
Node* frontDummy =malloc(sizeof(Node));
Node* endDummy =malloc(sizeof(Node));
if(newList && frontDummy && endDummy) {
newList->count = 0;
frontDummy->next = endDummy;
endDummy->next = frontDummy;
newList->head = frontDummy;
newList->tail = endDummy;
return newList;
} else {
if (newList){free(newList);}
if (frontDummy) {free(frontDummy);}
if (endDummy) {free(endDummy);}
return NULL;
}
}
int insertAtHead(int k, List* list) {
Node* newNode = malloc(sizeof(Node));
if(newNode == NULL) {return 0;}
if(getListSize(list) == 0) {list->tail->next = newNode;}
newNode->key = k;
newNode->next = list->head->next;
list->head->next = newNode;
list->count ++;
return 0;
}
int insertAtTail(int k, List* list) {
Node* newNode = malloc(sizeof(Node));
if(newNode == NULL) {return 0;}
newNode->key = k;
newNode->next = NULL;
list->tail->next->next = newNode;
list->tail->next = newNode;
list->count ++;
return 0;
}
int removeHead(List* list) {
if(getListSize(list) < 1) {return 0;}
Node* node = list->head->next;
list->head->next = node->next;
list->count --;
free(node);
return 0;
}
int getListSize(List* list) {
return list->count;
}
void printHead(List* list) {
if(getListSize(list) == 0) {
return;
} else {
printf("%i\n", list->head->next->key);
}
}
void moveHeadToTail(List* list) {
Node* head = list->head->next;
list->head->next = head->next;
list->tail->next->next = head;
list->tail->next = head;
head->next = NULL;
}
void freeList(List** l) {
List* list = *l;
while(list->count > 0) {removeHead(list);}
free(list->tail);
free(list->head);
free(list);
*l = NULL;
}
void printList(List* list){
Node* current = list->head->next;
while (current) {
printf("%i, ", current->key);
current = current->next;
}
printf("\n");
}