-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslist.c
111 lines (110 loc) · 3.52 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
//Yahya Saad
//slist.c
#include <stdio.h>
#include <stdlib.h>
#include "slist.h"
#define SUCCESS 0
#define ERROR -1
//start a double linked list
void dbllist_init(dbllist_t *dbllist)
{
if(dbllist == NULL){return;}
//init size, head and tail
dbllist_size(dbllist)= 0;
dbllist_head(dbllist)= NULL;
dbllist_tail(dbllist)= NULL;
}
//delete list
void dbllist_destroy(dbllist_t *dbllist,dbllist_destroy_t destroy)
{
if(dbllist==NULL || dbllist_head(dbllist)==NULL)//list doesn't exist
return;
dbllist_node_t* tmp= dbllist_head(dbllist);
while(tmp != NULL)
{
if(destroy==DBLLIST_FREE_DATA)//check flag
free(dbllist_data(tmp));
tmp=dbllist_next(dbllist_head(dbllist));
dbllist_prev(dbllist_head(dbllist))= NULL;
free(dbllist_head(dbllist));
dbllist_head(dbllist)=tmp;
dbllist_size(dbllist)=dbllist_size(dbllist)-1;
}
dbllist_init(dbllist);//empty the list again
}
//add to the end -> as a new node after the tail
int dbllist_append(dbllist_t *dbllist,void *data)
{
dbllist_node_t *node= (dbllist_node_t*) malloc(sizeof(dbllist_node_t));
if(!node || !dbllist || !data)
return ERROR;
dbllist_data(node)= data;
dbllist_next(node)= NULL;
dbllist_prev(node) = NULL;
if(dbllist_size(dbllist)==0){
dbllist_head(dbllist)= node;
dbllist_tail(dbllist)=dbllist_head(dbllist);
dbllist_prev(node) = dbllist_next(node);
}else{
dbllist_prev(node) = dbllist_tail(dbllist);
dbllist_next(dbllist_tail(dbllist))= node;
dbllist_tail(dbllist)= node;
}
dbllist_size(dbllist)++; //inc list size by 1, adding a new node in any case
return SUCCESS;
}
//add to the begun -> as a new head for the list
int dbllist_prepend(dbllist_t *dbllist,void *data)
{
dbllist_node_t *node = (dbllist_node_t *) malloc(sizeof(dbllist_node_t));
if(!node || !dbllist || !data) {
return ERROR;
}
dbllist_data(node) = data;
if (dbllist_size(dbllist) == 0) {
dbllist_head(dbllist) = node;
dbllist_tail(dbllist) = node;
} else {
dbllist_prev(dbllist_head(dbllist)) = node;
dbllist_next(node) = dbllist_head(dbllist);
dbllist_head(dbllist) = node;
}
dbllist_size(dbllist)++;
return SUCCESS;
}
//remove a specific node with giving node
int dbllist_remove(dbllist_t *dbllist, dbllist_node_t *node ,dbllist_destroy_t destroy){
//base case
if (dbllist == NULL || node == NULL) {
return ERROR;
}
dbllist_node_t* tmp= dbllist_head(dbllist);
while(tmp != NULL) {
if (tmp == node) {
//------- check both prev and next -------
if(dbllist_prev(tmp)){
dbllist_next(dbllist_prev(tmp)) = dbllist_next(tmp);
}
if(dbllist_next(tmp)){
dbllist_prev(dbllist_next(tmp)) = dbllist_prev(tmp);
}
//------- check both head and tail -------
if(tmp == dbllist_head(dbllist)){
dbllist_head(dbllist) = dbllist_next(tmp);
}
if(tmp == dbllist_tail(dbllist)){
dbllist_tail(dbllist) = dbllist_prev(tmp);
}
//------- check flag -------
if(destroy==DBLLIST_FREE_DATA){
free(dbllist_data(tmp));
}
// free and resize the list after delete
free(tmp);
dbllist_size(dbllist)--;
break;
}
tmp = dbllist_next(tmp); // check the next node
}
return SUCCESS;
}