/
LinkedList.c
164 lines (149 loc) · 3.27 KB
/
LinkedList.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
#include <stdlib.h>
#include "LinkedList.h"
/**
*Returns a pointer to a new, empty list.
*/
List list_create() {
List result = malloc(sizeof(struct list));
if (result != NULL) {
result->first = NULL;
result->last = NULL;
return result;
}
return NULL;
}
struct link *link_create(void *value, struct link *next) {
struct link *result = malloc(sizeof(struct link));
if (result != NULL) {
result->value = value;
result->next = next;
return result;
}
return NULL;
}
/**
* Returns the length of a list represented as an unsigned integer.
*/
unsigned int list_length(List list) {
unsigned int size = 0;
struct link *cursor = list->first;
while (cursor != NULL) {
++size;
cursor = cursor->next;
}
return size;
}
/**
* Returns true if list is empty else false
*/
int list_empty(List list) {
return list->first == NULL;
}
void list_inject(struct link *link, void *value) {
link->next = link_create(value, link->next);
}
/**
* Inserts value in list at the specified index.
*/
void list_insert(List list, void *value, int index) {
if (index == 0) {
list_prepend(list, value);
return;
}
struct link *cursor = list->first;
while (cursor) {
if (index-- == 1) list_inject(cursor, value);
cursor = cursor->next;
}
}
/**
* Inserts value at the start of the list
*/
void list_prepend(List list, void *value) {
list->first = link_create(value, list->first);
if (list->last == NULL) {
list->last = list->first;
}
}
/**
* Inserts value at the end of the list
*/
void list_append(List list, void *value){
if (list->last == NULL) {
list->last = list->first = link_create(value, NULL);
}else{
list->last = list->last->next = link_create(value, NULL);
}
}
/**
* Returns a pointer to the first element of the list.
*/
void *list_first(List list) {
if(list_empty(list)) {
return NULL;
}else{
return list->first->value;
}
}
/**
* Returns a pointer to the last element of the list.
*/
void *list_last(List list) {
if(list_empty(list)) {
return NULL;
}else{
return list->last->value;
}
}
/**
* Returns a pointer to the value at the specified index in the list.
*/
void *list_get(List list, int index) {
struct link *cursor = list->first;
for (int i = 0;i <= index;i++) {
if (i == index)
return cursor->value;
else
cursor = cursor->next;
}
return NULL;
}
/*
int main(void) {
List l = list_create();
int *x = malloc(sizeof(int));
int *y = malloc(sizeof(int));
int *z = malloc(sizeof(int));
*x = 1;
*y = 2;
*z = 3;
append(l,x);
append(l,y);
append(l,z);
for (int i = 0;i < length(l);i++) {
//printf("Element %d has a value of %d\n",i,*get(l,i));
}
free(x);
free(y);
free(z);
//printf("First element is %d\n Last element is %d\n",*first(l),*last(l));
List l2 = list_create();
char *x2 = malloc(sizeof(char));
char *y2 = malloc(sizeof(char));
char *z2 = malloc(sizeof(char));
*x2 = 'a';
*y2 = 'b';
*z2 = 'c';
append(l2,x2);
append(l2,y2);
append(l2,z2);
for (int i = 0; i < length(l2);i++)
{
int *test = get(l2,i);
printf("Element %i has a value of %c\n", i, *test);
}
free(x2);
free(y2);
free(z2);
return 0;
}*/