forked from apadin1/eecs373
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
79 lines (67 loc) · 2.25 KB
/
list.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
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "list.h"
// Takes a valid, sorted list starting at `head` and adds the element
// `new_element` to the list. The list is sorted based on the value of the
// `index` member in ascending order. Returns a pointer to the head of the list
// after the insertion of the new element.
list_t* insert_sorted(list_t* head, list_t* new_element) {
assert(head != NULL);
assert(new_element != NULL);
list_t *old_head = head;
// Iterate through the list until reaching the last element
while (head != NULL)
{
// If the very first element in the list is already greater
// than the ne being inserted, we must insert at the front
if (head->index > new_element->index)
{
new_element->next = head;
return new_element; // This becomes the new head
}
// If we have reached the end of the list without yet
// inserting, the index of new_element must be greater
// than the remaining elements, and we can insert it
// on the end.
else if (head->next == NULL)
{
head->next = new_element; // Insert new_element
new_element->next = NULL; // Point to end of list
return old_head;
}
// If the next element in the list (after the head) has
// a greater index, we have reached the point in the list
// where new_element should be inserted
else if ( head->next->index > new_element->index )
{
new_element->next = head->next; // Point to next element
head->next = new_element; // Add to list
return old_head;
}
// Otherwise, move to the next element in the list
else
head = head->next;
}
return old_head;
}
// Reverses the order of the list starting at `head` and returns a pointer to
// the resulting list. You do not need to preserve the original list.
list_t* reverse(list_t* head) {
assert(head != NULL);
// Need to keep track of the previous, current, and next
// list pointers
list_t* next_head = NULL;
list_t* previous_head = NULL;
// Iterate through the list until you reach the end
while (head != NULL)
{
// Make the next element lead to the current element,
// and make the previous element lead to the current element
next_head = head->next;
head->next = previous_head;
previous_head = head;
head = next_head;
}
return previous_head;
}