-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_3.c
133 lines (120 loc) · 2.19 KB
/
1_3.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct List // первый элемент будет служебным
{
int a;
struct List* next;
struct List* prev;
}List;
List* list_new ()
{
List* li = malloc (sizeof(List));
li->next = NULL;
li->prev = NULL;
li->a = 0;
return li;
}
int list_delete(List * li)
{
li = li->next;
while (li->next != NULL)
{
free(li->prev);
li = li->next;
}
free(li->prev);
free(li);
return 1;
}
int push(List * li,int* b)
{
while (li->next != NULL)
{
li = li->next;
}
List* l = malloc(sizeof(List));
li->next = l;
l->prev = li;
l->next = NULL;
l->a = *b;
return 1;
}
int pop(List* li,int* x)
{
if (li == NULL)
{
return 0;
}
while (li->next != NULL)
{
li = li->next;
}
*x = li->a;
return 1;
}
int unshift(List* li, int b)
{
List* l = malloc (sizeof(List));
if (li->next == NULL)
{
li->next = l;
l->prev = li;
l->next = NULL;
l->a = b;
return 1;
}
li->next->prev = l;
l->next = li->next;
li->next = l;
l->prev = li;
l->a = b;
return 1;
}
int shift(List* li, int* x)
{
if (li == NULL)
{
return 0;
}
*x = li->next->a;
return 1;
}
void print_list(List* li)
{
List*el = li->next;
while(el != NULL)
{
printf("%d\n", el->a);
el = el->next;
}
}
int main(void)
{
List* list = list_new();
printf("new list a:%d\n", list->a);
int b = 23;
printf("push 23: %d\n", push(list, &b));
int i = 0;
for (i = 0; i <= 10; i += 1)
{
int k;
k = push(list, &i);
}
int c;
printf("pop : %d\n", pop(list, &c));
printf("last element: %d\n", c);
int d = 155;
printf("unshift : %d\n", unshift(list, d));
printf("first element: %d\n", list->next->a);
int e;
printf("shift : %d\n", shift(list, &e));
printf("shift first element: %d\n", e);
print_list(list);
printf("list before delete:\n");
print_list(list);
list_delete(list);
printf("list after delete:\n");
print_list(list);
return 0 ;
}