Zapraszam na kolejny odcinek z serii wpisów na temat struktur danych. Dzisiaj uzupełnimy naszą wiedzę o pojęcie jakim jest lista dwukierunkowa, a więc poszerzymy koncept z poprzedniego wpisu.
Zanim zaczniemy, polecam zapoznać się z zagadnieniem listy jednokierunkowej. Program z dzisiejszego wpisu będzie szybką adaptacją tamtego , a więc oszczędzę Ci przydługich wywodów.

Nie daj się zaskoczyć na maturze – zapisz się do listy mailingowej już teraz!
Lista dwukierunkowa, a jednokierunkowa – zasadnicza różnica
Nie trzeba być Teslą, żeby domyślić się, że różnica pomiędzy listą dwu- a jednokierunkową polegać będzie na sposobie przemieszczania się po strukturze. W prostszej wersji listy poruszamy się tylko w jednym kierunku. Od początku do końca. Od głowy do ogona. Lista dwukierunkowa umożliwia zmianę azymutu na odwrotny, tzn. teraz możemy przechodzić po elementach od tyłu i cofać się w obrębie listy. Znika więc ograniczenie, które pojawiało się przy pierwszej z list. Mianowicie, gdy chcemy dostać się do np. przedostatniego elementu to nie musimy zaczynać przechodząc od początku listy. Możemy zacząć od jej końca, co pozwoli oszczędzić wiele kroków.
Jak widać każdy węzeł ma teraz więcej niż jeden wskaźnik. Teraz wskazuje nie tylko na następny element, ale również na poprzedni.
Lista dwukierunkowa – implementacja w C
Tak jak wspominałem w poprzednim wpisie – struktury zaimplementowane będą w C. Dzisiejszy program będzie pełnymi garściami czerpał z poprzedniego, więc ten wpis będzie mniej obszerny. Nie będę opisywać krok po kroku każdej funkcji.
Struktura listy dwukierunkowej C
1 2 3 4 5 |
typedef struct ListElement { int data; struct ListElement * previous; struct ListElement * next; } ListElement_type; |
Jedyne co trzeba mieć na uwadze to występowanie wskaźnika na poprzedni węzeł w strukturze. Co to zmienia? Przy każdej zmianie musimy edytować wskaźniki sąsiednich węzłów tak, aby nie pogubić wskaźników – nie rozerwać tego łańcuszka, który tworzą.
Poruszanie się po liście od tyłu
Tak jak wspomniałem od teraz mamy możliwość poruszania się po liście w odwrotnym kierunku. Skorzystamy, więc z niej, aby wyświetlić listę od tyłu.
Funkcja show_reverse() będzie wykorzystywać ten sam motyw z pętlą while co funkcja show(). Tylko, że zamiast korzystać ze wskaźnika next, skorzystam ze wskaźnika na element poprzedni – previous.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
void show_reverse(ListElement_type *head) { printf("\n"); if(head==NULL) printf("List is empty"); else { ListElement_type *current=head; while (current->next != NULL) { current = current->next; //idziemy na koniec listy } do { printf("%i", current->data); printf("\n"); current = current->previous; }while(current!=NULL); } } |
Dobrym pomysłem byłoby podawanie jako parametr funkcji ogona listy, a nie głowy. Nie byłoby wtedy potrzeby wtórnego przechodzenia po liście od jej początku. W tym celu można, by było wykorzystać np. dodatkowy wskaźnik tail, który wskazywałby na aktualny koniec listy.
Podsumowanie
Jak widać struktury nie są wcale zagadnieniem skomplikowanym. Powiedziałbym nawet, że są dość intuicyjne.
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
//Łukasz Kosiński - implementacja listy jednokierunkowej #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> typedef struct ListElement { int data; struct ListElement * previous; struct ListElement * next; } ListElement_type; void show(ListElement_type *head); void show_reverse(ListElement_type *head); int list_size(ListElement_type *head); void push_front(ListElement_type **head, int number); void push_back(ListElement_type **head, int number); void push_by_index(ListElement_type **head, int number, int position); void pop_front(ListElement_type **head); void pop_back(ListElement_type **head); void pop_by_index(ListElement_type **head, int position); int main() { ListElement_type *head; head = (ListElement_type *)malloc(sizeof(ListElement_type)); head=NULL; int option=-1; int number=-1; int index=-1; while(option!=0) { system("cls"); printf("\nAktualny stan listy: "); show(head); printf("\n\nDrogi uzytkowniku! Co chcesz zrobic?\n"); printf("1. Dodac element na poczatek listy.\n"); printf("2. Dodac element na koniec listy.\n"); printf("3. Dodac element zgodnie z wybranym indeksem.\n"); printf("4. Usunac element z poczatku listy.\n"); printf("5. Usunac element z konca listy.\n"); printf("6. Usunac element o wybranym indeksie.\n"); printf("7. Wyswietlic liste w odwrotnej kolejnosci.\n"); printf("0. Zakonczyc program.\n"); scanf("%i", &option); switch (option) { case 0: return 0; break; case 1: printf("Wpisz liczbe jaka chcesz dodac: "); scanf("%i", &number); push_front(&head, number); break; case 2: printf("Wpisz liczbe jaka chcesz dodac: "); scanf("%i", &number); push_back(&head, number); break; case 3: printf("Wpisz liczbe jaka chcesz dodac: "); scanf("%i", &number); printf("Wpisz indeks: "); scanf("%i", &index); push_by_index(&head, number, index); break; case 4: pop_front(&head); break; case 5: pop_back(&head); break; case 6: printf("Wpisz indeks elementu, ktorego chcesz sie pozbyc raz na zawsze: "); scanf("%i", &index); pop_by_index(&head, index); break; case 7: show_reverse(head); Sleep(4000); break; default: printf("Podaj wlasciwa opcje."); Sleep(2000); break; } } return 0; } void push_front(ListElement_type **head, int number) { if(*head==NULL) { *head = (ListElement_type *)malloc(sizeof(ListElement_type)); (*head)->data = number; (*head)->previous=NULL; (*head)->next = NULL; } else { ListElement_type *current; current=(ListElement_type *)malloc(sizeof(ListElement_type)); current->data=number; current->previous=NULL; current->next=(*head); (*head)->previous=current; *head=current; } } void push_back(ListElement_type **head, int number) { if(*head==NULL) { *head = (ListElement_type *)malloc(sizeof(ListElement_type)); (*head)->data = number; (*head)->previous = NULL; (*head)->next = NULL; }else { ListElement_type *current=*head; ListElement_type *new_element; while (current->next != NULL) { current = current->next; } current->next = (ListElement_type *)malloc(sizeof(ListElement_type)); current->next->data = number; current->next->previous=current; current->next->next = NULL; } } void push_by_index(ListElement_type **head, int number, int position) { if(position==0) push_front(head, number); else { if(position==list_size(*head)) push_back(head, number); else { ListElement_type *current=*head; ListElement_type *tmp; int i=0; while (current->next != NULL && i<position-1) { current = current->next; i++; } tmp=current->next; current->next=(ListElement_type *)malloc(sizeof(ListElement_type)); current->next->data=number; current->next->previous=current; tmp->previous=current->next; current->next->next=tmp; } } } void pop_front(ListElement_type **head) { if (*head!=NULL) { if((*head)->next==NULL) { *head=NULL; } else { ListElement_type *tmp; tmp=(*head)->next; free(*head); *head=tmp; (*head)->previous=NULL; } } } void pop_back(ListElement_type **head) { if((*head)->next==NULL) { *head=NULL; }else { ListElement_type *current=*head; while (current->next->next!= NULL) { current = current->next; } free(current->next); current->next=NULL; } } void pop_by_index(ListElement_type **head, int position) { if(position==0) pop_front(head); else { ListElement_type *current=*head; ListElement_type *tmp; int i=0; while (current->next != NULL && i<position-1) { current=current->next; i++; } tmp = current->next; current->next = tmp->next; current->next->previous=current; free(tmp); } } void show(ListElement_type *head) { printf("\n"); if(head==NULL) printf("List is empty"); else { ListElement_type *current=head; do { printf("%i", current->data); printf("\n"); current = current->next; }while (current != NULL); } } void show_reverse(ListElement_type *head) { printf("\n"); if(head==NULL) printf("List is empty"); else { ListElement_type *current=head; while (current->next != NULL) { current = current->next; //idziemy na koniec listy } do { printf("%i", current->data); printf("\n"); current = current->previous; }while(current!=NULL); } } int list_size(ListElement_type *head) { int counter=0; if(head==NULL) return counter; else { ListElement_type *current=head; do { counter++; current = current->next; }while (current != NULL); } return counter; } |
O innych strukturach danych, a także o algorytmach i technikach informatycznych przeczytasz w tej książce: