-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistlib.c
171 lines (152 loc) · 3.52 KB
/
listlib.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
#include "LinkedList.h"
//Constructor to create a list
List* CreateList(int val, List* list)
{
List* newlist = malloc(sizeof(List));
newlist->data = val;
newlist->rest = list;
return newlist;
}
//Recursive function used to implement ListConstruct
List* ListBuilder(int val, int start, int size)
{
if(start < size - 1)
{
return CreateList(val, ListBuilder(val, start + 1, size));
}
return CreateList(val, EmptyList());
}
//Creates a list of a specified size filled with the 'val' argument
List* ListConstruct(int val, int size)
{
ListBuilder(val, 0, size);
}
//Recursively free all the elements in a list, then sets the pointer to null
void ListDestroy(List** list)
{
if(isEmpty(*list))
{
printf("List is empty\n");
*list = nullptr;
return;
}
if(!isEmpty(*list))
{
List* temp = (*list);
(*list) = (*list)->rest;
free(temp);
temp = nullptr;
ListDestroy(list);
}
}
//Useful function to create an empty list
List* EmptyList()
{
return nullptr;
}
//Checks if the element points to 0
int isEmpty(List* empty)
{
if(empty == nullptr)
{
return 1;
}
return 0;
}
//Recursive function used to implement list_length
int length(List* list, int startPos)
{
if(isEmpty(list))
{
return startPos;
}
length(list->rest, startPos + 1);
}
//Returns the amount of elements on a list
int list_length(List* list)
{
length(list, 0);
}
//Recursive Function used to implement searchPos
List* getPosition(List* list, int start, int pos)
{
if(start == pos)
{
return list;
}
getPosition(list->rest, start + 1, pos);
}
//Searches for the element in the specified position, set start to zero unless
//You otherwise need to
List* searchPos(List* list, int pos)
{
return getPosition(list, 0, pos);
}
//Grabs the last element of a list
List* last(List* list)
{
if(isEmpty(list->rest))
{
return list;
}
last(list->rest);
}
//Inserts a value at a zero-indexed position in the list
void insert(List* list, int val, int ind)
{
if(ind <= 0)
{
printf("Use the (join_head) function to insert at the start.\n");
return;
}
List* prev = searchPos(list, ind - 1);
List* next = prev->rest;
prev->rest = CreateList(val, next);
printf("Inserted element into list at position %d\n", ind);
prev = nullptr;
next = nullptr;
}
//Returns a new list with the specified value at the start
List* join_head(List* list, int val)
{
return CreateList(val, list);
}
//Add the elements of the second list to the first
void join_list(List* first, List* second)
{
if(!isEmpty(first->rest))
{
List* connect = last(first);
connect->rest = second;
connect = nullptr;
}
else if(isEmpty(first->rest))
{
first->rest = second;
}
}
//Adds an element to the end of the list
void append_list(List* list, int val)
{
List* last_element = last(list);
last_element->rest = CreateList(val, EmptyList());
//Set pointers to null before returning
last_element = nullptr;
}
//Replaces an element with the value of your choice
void replace(List* list, int ind, int val)
{
searchPos(list, ind)->data = val;
}
//Recursively displays all the elements of a list in a way that is easy to visualize
int DisplayList(List* display)
{
if(isEmpty(display))
{
printf("End of list.\n");
return 0;
}
printf("%d", display->data);
printf("->");
DisplayList(display->rest);
}