-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
165 lines (139 loc) · 3.27 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
158
159
160
161
162
163
164
165
#include "list.h"
#include <stdlib.h>
#include <stdio.h>
/**
* * \class List list.h
* * \brief A chained list.
* */
/**
* * \fn int List_push(List *This, const int val)
* * \brief Push the list to add a new node with a value.
* * \param[in, out] This The List to add into.
* * \param[in] val The value to add.
* * \return 0 if no error detected. See list.h for error values.
* */
int List_push(List *This, const int val){
Node *newNode = malloc(sizeof(Node));
if(!newNode) {
return -1;
}
newNode->value = val;
newNode->next = This->top;
This->top = newNode;
This->size++;
return 0;
}
/**
* * \fn int List_pop(List *This)
* * \brief Pop a value from a List. Delete from the List and return the last value.
* * \param[in, out] This The List to pop from.
* * \return The value poped.
* */
int List_pop(List *This){
int val;
Node *tmp;
if(!This->top) {
return -1;
}
tmp = This->top->next;
val = This->top->value;
free(This->top);
This->top = tmp;
This->size++;
return val;
}
/**
* * \fn void List_clear(List *This)
* * \brief Free all values from a List.
* * \param[in, out] This The List to free.
* */
void List_clear(List *This){
Node *tmp;
while(This->top){
tmp = This->top->next;
free(This->top);
This->top = tmp;
}
This->size = 0;
}
/**
* * \fn int List_length(List *This)
* * \brief Get the current size/length of a List.
* * \param[in, out] This The List to free.
* * \return The current size/length of a List.
* */
int List_length(List *This){
return This->size;
}
/**
* \fn void List_view(List *This)
* \brief Display the List on the standard output.
* \param[in, out] This The List to display.
* \bug Problems on the display.
*/
void List_view(List *This){
Node *tmp = This->top;
while(tmp){
printf("%d\n",tmp->value);
tmp = tmp->next;
}
}
/**
* \fn void List_free(List *This)
* \brief Free all values from a List. but not List itself.
* \param[in, out] This The List to free.
*/
void List_free(List *This){
List_clear(This);
}
/**
* \fn void List_newFree(List *This)
* \brief Free all values from a List with List itslef.
* \param[in, out] This The List to free.
*/
void List_newFree(List *This){
if(This) {
List_clear(This);
}
free(This);
}
/**
* \fn static void List_init(List *This)
* \brief initialize all values of a List.
* \param[in, out] This The List to initialize.
*/
static void List_init(List *This){
This->size = 0;
This->top = NULL;
This->push = List_push;
This->pop = List_pop;
This->clear = List_clear;
This->length = List_length;
This->view = List_view;
}
/**
* \fn List List_create()
* \brief Create statically a List.
* \return The new List.
*/
List List_create(){
List This;
List_init(&This);
This.free = List_free;
return This;
}
/**
* \fn List* new_List()
* \brief Create dynamically (with a malloc) a List.
* \return The new List pointer.
*/
List* new_List(){
List *This = malloc(sizeof(List));
if(!This) {
perror("malloc");
return NULL;
}
List_init(This);
This->free = List_newFree;
return This;
}