-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
executable file
·158 lines (146 loc) · 3.31 KB
/
list.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
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
#include "list.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
list* init()
{
list* rlist = (list*)malloc(sizeof(list));
SAFE_ALLOCATED_POINTER(rlist);
node* first = (node*)malloc(sizeof(node));
SAFE_ALLOCATED_POINTER(first);
node* tail = (node*)malloc(sizeof(node));
SAFE_ALLOCATED_POINTER(tail);
rlist->first = first;
rlist->tail = tail;
rlist->size = 0;
return rlist;
}
int is_empty(list* list){
return !list->size;
}
void append(list* list, void* element)
{
node* temp_node = (node*)malloc(sizeof(node));
SAFE_ALLOCATED_POINTER(temp_node);
temp_node->data = element;
temp_node->next = NULL;
temp_node->prev = NULL;
if(list->size == 0)
list->first = temp_node;
else{
temp_node->prev = list->tail;
list->tail->next = temp_node;
}
list->tail = temp_node;
list->size++;
}
node* popend(list* list)
{
node* ret_node = list->tail;
if(list->size == 0)
return NULL;
if(list->size == 1){
list->first = NULL;
list->tail = NULL;
}
else{
list->tail = list->tail->prev;
list->tail->next = NULL;
}
ret_node->next = NULL;
ret_node->prev = NULL;
list->size--;
return ret_node;
}
node* popfirst(list* list)
{
node* ret_node = list->first;
if(list->size == 0)
return NULL;
if(list->size == 1)
{
list->first = NULL;
list-> tail = NULL;
}
else
{
list->first = list->first->next;
list->first->prev = NULL;
}
ret_node->next = NULL;
ret_node->prev = NULL;
list->size--;
return ret_node;
}
void insert(list* list, void* element)
{
if(list->size == 0)
append(list, element);
else
{
node* temp_node = malloc(sizeof(node));
SAFE_ALLOCATED_POINTER(temp_node);
temp_node->data = element;
temp_node->prev = NULL;
temp_node->next = list->first;
list->first = temp_node;
temp_node->next->prev = temp_node;
list->size++;
}
}
node* get(list* list, int index){
if(list->size != 0){
if(index >= 0 & index < list->size){
node* temp_node = malloc(sizeof(node));
SAFE_ALLOCATED_POINTER(temp_node);
temp_node = list->first;
for(int i = 0; i < index; i++)
temp_node = temp_node->next;
return temp_node;
}
}
return NULL;
}
node* remove_at(list* list,unsigned int index)
{
if(list == NULL)
{
printf("ERROR: List is null\n");
return NULL;
}
if(is_empty(list))
{
printf("ERROR: List is13 empty\n");
return NULL;
}
if(index >= list->size)
{
printf("ERROR: Index is out of range.\n");
return NULL;
}
node* ret = get(list, index);
if(list->size ==1)
{
list->first = NULL;
list->tail = NULL;
}
else if(index == 0){
list->first = ret->next;
list->first->prev = NULL;
}
else if(index == list->size -1)
{
list->tail = ret->prev;
list->tail->next = NULL;
}
else{
ret->prev->next = ret->next;
ret->next->prev = ret->prev;
}
ret->next = NULL;
ret->prev =NULL;
list->size--;
return ret;
}