Dvostruko povezani popis složena je vrsta povezanog popisa u kojem čvor sadrži pokazivač na prethodni, kao i na sljedeći čvor u nizu. Stoga se u dvostruko povezanoj listi čvor sastoji od tri dijela: podataka o čvoru, pokazivača na sljedeći čvor u nizu (sljedeći pokazivač), pokazivača na prethodni čvor (prethodni pokazivač). Uzorak čvora u dvostruko povezanoj listi prikazan je na slici.
Dvostruko povezana lista koja sadrži tri čvora koji u svom podatkovnom dijelu imaju brojeve od 1 do 3, prikazana je na sljedećoj slici.
U C-u, struktura čvora u dvostruko povezanoj listi može se dati kao:
struct node { struct node *prev; int data; struct node *next; }
The prev dio prvog čvora i Sljedeći dio posljednjeg čvora uvijek će sadržavati nulu koja označava kraj u svakom smjeru.
U pojedinačno povezanom popisu, mogli bismo kretati samo u jednom smjeru, jer svaki čvor sadrži adresu sljedećeg čvora i nema zapis o svojim prethodnim čvorovima. Međutim, dvostruko povezani popis nadmašuje ovo ograničenje jednostruko povezanog popisa. Zbog činjenice da svaki čvor popisa sadrži adresu svog prethodnog čvora, sve pojedinosti o prethodnom čvoru također možemo pronaći korištenjem prethodne adrese pohranjene unutar prethodnog dijela svakog čvora.
Memorijski prikaz dvostruko povezane liste
Memorijski prikaz dvostruko povezane liste prikazan je na sljedećoj slici. Općenito, dvostruko povezani popisi zauzimaju više prostora za svaki čvor i stoga uzrokuju opsežnije osnovne operacije kao što su umetanje i brisanje. Međutim, možemo lako manipulirati elementima popisa budući da popis održava pokazivače u oba smjera (naprijed i unatrag).
Na sljedećoj slici, prvi element popisa koji je, tj. 13, pohranjen na adresi 1. Pokazivač glave pokazuje na početnu adresu 1. Budući da je ovo prvi element koji se dodaje na popis, stoga prev popisa sadrži ništavan. Sljedeći čvor popisa nalazi se na adresi 4, stoga prvi čvor sadrži 4 u svom sljedećem pokazivaču.
Možemo prelaziti kroz popis na ovaj način sve dok ne pronađemo bilo koji čvor koji sadrži null ili -1 u svom sljedećem dijelu.
Operacije na dvostruko povezanoj listi
Stvaranje čvora
struct node { struct node *prev; int data; struct node *next; }; struct node *head;
Sve preostale operacije vezane uz dvostruko povezanu listu opisane su u sljedećoj tablici.
S N | Operacija | Opis |
---|---|---|
1 | Umetanje na početku | Dodavanje čvora u povezani popis na početku. |
2 | Umetanje na kraju | Dodavanje čvora u povezanu listu na kraj. |
3 | Umetanje nakon navedenog čvora | Dodavanje čvora u povezani popis nakon navedenog čvora. |
4 | Brisanje na početku | Uklanjanje čvora s početka liste |
5 | Brisanje na kraju | Uklanjanje čvora s kraja popisa. |
6 | Brisanje čvora koji je dao podatke | Uklanjanje čvora koji je prisutan neposredno nakon čvora koji sadrži dane podatke. |
7 | Traženje | Uspoređivanje podataka svakog čvora sa stavkom koju treba pretražiti i vraćanje lokacije stavke na popisu ako je pronađena stavka inače vraća null. |
8 | Prelaženje | Posjet svakom čvoru popisa barem jednom kako bi se izvršila određena operacija poput pretraživanja, sortiranja, prikaza itd. |
Program vođen izbornikom u C-u za implementaciju svih operacija dvostruko povezanog popisa
#include #include struct node { struct node *prev; struct node *next; int data; }; struct node *head; void insertion_beginning(); void insertion_last(); void insertion_specified(); void deletion_beginning(); void deletion_last(); void deletion_specified(); void display(); void search(); void main () { int choice =0; while(choice != 9) { printf(' *********Main Menu********* '); printf(' Choose one option from the following list ... '); printf(' =============================================== '); printf(' 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit '); printf(' Enter your choice? '); scanf(' %d',&choice); switch(choice) { case 1: insertion_beginning(); break; case 2: insertion_last(); break; case 3: insertion_specified(); break; case 4: deletion_beginning(); break; case 5: deletion_last(); break; case 6: deletion_specified(); break; case 7: search(); break; case 8: display(); break; case 9: exit(0); break; default: printf('Please enter valid choice..'); } } } void insertion_beginning() { struct node *ptr; int item; ptr = (struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { printf(' OVERFLOW'); } else { printf(' Enter Item value'); scanf('%d',&item); if(head==NULL) { ptr->next = NULL; ptr->prev=NULL; ptr->data=item; head=ptr; } else { ptr->data=item; ptr->prev=NULL; ptr->next = head; head->prev=ptr; head=ptr; } printf(' Node inserted '); } } void insertion_last() { struct node *ptr,*temp; int item; ptr = (struct node *) malloc(sizeof(struct node)); if(ptr == NULL) { printf(' OVERFLOW'); } else { printf(' Enter value'); scanf('%d',&item); ptr->data=item; if(head == NULL) { ptr->next = NULL; ptr->prev = NULL; head = ptr; } else { temp = head; while(temp->next!=NULL) { temp = temp->next; } temp->next = ptr; ptr ->prev=temp; ptr->next = NULL; } } printf(' node inserted '); } void insertion_specified() { struct node *ptr,*temp; int item,loc,i; ptr = (struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { printf(' OVERFLOW'); } else { temp=head; printf('Enter the location'); scanf('%d',&loc); for(i=0;inext; if(temp == NULL) { printf(' There are less than %d elements', loc); return; } } printf('Enter value'); scanf('%d',&item); ptr->data = item; ptr->next = temp->next; ptr -> prev = temp; temp->next = ptr; temp->next->prev=ptr; printf(' node inserted '); } } void deletion_beginning() { struct node *ptr; if(head == NULL) { printf(' UNDERFLOW'); } else if(head->next == NULL) { head = NULL; free(head); printf(' node deleted '); } else { ptr = head; head = head -> next; head -> prev = NULL; free(ptr); printf(' node deleted '); } } void deletion_last() { struct node *ptr; if(head == NULL) { printf(' UNDERFLOW'); } else if(head->next == NULL) { head = NULL; free(head); printf(' node deleted '); } else { ptr = head; if(ptr->next != NULL) { ptr = ptr -> next; } ptr -> prev -> next = NULL; free(ptr); printf(' node deleted '); } } void deletion_specified() { struct node *ptr, *temp; int val; printf(' Enter the data after which the node is to be deleted : '); scanf('%d', &val); ptr = head; while(ptr -> data != val) ptr = ptr -> next; if(ptr -> next == NULL) { printf(' Can't delete '); } else if(ptr -> next -> next == NULL) { ptr ->next = NULL; } else { temp = ptr -> next; ptr -> next = temp -> next; temp -> next -> prev = ptr; free(temp); printf(' node deleted '); } } void display() { struct node *ptr; printf(' printing values... '); ptr = head; while(ptr != NULL) { printf('%d ',ptr->data); ptr=ptr->next; } } void search() { struct node *ptr; int item,i=0,flag; ptr = head; if(ptr == NULL) { printf(' Empty List '); } else { printf(' Enter item which you want to search? '); scanf('%d',&item); while (ptr!=NULL) { if(ptr->data == item) { printf(' item found at location %d ',i+1); flag=0; break; } else { flag=1; } i++; ptr = ptr -> next; } if(flag==1) { printf(' Item not found '); } } }
Izlaz
*********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 1 Enter Item value12 Node inserted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 1 Enter Item value123 Node inserted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 1 Enter Item value1234 Node inserted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 1234 123 12 *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 2 Enter value89 node inserted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 3 Enter the location1 Enter value12345 node inserted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 1234 123 12345 12 89 *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 4 node deleted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 5 node deleted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 123 12345 *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 6 Enter the data after which the node is to be deleted : 123 *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 123 *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 7 Enter item which you want to search? 123 item found at location 1 *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 6 Enter the data after which the node is to be deleted : 123 Can't delete *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 9 Exited..