-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslist.c
More file actions
49 lines (43 loc) · 1.08 KB
/
slist.c
File metadata and controls
49 lines (43 loc) · 1.08 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
#include <stdio.h>
#include <stdlib.h>
#include "slist.h"
slist* createList(){
slist* list = (slist*)malloc(sizeof(slist));
if(list == NULL){
fprintf(stderr, "Could not create LL\n");
exit(1);
}
list -> head = NULL;
list -> tail = NULL;
return list;
}
//creates new node and put it at the end of linked list
void insertTail(slist* list, const char* data){
//creates new node
struct node* newNode;
newNode = malloc(sizeof(struct node));
if(newNode == NULL){
fprintf(stderr, "Couldn't insert node into LL");
exit(1);
}
strcpy(newNode -> data, data);
newNode -> next = NULL;
//adjust pointers
if(list -> head = NULL){
list -> head = newNode;
list -> tail = newNode;
} else {
list -> head = newNode;
list -> tail = newNode;
}
}
//empties the current linked list
void freeList(slist* list){
struct node* current = list -> head;
while(current != NULL){
struct node* temp = current;
current = current -> next;
free(temp);
}
free(list);
}