-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.c
105 lines (78 loc) · 1.92 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
/* FILE NAME: linkedList.c
AUTHOR : Harshi Kasundi Bandaranayake
DATE : 02/10/2021
INCLUDES : linkedList.h */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "linkedList.h"
/* FUNCTION NAME: createLinkedList
PURPOSE: create an empty linked list
IMPORTS: NONE
EXPORTS: list */
LinkedList* createLinkedList()
{
LinkedList* list = (LinkedList *) malloc (sizeof(LinkedList));
(*list).head = NULL;
return list;
}
/* FUNCTION NAME: listLength
PURPOSE: calculates the length of the linked list
IMPORTS: LinkedListNode
EXPORTS: length */
int listLength(LinkedListNode* h)
{
int length=0;
if(h != NULL)
{
length = 1+ listLength((*h).next);
}
return length;
}
/* FUNCTION NAME: insertFirst
PURPOSE: adds a linked list node beginning of the linked list
IMPORTS: list, data, size
EXPORTS: NONE */
void insertFirst(LinkedList *list, void* data, int size)
{
LinkedListNode* newNode;
newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
(*newNode).next = (*list).head;
(*list).head = newNode;
(*newNode).data = malloc(size);
memcpy(newNode->data, data, size);
}
/* FUNCTION NAME: deleteFirst
PURPOSE: deletes the first node of the linked list and returns data stored in it
IMPORTS: NONE
EXPORTS: temp */
void* deleteFirst(LinkedList *list)
{
void* temp;
LinkedListNode* tempLink;
tempLink = (*list).head;
if(tempLink != NULL )
{
(*list).head = (*tempLink).next;
}
temp = (*tempLink).data;
free(tempLink);
return temp;
}
/* FUNCTION NAME: freeLinkedList
PURPOSE: de-allocates memory in the linked list
IMPORTS: list
EXPORTS: NONE */
void freeLinkedList(LinkedList *list)
{
LinkedListNode *current, *nextNode;
current = (*list).head;
while(current != NULL)
{
nextNode = (*current).next;
free(current->data);
free(current);
current = nextNode;
}
free(list);
}