-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
executable file
·60 lines (49 loc) · 1.13 KB
/
linked_list.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
/* This linked_list.c file has been adapted from the linked list that
* we were required to implement in prac 7. A lot of the functions have
* been omitted as they were not required, and the linked list has primarily
* been turned into a linked list that stores a char *, instead of a void *
*
* FILE: linked_list.c
* AUTHOR: Syed Faraz Abrar (19126296)
* UNIT: COMP1000
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linked_list.h"
/* This function inserts a node with the imported char * as its data
* at the end of the linked list
*/
void insertLast(Node **head, char *str)
{
Node *current = *head;
Node *node = (Node *) malloc(sizeof(Node));
strcpy(node->str, str);
node->next = NULL;
if (*head == NULL)
{
*head = node;
}
else if (current->next != NULL)
{
while (current->next != NULL)
{
current = current->next;
}
current->next = node;
}
else
{
current->next = node;
}
if(*head == NULL) printf("WTF\n");
}
/* This function is used to free the entire linked list recursively */
void freeList(Node **head)
{
if (*head != NULL)
{
freeList(&((*head)->next));
free(*head);
}
}