-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.c
More file actions
169 lines (154 loc) · 3.31 KB
/
List.c
File metadata and controls
169 lines (154 loc) · 3.31 KB
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
/*
CSE 109: Spring 2018
Dylan Spector
drs320
The C file for a linked-list object.
Program #3
*/
#include"List.h"
#include"Node.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct List_t* makeList(struct List_t* it)
{
it->size = 0;
struct Node_t* newNode = (struct Node_t*)malloc(1 * sizeof(struct Node_t));
newNode = makeNode1(newNode);
it->headNode = newNode;
return it;
}
struct List_t* freeList(struct List_t* it)
{
struct Node_t* current = it->headNode;
struct Node_t* next = getNext(it->headNode);
if(size(it) == 0)
{
freeNode(current);
}
for(size_t i = 0; i < size(it); i++)
{
freeNode(current);
current = next;
next = getNext(current);
}
free(it);
return NULL;
}
size_t size(struct List_t* it)
{
return it->size;
}
struct Node_t* insert(struct List_t* it, int value)
{
struct Node_t* current = it->headNode;
struct Node_t* next = getNext(current);
struct Node_t* newNode = (struct Node_t*)malloc(1 * sizeof(struct Node_t));
newNode = makeNode2(newNode, value);
if(size(it) == 0)
{
setData(current, getData(newNode));
freeNode(newNode);
}
else
{
for(size_t i = 0; i < size(it); i++) // for every node in the list
{
if(next == NULL) // if current is the last node in the list
{
setNext(current, newNode);
}
current = next;
next = getNext(current);
}
}
it->size++;
return newNode;
}
size_t find(struct List_t* it, int value)
{
size_t counter = 0;
struct Node_t* current = it->headNode;
for(size_t i = 0; i < size(it); i++) // ? size(it) or it->size
{
if(getData(current) == value)
{
counter++;
}
current = getNext(current);
}
return counter;
}
size_t traverse(struct List_t* it, char* string) // string is large enough
{
if(it == NULL)
{
return 0;
}
struct Node_t* current = it->headNode;
for(size_t i = 0; i < size(it); i++) // for every node in the list
{
if(i == size(it) - 1) // if the last node in the list
{
sprintf(string, "%s%d", string, getData(current));
}
else
{
sprintf(string, "%s%d, ", string, getData(current));
current = getNext(current);
}
}
return strlen(string);
}
size_t removeItem(struct List_t* it, int value)
{
size_t numRemoved = 0;
struct Node_t* previous = NULL;
struct Node_t* current = it->headNode;
struct Node_t* next = getNext(it->headNode);
while(current != NULL)
{
if(getData(current) == value) // remove node
{
freeNode(current);
it->size--;
if(next == NULL) // If the current Node is the last in the list
{
if(previous != NULL) // If not the only node in the list
{
setNext(previous, NULL);
}
// current = NULL; I think I can remove this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
else if(previous == NULL) // If the current Node is the headNode
{
current = next; // it->headNode <-> current
next = getNext(current);
}
else // If the current Node isn't the head or at the end
{
setNext(previous, next);
current = next;
next = getNext(next);
}
numRemoved++;
}
else
{
previous = current;
current = next;
next = getNext(current);
}
}
return numRemoved;
}
char* traverse2(struct List_t* it)
{
if(size(it) == 0)
{
return NULL;
}
char* string = (char *)calloc(1, ((12 * size(it))-2) * sizeof(char));
traverse(it, string);
return string;
}