1+ #include <stdio.h>
2+ #include <stdlib.h>
3+
4+ typedef struct node_t {
5+ int value ;
6+ struct node_t * prev ;
7+ struct node_t * next ;
8+ } node_t ;
9+
10+ typedef struct list_t {
11+ node_t * head ;
12+ node_t * tail ; // доп задание
13+ size_t size ;
14+ } list_t ;
15+
16+ void init (list_t * list ) {
17+ list -> head = NULL ;
18+ list -> tail = NULL ;
19+ list -> size = 0 ;
20+ }
21+
22+ void old_push_back (list_t * list , int val ) {
23+ node_t * temp = (node_t * ) malloc (sizeof (node_t ));
24+ temp -> value = val ;
25+ temp -> next = NULL ;
26+ node_t * curr ;
27+ if (list -> head != NULL ) {
28+ curr = list -> head ;
29+ while (curr -> next != NULL )
30+ curr = curr -> next ;
31+ curr -> next = temp ;
32+ temp -> prev = curr ;
33+ } else {
34+ list -> head = temp ;
35+ temp -> prev = NULL ;
36+ }
37+ list -> size ++ ;
38+ }
39+
40+ // здесь с использованием tail
41+ void push_back (list_t * list , int val ) {
42+ node_t * temp = (node_t * )malloc (sizeof (node_t ));
43+ temp -> value = val ;
44+ temp -> next = NULL ;
45+
46+ if (list -> size == 0 ) {
47+ list -> head = temp ;
48+ list -> tail = temp ;
49+ temp -> prev = NULL ;
50+ } else {
51+ temp -> prev = list -> tail ;
52+ list -> tail -> next = temp ;
53+ list -> tail = temp ;
54+ }
55+ list -> size ++ ;
56+ }
57+
58+ void destroy (list_t * list ) {
59+ // проходим по всему списку и все заменяем на NULL
60+ node_t * curr = list -> head ;
61+ node_t * prev = NULL ;
62+ while (curr != NULL ) {
63+ prev = curr ;
64+ curr = curr -> next ;
65+ free (prev );
66+ }
67+ list -> head = NULL ;
68+ list -> size = 0 ;
69+ // по факту это просто init()
70+ }
71+
72+
73+ void insert (list_t * list , node_t * node , int val ) {
74+ // еще нужно проверить, не равнво ли node != head и node != tail
75+ node_t * temp = (node_t * )malloc (sizeof (node_t ));
76+ temp -> value = val ;
77+ temp -> prev = node ;
78+ temp -> next = node -> next ;
79+ node -> next = temp ;
80+ node -> next -> prev = temp ;
81+ list -> size ++ ;
82+ }
83+
84+ void set (list_t * list , int index , int val ) {
85+ node_t * curr = list -> head ;
86+ int i = 0 ;
87+ while (i != index ) {
88+ curr = curr -> next ;
89+ i ++ ;
90+ }
91+ curr -> value = val ;
92+ }
93+
94+ int get (list_t * list , int index ) {
95+ if (index < 0 || index >= list -> size ) return -1 ;
96+ node_t * curr = list -> head ;
97+ for (int i = 0 ; i < index ; i ++ ) curr = curr -> next ;
98+ return curr -> value ;
99+ }
100+
101+ void erase (list_t * list , node_t * node ) {
102+ if (node == list -> head ) { // проверка на head
103+ list -> head = list -> head -> next ;
104+ if (node -> next != NULL ) // проверка на tail
105+ node -> next -> prev = NULL ;
106+ } else {
107+ node -> prev -> next = node -> next ;
108+ if (node -> next != NULL ) // проверка на tail
109+ node -> next -> prev = node -> prev ;
110+ }
111+ free (node );
112+ list -> size -- ;
113+ }
114+
115+ int find (list_t * list , int val ) {
116+ node_t * curr = list -> head ;
117+ int index = 0 ;
118+ while (curr != NULL ) {
119+ if (curr -> value == val ) return index ;
120+ curr = curr -> next ;
121+ index ++ ;
122+ }
123+ return -1 ;
124+ } // можно идти с конца если индекс больше середины
125+
126+ void print_list (list_t * list ) {
127+ node_t * curr = list -> head ;
128+ while (curr != NULL ) {
129+ printf ("%d " , curr -> value );
130+ curr = curr -> next ;
131+ }
132+ }
133+
134+ int main (int argc , char * argv []) {
135+ list_t list ;
136+ init (& list );
137+
138+ for (int i = 0 ; i < 10 ; i ++ ) old_push_back (& list , i );
139+
140+ print_list (& list );
141+ printf ("\n" );
142+
143+ printf ("%d\n" , find (& list , 8 ));
144+
145+ set (& list , 7 , 99999 );
146+ print_list (& list );
147+ printf ("\n" );
148+
149+ insert (& list , (& list )-> head , 15 );
150+ print_list (& list );
151+ printf ("\n" );
152+
153+ printf ("%d\n" , get (& list , 8 ));
154+
155+ print_list (& list );
156+ printf ("\n" );
157+
158+ old_push_back (& list , 123 );
159+ print_list (& list );
160+ }
0 commit comments