-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2.c
328 lines (265 loc) · 8.28 KB
/
p2.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//////////////////////////////////////////////////////////////////////////////////
/* CE1007/CZ1007 Data Structures
2016/17 S1
Author and Lab Group: Chee Jun Yuan Glenn FSP7
Program name: FSP7_Chee Jun Yuan Glenn
Date: 09 November 2016
Purpose: Implementing the required functions for Assignment 1 (Question 2)*/
//////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
//////////////////////////////////////////////////////////////////////////////////
typedef struct _listnode
{
int item;
struct _listnode *next;
} ListNode; // You should not change the definition of ListNode
typedef struct _linkedlist
{
int size;
ListNode *head;
} LinkedList; // You should not change the definition of LinkedList
//////////////////////// function prototypes /////////////////////////////////////
// This is for question 2. You should not change the prototype of this function
void moveOddItemsToBack(LinkedList *ll);
// You may use the following functions or you may write your own
int insertSortedLL(LinkedList *ll, int item);
void printList(LinkedList *ll);
void removeAllItems(LinkedList *ll);
ListNode * findNode(LinkedList *ll, int index);
int insertNode(LinkedList *ll, int index, int value);
int removeNode(LinkedList *ll, int index);
//////////////////////////// main() //////////////////////////////////////////////
int main()
{
LinkedList ll;
int c, i, j;
c = 1;
//Initialize the linked list 1 as an empty linked list
ll.head = NULL;
ll.size = 0;
printf("1: Insert an integer to the sorted linked list:\n");
printf("2: Moves all odd integers to the back of the linked list:\n");
printf("0: Quit:\n");
while (c != 0)
{
printf("Please input your choice(1/2/3/0): ");
scanf("%d", &c);
switch (c)
{
case 1:
printf("Input an integer that you want to add to the linked list: ");
scanf("%d", &i);
j = insertSortedLL(&ll, i); // You may use question1 function or code this function
printf("The resulting Linked List is: ");
printList(&ll);
break;
case 2:
moveOddItemsToBack(&ll); // You need to code this function
printf("The resulting Linked List after moving odd integers to the back of the Linked List is: ");
printList(&ll);
removeAllItems(&ll);
break;
case 0:
removeAllItems(&ll);
break;
default:
printf("Choice unknown;\n");
break;
}
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////////
void moveOddItemsToBack(LinkedList *ll) {
/* add your code here */
//if the LinkedList is EMPTY, don't do anything
if (ll == NULL || ll[0].size < 1 || ll[0].head == NULL) {
return; //break out of function
}
//else, if the LinkedList is NOT empty
else {
ListNode *tempNode, *prevNode; //initialise ListNode *tempNode to traverse LinkedList, and ListNode *prevNode to store previousNode
int x = 0, y = 0; //initialise x (counter), and y (index)
//set tempNode to FirstNode
tempNode = ll[0].head;
prevNode = NULL;
///Inserting and Removing nodes
//traverse each element of the LinkedList only once, x (counter) < ll[0].size
while (x < ll[0].size && tempNode != NULL) {
//if item is an odd number
if (tempNode[0].item % 2 != 0) {
insertNode(ll, ll[0].size, tempNode[0].item); //insert currentNode.item to the back of the LinkedList (ll[0].size)
removeNode(ll, y); //remove currentNode from the LinkedList
tempNode = prevNode; //set tempNode to previousNode as we have removed the current node from the LinkedList
y--; //decrement index counter
}
prevNode = tempNode; //store address of previous node
tempNode = tempNode != NULL ? tempNode[0].next : ll[0].head; //traverse next node; if tempNode is NULL, set tempNode to FirstNode again, else traverse next node...
//increment counter(s)
x++; //counter
y++; //index
}
///Repointing LinkedList
//traverse each element of the LinkedList only once, x (counter) < ll[0].size
/*while (x < ll[0].size && tempNode != NULL) {
//if item is an odd number
if (tempNode[0].item % 2 != 0) {
//if currentNode is firstNode
if (prevNode == NULL) {
//repoint ll[0].head to nextNode
ll[0].head = tempNode[0].next;
}
//else if currentNode is not firstNode
else {
//repoint previousNode[0].next to nextNode
prevNode[0].next = tempNode[0].next != NULL ? tempNode[0].next : NULL;
}
//repoint lastNode to currentNode
findNode(ll, ll[0].size - 2)[0].next = tempNode;
//end lastNode's tail
tempNode[0].next = NULL;
//decrement index
y--;
}
prevNode = findNode(ll, y); //store address of previous node
tempNode = findNode(ll, y + 1); //traverse next node
//increment counter(s)
x++; //counter
y++; //index
}*/
}
}
int insertSortedLL(LinkedList *ll, int item) {
/* add your code here (You may use question1 function or code this function) */
//if the LinkedList is EMPTY, simply insert item to the first index (0)
if (ll == NULL || ll[0].size < 1 || ll[0].head == NULL) {
insertNode(ll, 0, item);
return 0; //return index
}
//else, if the LinkedList is NOT empty
else {
ListNode *tempNode; //initialise ListNode *tempNode to traverse LinkedList
int x = 0; //initialise counter
//set tempNode to FirstNode
tempNode = ll[0].head;
//traverse the LinkedList
while (tempNode != NULL) {
//if item is less than currentNode.item
if (item < tempNode[0].item) {
insertNode(ll, x, item); //insert item to LinkedList just before the currentNode
return x;
}
//else if item equals to currentNode.item
else if (item == tempNode[0].item) {
return -1; //return invalid index
}
//else if item is greater than currentNode.item
else {
//if currentNode is LastNode OR if item is greater than currentNode.item AND lesser than nextNode.item
if (tempNode[0].next == NULL || item < tempNode[0].next[0].item) {
insertNode(ll, x + 1, item); //simply insert item to LinkedList just after the currentNode
return x + 1;
}
//do not insert if item is greater than tempNode.item AND nextNode.item
//else {}
}
//traverse next node and increment counter
tempNode = tempNode[0].next;
x++;
}
}
}
//////////////////////////////////////////////////////////////////////////////////
void printList(LinkedList *ll) {
ListNode *cur;
if (ll == NULL)
return;
cur = ll->head;
if (cur == NULL)
printf("Empty");
while (cur != NULL)
{
printf("%d ", cur->item);
cur = cur->next;
}
printf("\n");
}
void removeAllItems(LinkedList *ll)
{
ListNode *cur = ll->head;
ListNode *tmp;
while (cur != NULL) {
tmp = cur->next;
free(cur);
cur = tmp;
}
ll->head = NULL;
ll->size = 0;
}
ListNode * findNode(LinkedList *ll, int index) {
ListNode *temp;
if (ll == NULL || index < 0 || index >= ll->size)
return NULL;
temp = ll->head;
if (temp == NULL || index < 0)
return NULL;
while (index > 0) {
temp = temp->next;
if (temp == NULL)
return NULL;
index--;
}
return temp;
}
int insertNode(LinkedList *ll, int index, int value) {
ListNode *pre, *cur;
if (ll == NULL || index < 0 || index > ll->size + 1)
return -1;
// If empty list or inserting first node, need to update head pointer
if (ll->head == NULL || index == 0) {
cur = ll->head;
ll->head = malloc(sizeof(ListNode));
ll->head->item = value;
ll->head->next = cur;
ll->size++;
return 0;
}
// Find the nodes before and at the target position
// Create a new node and reconnect the links
if ((pre = findNode(ll, index - 1)) != NULL) {
cur = pre->next;
pre->next = malloc(sizeof(ListNode));
pre->next->item = value;
pre->next->next = cur;
ll->size++;
return 0;
}
return -1;
}
int removeNode(LinkedList *ll, int index) {
ListNode *pre, *cur;
// Highest index we can remove is size-1
if (ll == NULL || index < 0 || index >= ll->size)
return -1;
// If removing first node, need to update head pointer
if (index == 0) {
cur = ll->head->next;
free(ll->head);
ll->head = cur;
ll->size--;
return 0;
}
// Find the nodes before and after the target position
// Free the target node and reconnect the links
if ((pre = findNode(ll, index - 1)) != NULL) {
if (pre->next == NULL)
return -1;
cur = pre->next;
pre->next = cur->next;
free(cur);
ll->size--;
return 0;
}
return -1;
}