-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliste.c
170 lines (133 loc) · 2.76 KB
/
liste.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
166
167
168
169
170
#include <stdio.h>
#include <string.h>
#include "liste.h"
typedef struct kettenglied
{
struct kettenglied *vor, *nach;
void* daten;
} Kettenglied;
static Kettenglied* start = NULL;
static Kettenglied* actual = NULL;
void liste_add(void* data)
{
Kettenglied* neu = malloc(sizeof(Kettenglied));
memset(neu, 0,sizeof(Kettenglied));
neu->daten = data;
if(liste_isEmpty())
{
start = neu;
} else
{
liste_moveLast();
actual->nach = neu;
neu->vor = actual;
}
actual = neu;
}
void list_insert(void* value) // insert value between actual and actual->nach
{
// !!!!!! Experimental !!!!!! //
// length(list) <= 1 oder ende der liste
if (liste_isBOL() || liste_isEOL())
{
liste_add(value);
return;
}
// middle of list:
// turn this into a function -> create kettenglied()
Kettenglied* neu = malloc(sizeof(Kettenglied));
memset(neu, 0, sizeof(Kettenglied));
neu->daten = value;
// end funtion
actual->vor->nach = neu;
actual->nach->vor = neu;
}
void* liste_get() // return data for current pointer if not empty:NULL
{
return liste_isEmpty() ? NULL : actual->daten;
}
bool liste_update(void* value) // change value at current position
{
if (liste_isEmpty()) {
return false;
}
actual->daten = value;
return true;
}
bool liste_remove() // remove current part from chain
{
if (liste_isEmpty()) // liste ist leer
return false;
Kettenglied* toDelete = actual;
if (liste_isEOL() && liste_isBOL()) { // nur 1 objekt in liste
start = NULL;
actual = NULL;
}
else if (liste_isEOL()) { // ende der liste
liste_movePrevious();
actual->nach = NULL;
}
else if (liste_isBOL()) { // anfang der liste
liste_moveNext();
start = actual;
actual->vor = NULL;
}
else {
// middle of list
actual->vor->nach = actual->nach;
actual->nach->vor = actual->vor;
liste_moveNext();
}
// clear actual
free(toDelete); // löscht den eintrag an der stelle &toDelete
return true;
}
void liste_removeAll() // remove all chain part of list
{
while(liste_remove()) {}
}
bool liste_moveNext()
{
if (liste_isEOL())
return false; // no more chainpart
actual = actual->nach;
return true;
}
bool liste_movePrevious()
{
if (liste_isBOL())
return false; // no more chainpart
actual = actual->vor;
return true;
}
bool liste_moveFirst()
{
// return liste_isEmpty() ? false : true;
if (liste_isEmpty())
return false;
while (liste_movePrevious()) {}
return true;
/*
parameter = int index
liste_movePrevious();
return list_isEmpty() ? false : liste_isBOL() ? true : liste_moveFirst(index + 1);
*/
}
bool liste_moveLast()
{
if (liste_isEmpty())
return false;
while (liste_moveNext()) {}
return true;
}
bool liste_isEmpty()
{
return start;
}
bool liste_isBOL()
{
return start == actual;
}
bool liste_isEOL(){
return liste_isEmpty() || actual->nach;
}