Jenis Daftar Tertaut

Dalam tutorial ini, Anda akan mempelajari berbagai jenis daftar tertaut. Selain itu, Anda akan menemukan implementasi daftar tertaut di C.

Sebelum Anda mempelajari tentang tipe daftar tertaut, pastikan Anda mengetahui tentang Struktur Data LinkedList.

Ada tiga jenis umum dari Daftar Berantai.

  1. Daftar Tertaut Tunggal
  2. Daftar Tertaut Ganda
  3. Daftar Tertaut Melingkar

Daftar Tertaut Tunggal

Itu yang paling umum. Setiap node memiliki data dan penunjuk ke node berikutnya.

Daftar tunggal tertaut

Node direpresentasikan sebagai:

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

Daftar tertaut tunggal dengan tiga anggota dapat dibuat sebagai:

 /* 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;

Daftar Tertaut Ganda

Kami menambahkan pointer ke node sebelumnya dalam daftar tertaut ganda. Jadi, kita bisa pergi ke salah satu arah: maju atau mundur.

Daftar tertaut ganda

Sebuah node direpresentasikan sebagai

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

Daftar tiga anggota tertaut ganda dapat dibuat sebagai

 /* 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;

Daftar Tertaut Melingkar

Daftar tertaut melingkar adalah variasi dari daftar tertaut di mana elemen terakhir ditautkan ke elemen pertama. Ini membentuk lingkaran melingkar.

Daftar terkait melingkar

Daftar tertaut melingkar dapat ditautkan sendiri atau ditautkan dua kali.

  • untuk daftar tertaut tunggal, penunjuk berikutnya dari item terakhir menunjuk ke item pertama
  • Dalam daftar tertaut ganda, penunjuk prev dari item pertama menunjuk ke item terakhir juga.

Daftar tertaut tunggal melingkar tiga anggota dapat dibuat sebagai:

 /* 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;

Artikel yang menarik...