-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslist.c
131 lines (122 loc) · 2.4 KB
/
slist.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "slist.h"
void printlist(slist_t*);
void slist_init(slist_t *list)
{
list->head = NULL;
list->tail = NULL;
list->size = 0;
}
void printlist(slist_t *list){
slist_node_t* l = list->head;
printf("List is:");
while(l != NULL) {
printf(" %s", (char*) l->data);
l = l->next;
}
}
void slist_destroy(slist_t *list, slist_destroy_t dealloc)
{
if(dealloc == SLIST_LEAVE_DATA)
{
free(list);
slist_size(list) = 0;
list = NULL;
}
if(dealloc == SLIST_FREE_DATA)
{
slist_node_t* current = list->head;
slist_node_t* tmp;
while(current != NULL)
{
tmp = current;
current = current->next;
free(tmp->data);
free(current);
}
free(list);
list = NULL;
}
}
void* slist_pop_first(slist_t *list)
{
if(list == NULL){
//printf("No solution available, exiting program.\n");
return NULL;
}
slist_node_t* newHead = list->head->next;
slist_node_t* poped = list->head;
list->head = newHead;
list->size--;
return poped;
}
int slist_append(slist_t *list, void *data)
{
// if the list not exist
if(list == NULL) {
printf("ERROR: the list wasn't initialized\n");
return -1;
}
// Create new node
slist_node_t* newNode = malloc(sizeof(slist_node_t));
newNode->data = data;
newNode->next = NULL;
// if list is empty
if(list->head == NULL){
list->head = newNode;
list->tail = newNode;
}
else // Add node to the end of list
{
list->tail->next = newNode;
list->tail = newNode;
}
list->size++;
return 0;
}
int slist_prepend(slist_t *list, void *data)
{
// if the list is empty
if(list == NULL){
printf("ERROR: the list wasn't initialized\n");
return -1;
}
// Create new node
slist_node_t *newNode = malloc(sizeof(slist_node_t));
newNode->data = data;
if(list->head == NULL)
{
list->head = newNode;
list->tail = newNode;
}
else
{
newNode->next = list->head;
list->head = newNode;
}
list->size++;
return 0;
}
int slist_append_list(slist_t *destinationList, slist_t *sourceList)
{
if(destinationList == NULL)
{
printf("ERROR: the destination list wasn't initialized\n");
return -1;
}
if(sourceList == NULL)
{
printf("ERROR: the source list wasn't initialized\n");
return -1;
}
slist_node_t* current = sourceList->head;
while(current != NULL)
{
slist_append(destinationList, current->data);
current = current->next;
}
free(current); //free memory
return 0;
}