-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.c
217 lines (174 loc) · 4.98 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*--------------------------------------------------- LinkedList -----
| Author: Tarek Alakmeh
| Date: April 2020
| Lang: C (English)
|
| struct
| - node
| - list
|
| functions
| - initNode
| - initLinkedList
| - appendAtTail
| - appendAtHead
| - appendAtPosition
| - size
| - printList
| - deleteNode (TODO)
| - main
|
*-------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
int size();
struct node {
int val;
struct node* next;
};
struct list {
struct node* head;
struct node* tail;
};
/*----------------------------------------------------- initNode -----
| Parameters:
| int val (IN) -- Node Value
|
| Returns:
| struct node (OUT) -- Returns initialised node
*-------------------------------------------------------------------*/
struct node* initNode(int val) {
struct node* new_node = malloc(sizeof(struct node));
new_node->val = val;
new_node->next = NULL;
return new_node;
}
/*----------------------------------------------- initLinkedList -----
| Returns:
| struct list (OUT) -- Returns initialised node
*-------------------------------------------------------------------*/
struct list* initLinkedList() {
struct list* l = malloc(sizeof(struct list));
l->head = NULL;
l->tail = NULL;
return l;
}
/*-------------------------------------------------- appendAtTail -----
| Purpose: Add node to the end of linkedList
|
| Parameters:
| struct list l (IN) -- LinkedList
| int val (IN) -- Node Value
*-------------------------------------------------------------------*/
void appendAtTail(struct list* l, int val) {
struct node* new_node = malloc(sizeof(struct node));
new_node->val = val;
new_node->next = NULL;
// struct node* new_node = initNode(val); // alternatively
if (l->tail == NULL) {
l->head = new_node;
l->tail = new_node;
} else {
l->tail->next = new_node;
l->tail = new_node;
}
}
/*------------------------------------------------- appendAtHead -----
| Purpose: Add node to the start of linkedList
|
| Parameters:
| struct list l (IN) -- LinkedList
| int val (IN) -- Node Value
*-------------------------------------------------------------------*/
void appendAtHead(struct list* l, int val) {
struct node* new_node = initNode(val);
if(l->head == NULL) {
l->head = new_node;
l->tail = new_node;
} else {
new_node->next = l->head;
l->head = new_node;
}
}
/*--------------------------------------------- appendAtPosition -----
| Purpose: Add node at stated position
|
| Parameters:
| struct list l (IN) -- LinkedList
| int val (IN) -- Node Value
| int post (IN) -- Position of new node (pos = 0,1,2,...,n-1)
*-------------------------------------------------------------------*/
void appendAtPosition(struct list* l, int val, int pos) {
/* Switch to prior methods if head / tail needs to be updated */
if(pos == 0) {
appendAtHead(l, val);
return;
} else if (pos == size(l)) {
appendAtTail(l, val);
return;
}
struct node* new_node = initNode(val);
struct node* n = l->head;
int i = 0;
while(n->next != NULL && i < pos-1) {
n = n->next;
i++;
}
new_node->next = n->next;
n->next = new_node;
}
/*--------------------------------------------------------- size -----
| Returns:
| int size (OUT) -- Returns size of linkedList
*-------------------------------------------------------------------*/
int size(struct list* l) {
struct node *n = l->head;
int i = 0;
while(n != NULL) {
n = n->next;
i++;
}
return i;
}
/*---------------------------------------------------- printList -----
| Purpose:
| Prints linkedList. i.e. [ 1 2 3 4 5 ]
|
| Parameters:
| struct list l (IN) -- LinkedList
*-------------------------------------------------------------------*/
void printList(struct list* l){
printf("[");
struct node* current = l->head;
int i = 0;
while(current != NULL) {
printf(" %d ",current->val);
current = current->next;
i++;
}
printf("]");
}
/*---------------------------------------------------- deleteNode -----
| Purpose:
| Deletes node at position pos from linkedList
|
| Parameters:
| struct list l (IN) -- LinkedList
| int pos (IN) -- Postion of node to be deleted
*-------------------------------------------------------------------*/
void deleteNode(struct list *listA, int pos) {
/* TODO */
}
/* Try it out: returns [ 0 1 2 3 4 5 ] */
int main() {
struct list* l = initLinkedList();
appendAtTail(l, 2);
appendAtTail(l, 4);
appendAtHead(l, 1);
appendAtPosition(l, 3, 2);
appendAtHead(l, 0);
appendAtTail(l, 5);
printList(l);
printf("\nlinkedList size: %d",size(l));
return 0;
}