Tipuri de liste legate

În acest tutorial, veți afla diferite tipuri de liste legate. De asemenea, veți găsi implementarea listei legate în C.

Înainte de a afla despre tipul listei conectate, asigurați-vă că știți despre structura de date LinkedList.

Există trei tipuri comune de Listă legată.

  1. Listă legată individual
  2. Lista dublă legată
  3. Listă circulară legată

Listă legată individual

Este cel mai frecvent. Fiecare nod are date și un pointer către următorul nod.

Listă legată individual

Nodul este reprezentat ca:

 struct node ( int data; struct node *next; )

O listă cu trei membri conectată individual poate fi creată ca:

 /* Initialize nodes */ struct node *head; struct node *one = NULL; struct node *two = NULL; struct node *three = NULL; /* Allocate memory */ one = malloc(sizeof(struct node)); two = malloc(sizeof(struct node)); three = malloc(sizeof(struct node)); /* Assign data values */ one->data = 1; two->data = 2; three->data = 3; /* Connect nodes */ one->next = two; two->next = three; three->next = NULL; /* Save address of first node in head */ head = one;

Lista dublă legată

Adăugăm un indicator la nodul anterior într-o listă dublă legată. Astfel, putem merge în ambele direcții: înainte sau înapoi.

Lista dublă legată

Un nod este reprezentat ca

 struct node ( int data; struct node *next; struct node *prev; )

Se poate crea o listă cu trei membri dublă legată ca

 /* Initialize nodes */ struct node *head; struct node *one = NULL; struct node *two = NULL; struct node *three = NULL; /* Allocate memory */ one = malloc(sizeof(struct node)); two = malloc(sizeof(struct node)); three = malloc(sizeof(struct node)); /* Assign data values */ one->data = 1; two->data = 2; three->data = 3; /* Connect nodes */ one->next = two; one->prev = NULL; two->next = three; two->prev = one; three->next = NULL; three->prev = two; /* Save address of first node in head */ head = one;

Listă circulară legată

O listă circulară legată este o variație a unei liste legate în care ultimul element este legat de primul element. Aceasta formează o buclă circulară.

Listă circulară legată

O listă circulară poate fi legată individual sau dublă.

  • pentru lista legată individual, următorul indicator al ultimului element indică primul articol
  • În lista dublu legată, indicatorul anterior al primului element indică și ultimul element.

O listă circulară cu trei membri, legată individual, poate fi creată ca:

 /* Initialize nodes */ struct node *head; struct node *one = NULL; struct node *two = NULL; struct node *three = NULL; /* Allocate memory */ one = malloc(sizeof(struct node)); two = malloc(sizeof(struct node)); three = malloc(sizeof(struct node)); /* Assign data values */ one->data = 1; two->data = 2; three->data = 3; /* Connect nodes */ one->next = two; two->next = three; three->next = one; /* Save address of first node in head */ head = one;

Articole interesante...