-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
103 lines (94 loc) · 2.45 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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
pslist slist_new(void)
{
pslist plst;
plst = (pslist)malloc(sizeof(struct slist));
plst->head = (pslist_entry)malloc(sizeof(struct slist_entry));
plst->list_size = 0;
plst->head->next = NULL;
return(plst);
}
pslist_entry slist_pop(pslist list)
{
pslist_entry prev = NULL;
int val;
if(list->head == NULL)
exit(-1);
prev = list->head;
val = prev->value;
list->head = list->head->next;
free(prev);
return NULL;
}
void slist_delete(pslist list)
{
while(list->head->next)
{
slist_pop(list);
list->head = list->head->next;
}
free(list->head);
free(list);
}
int slist_insert(pslist list, int value)
{
pslist_entry tmp = (pslist_entry)malloc(sizeof(struct slist_entry));
if (tmp == NULL)
return -1;
tmp->next = list->head;
tmp->value = value;
printf("New element: %d\n", tmp->value);
list->head = tmp;
list->list_size++;
return 0;
}
int slist_remove(pslist list, int value)
{
pslist_entry prev = NULL, prevprev; // Указатели на предыдущий и предпредыдущий элемент
int val; // сравниваемое значение
if(list->head == NULL)
exit(-1);
prevprev = prev;
prev = list->head;
val = prev->value;
list->head = list->head->next;
while(list->head)
{
if(val == value)
{
prevprev->next = list->head; // указатель, который ссылался на элемент, значение которого равно значению
// value, теперь ссылается на следующий за этим элемент.
free(prev);
prev = prevprev;
}
prevprev = prev;
prev = list->head;
val = prev->value;
list->head = list->head->next;
}
return 0;
}
void slist_print(pslist list)
{
pslist_entry pcurrent = list->head;
printf("List: \n");
while (pcurrent != NULL)
{
printf("> %d\n", pcurrent->value);
pcurrent = pcurrent->next;
}
}
int main()
{
pslist list;
list = slist_new();
printf("List created! %p\n", list);
slist_insert(list, 1);
slist_insert(list, 2);
slist_insert(list, 3);
slist_print(list);
slist_delete(list);
return 0;
}