Task: implement a linked list.

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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"


// displayList()
// -------------
// Given a list, print its elements.
//
void displayList(listNode * list) {
    printf("list: ");
    while (list != NULL) {
        printf("%d ", list->value);
        list = list->next;
    }
    printf("%p\n", list);
    return;
}


// push()
// ------
// Given a pointer to the head node of a list and a value,
// add a new node with that value to the head of the list.
//
void push(listNode ** listPtr, int value) {
    listNode * newNodePtr = (listNode *) malloc(sizeof(listNode));
    newNodePtr->value = value;
    newNodePtr->next = *listPtr;
    *listPtr = newNodePtr;
    return;
}


// pop()
// -----
// Given a pointer to the head node of a list, remove the head node,
// free the head node's memory, and return the head node's value.
// Return 0 if the list is empty.
//
int pop(listNode ** listPtr) {
    int value = 0;
    listNode * tempPtr;
    if (*listPtr) {
        tempPtr = *listPtr;
        value = tempPtr->value;
        *listPtr = tempPtr->next;
        free(tempPtr);
    }
    return value;
}


// destroyList()
// -------------
// Given a pointer to the head of a list, remove and free the memory
// of all elements of the list.
//
void destroyList(listNode ** listPtr) {
    listNode * temp = *listPtr;
    while (temp != NULL) {
        *listPtr = (*listPtr)->next;
        free(temp);
        temp = *listPtr;
    }
    return;
}


// count()
// -------
// Given a pointer to the head node of a list and a value, return an 
// integer representing the number of times that value appears in the 
// list.
//
int count(listNode * listPtr, int value) {
    int c = 0;
    while (listPtr != NULL) {
        if (value == listPtr->value) c++;
        listPtr = listPtr->next;
    }
    return c;
} 


// insert()
// --------
// Given a pointer to the head node of a list and a value, add a new
// node with the value to the tail of the list. If the list is
// empty, the new node becomes the head node.
//
void insert(listNode ** listPtr, int value) {

    // Get the first node in the list.
    listNode * temp = *listPtr;

    // Create the new node to be appended.
    listNode * newNodePtr = (listNode *) malloc(sizeof(listNode));
    newNodePtr->value = value;
    newNodePtr->next = NULL;

    // If the list is empty, the new node becomes the head node.
    if (temp == NULL) {
        *listPtr = newNodePtr;
        return;
    }

    // Otherwise, traverse the list to find the last node.
    while (temp->next != NULL) temp = temp->next;

    // The node pointer in the last node in the list must be the
    // pointer to the new, appended node.
    temp->next = newNodePtr;
}


// reverse()
// ---------
// Given a pointer to the head node of a list, reverse the order of
// nodes in the list by relinking the existing nodes (do not allocate
// or free any memory).
//
void reverse(listNode ** listPtr) {

    // Get the first node in the list.
    listNode * curr = *listPtr;

    // If the list is empty, don't do anything to it..
    if (curr == NULL) return;

    // Otherwise, create two more pointers:
    // - `next` for storing the next node
    // - `prev` for storing the previous node
    // 
    // We store the pointer to the next node in `next`, then set the 
    // current node's node pointer to the pointer to the previous node.
    // Since the first node in the list will become the last node in
    // the reversed list, the first node's node pointer should be null.
    // Then, we set the previous node to the current node, and the
    // current node to the next node, iterating until the current node
    // is null. When the loop has completed, set the new head of the 
    // list to the final value of the previous node.
    listNode * prev = NULL;
    listNode * next = NULL;

    while (curr != NULL) {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }

    *listPtr = prev;
}


// removeAll()
// -----------
// Given a pointer to the head node of a list and a value, remove
// all nodes in the list that have that value, freeing the memory
// space for those nodes (and re-linking the surviving nodes). Must 
// work properly when the value is at the head or the tail of the list.
//
void removeAll(listNode ** listPtr, int value) {

    // Get the first node in the list.
    listNode * curr = *listPtr;

    // If the list is empty, don't do anything to it.
    if (curr == NULL) return;

    // Remove all contiguous leading nodes containing the value.
    while (curr != NULL && value == curr->value) {
        *listPtr = curr->next;
        free(curr);
        curr = *listPtr;
    }

    // If we're not done, we'll need to keep track of the previous node
    // while processing the rest of the list.
    listNode * prev;

    while (curr != NULL) {

        // Find the next occurrence of the value.
        while (curr != NULL && value != curr->value) {
            prev = curr;
            curr = curr->next;
        }

        // If we haven't found any other occurrences of the value, return.
        if (curr == NULL) return;

        // If we have found an occurrence of the value, relink the previous
        // node to the next node, then free the current node's memory and 
        // assign the (new) next node to its name.
        prev->next = curr->next;
        free(curr);
        curr = prev->next;
    }

}