aboutsummaryrefslogtreecommitdiff
path: root/src/list-tail-node.c
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2022-04-09 09:46:17 +0200
committerThomas Schmucker <ts@its1.de>2022-04-09 09:46:17 +0200
commit38f5e364f73967c01f9ed442fe6646e70cb1dde0 (patch)
tree274817eb2793584b0e3d856de5504a614f2b4e89 /src/list-tail-node.c
parent2863f4f1d2a6a6a8704454824d6c291d2e0d4b5c (diff)
parent154874afda4a8df885e51c01f7681f04fb0b8e61 (diff)
downloaddata-structures-38f5e364f73967c01f9ed442fe6646e70cb1dde0.tar.gz
data-structures-38f5e364f73967c01f9ed442fe6646e70cb1dde0.tar.bz2
data-structures-38f5e364f73967c01f9ed442fe6646e70cb1dde0.zip
Merge branch 'rework-directory-structure'
Diffstat (limited to 'src/list-tail-node.c')
-rw-r--r--src/list-tail-node.c197
1 files changed, 197 insertions, 0 deletions
diff --git a/src/list-tail-node.c b/src/list-tail-node.c
new file mode 100644
index 0000000..edfcf31
--- /dev/null
+++ b/src/list-tail-node.c
@@ -0,0 +1,197 @@
1/* Standard C */
2#include <stdbool.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6/* Project */
7#include "util.h"
8
9/* --8<-- list_tail_type */
10typedef int T;
11
12struct list_node {
13 struct list_node *next;
14 T data;
15};
16
17struct list {
18 struct list_node *head, *tail;
19};
20/* -->8-- */
21
22/* --8<-- list_tail_init */
23void
24list_init(struct list *list)
25{
26 list->head = NULL;
27}
28/* -->8-- */
29
30/* --8<-- list_tail_create_node */
31static struct list_node *
32create_node(struct list_node *next, T data)
33{
34 struct list_node *node = malloc(sizeof *node);
35
36 if ( node ) {
37 node->next = next;
38 node->data = data;
39 }
40 return node;
41}
42/* -->8-- */
43
44/* --8<-- list_tail_push_back */
45void
46list_push_back(struct list *list, T data)
47{
48 struct list_node *node = create_node(NULL, data);
49
50 if ( node ) {
51 if ( list->head == NULL ) {
52 list->head = node;
53 }
54 else {
55 list->tail->next = node;
56 }
57 list->tail = node;
58 }
59 else {
60 ERROR("out of memory");
61 }
62}
63/* -->8-- */
64
65/* --8<-- list_tail_front */
66void
67list_push_front(struct list *list, T data)
68{
69 struct list_node *node = create_node(list->head, data);
70
71 if ( node ) {
72 if ( list->head == NULL ) { // Einfügen in eine leere Liste?
73 list->tail = node;
74 }
75 list->head = node;
76 }
77 else {
78 ERROR("out of memory");
79 }
80}
81/* -->8-- */
82
83/* --8<-- list_tail_pop_front */
84bool
85list_pop_front(struct list *list, T *data)
86{
87 if ( list->head ) {
88 struct list_node *next = list->head->next;
89
90 if ( data ) {
91 *data = list->head->data;
92 }
93 free(list->head);
94 list->head = next;
95
96 return true;
97 }
98 else {
99 return false;
100 }
101}
102/* -->8-- */
103
104/* --8<-- list_tail_insert_next */
105void
106list_insert_next(struct list *list, struct list_node *node, T data)
107{
108 if ( node == NULL ) { // Am Anfang einfügen
109 list_push_front(list, data);
110 }
111 else {
112 struct list_node *new_node = create_node(node->next, data);
113
114 if ( new_node ) {
115 if ( node->next == NULL ) { // Am Ende einfügen
116 list->tail = new_node;
117 }
118 node->next = new_node;
119 }
120 else {
121 ERROR("out of memory");
122 }
123 }
124}
125/* -->8-- */
126
127/* --8<-- list_tail_delete_next */
128void
129list_delete_next(struct list *list, struct list_node *node, T *data)
130{
131 if ( node == NULL ) { // Am Anfang entfernen
132 list_pop_front(list, data);
133 }
134 else {
135 if ( node->next ) {
136 struct list_node *old_node = node->next;
137 node->next = node->next->next;
138
139 if ( node->next == NULL ) {
140 list->tail = node;
141 }
142
143 if ( data ) {
144 *data = old_node->data;
145 }
146 free(old_node);
147 }
148 }
149}
150/* -->8-- */
151
152/* --8<-- list_tail_empty */
153bool
154list_empty(struct list *list)
155{
156 return list->head == NULL;
157}
158/* -->8-- */
159
160/* --8<-- list_tail_free */
161void
162list_free(struct list *list)
163{
164 struct list_node *item, *next;
165
166 for ( item = list->head; item; item = next ) {
167 next = item->next;
168 free(item);
169 }
170}
171/* -->8-- */
172
173int
174main()
175{
176 struct list list[1];
177
178 list_init(list);
179
180 for ( int i = 0; i != 10; ++i )
181 list_push_front(list, -i);
182
183 for ( int i = 0; i != 10; ++i )
184 list_push_back(list, i);
185
186 while ( !list_empty(list) ) {
187 int i;
188 if ( list_pop_front(list, &i) )
189 printf("%d\n", i);
190 else
191 ERROR("this shouldn't happen!");
192 }
193
194 list_free(list);
195
196 return EXIT_SUCCESS;
197}