-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.c
More file actions
170 lines (154 loc) · 3.96 KB
/
linkedList.c
File metadata and controls
170 lines (154 loc) · 3.96 KB
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
#include <stdlib.h>
#include <stdio.h>
/* NAME: Node
* PURPOSE: struct for the node of a linkedlist
* IMPORTS: void pointer to generic data to be stored
* ASSERTIONS: Linked list has been created
*/
typedef struct LinkedListNode
{
void* data;
struct LinkedListNode* next;
struct LinkedListNode* prev;
} Node;
/* NAME: LinkedList
* PURPOSE: create a linkedlist data structure
*/
typedef struct LinkedList
{
Node* HEAD;
Node* TAIL;
}LinkedList;
/* NAME: createLinkedList
* PURPOSE: initalise the linkedlist
* IMPORTS: NONE
* EXPORTS: list
* ASSERTIONS: linkedlist is currently not initalised
*/
LinkedList* createLinkedList(){
LinkedList* list;
list = (LinkedList*)malloc(sizeof(LinkedList));
list->HEAD= NULL;
return list;
}
/* NAME: insertStart
* PURPOSE:inserts an element at the start of the linkedlist
* IMPORTS: data generic data to be stored in the node, list linkedlist to store the created node
* EXPORTS: NONE
* ASSERTIONS: list has been initalised
*/
void insertStart(void* data,LinkedList* list){
Node* newNode;
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = list->HEAD;
newNode->prev = NULL;
if(list->HEAD!= NULL)
{
list->HEAD->prev = newNode;
}
else
{
list->TAIL = newNode;
}
list->HEAD = newNode;
}
/* NAME: insertEnd
* PURPOSE: inserts an element at the end of the linkedlist
* IMPORTS: data generic data to be stored in the node, list linkedlist to store the created node
* EXPORTS: NONE
* ASSERTIONS: list has been initalised
*/
void insertEnd(void* data,LinkedList* list){
Node* newNode;
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = list->TAIL;
newNode->next = NULL;
if(list->TAIL != NULL)
{
list->TAIL->next = newNode;
}
else
{
list->HEAD = newNode;
}
list->TAIL = newNode;
}
/* NAME: linkedlistLength
* PURPOSE: gets the length of the linkedlist
* IMPORTS: list the linkedlist
* EXPORTS: lenght an int for the length of the linkedlist
* ASSERTIONS: list has been initalised
* ! Not used
*/
int linkedlistLength(LinkedList* list){
int length = 0;
Node* current = list->HEAD;
while (current != NULL)
{
length++;
current = current->next;
}
return length;
}
/* NAME: isEmpty
* PURPOSE: checks if the linkedlist is empty
* IMPORTS: list the linkedlist
* EXPORTS: 1 or 0
* ASSERTIONS: list has been initalised
* I dont think this really works that well
*/
int isEmpty(LinkedList* list){
return(list == NULL || (list->HEAD == NULL && list->TAIL == NULL));
}
/* NAME: deleteLastNode
* PURPOSE: remove last node of the linkedlist
* IMPORTS: list the linkedlist
* EXPORTS: NONE
* ASSERTIONS: list has been initalised and is not empty
*/
void deleteLastNode(LinkedList* list){
if(isEmpty(list)== 0){
Node* temp;
temp = list->TAIL;
if(temp->prev != NULL){
list->TAIL = temp->prev;
list->TAIL->next = NULL;
}else{
list->HEAD =NULL;
list->TAIL = NULL;
}
if(temp->data != NULL){
free(temp->data);
}
free(temp);
}
}
/* NAME: deleteEntireList
* PURPOSE: calls the deletaLastNode func until the list is empty
* IMPORTS: list the linkedlist
* EXPORTS: NONE
* ASSERTIONS: list has been initalised
*/
void deleteEntireList(LinkedList* list){
while (isEmpty(list)==0)
{
deleteLastNode(list);
}
free(list);
}
/* NAME: printList
* PURPOSE: prints the linkedlist contents
* IMPORTS: list the linkedlist
* EXPORTS: NONE
* ASSERTIONS: the list has been initalised and is not empty
*/
void printList(LinkedList* list){
Node* current = list->HEAD;
while (current != NULL) {
int* retrievedData = (int*)current->data;
printf("Data: %d %d %d\n", retrievedData[0], retrievedData[1], retrievedData[2]);
current = current->next;
}
}