DOUBLY LINKED LIST PROGRAM : DOUBLY LINKED LIST (TAMBAH SIMPUL DI BELAKANG) # include <stdio.h> # include <stdlib.h> # include <malloc.h> struct tnode { int data; struct tnode *prev, *next; } ; int main() { struct tnode *head, *tail, *node; int bil; head = tail = NULL; while(scanf("%d", &bil) == 1) { node = (struct tnode*) malloc (sizeof(struct tnode)); node->data = bil; if (head == NULL) { node->prev = NULL; head = node; } else { tail->next = node; node->prev = tail; } tail = node; tail->next = NULL; } printf("depan ke belakang: "); node = head; while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("\nbelakang ke depan: "); node = tail; while (node != NULL) { printf("%d ", node->data); node = node->prev; } while (head != NULL) { node = head; head = head->next; free(node); } tail = NULL; return 1; }
Penjelasan Program A. Deklarasi tipe data simpul DLL struct tnode { int data; struct tnode *prev, *next; } ;
B. Deklarasi pointer penunjuk DLL int main() { struct tnode *head, *tail, *node; int bil; ... head = tail = NULL; ... }
C. Membentuk simpul baru (pertama) dan mengaitkannya ke DLL [simpul tambah di belakang] (c1) memesan sebuah simpul baru dengan menggunakan instruksi malloc() dan memasukkan data (nilai) ke simpul. (c2) memberi nilai NULL kepada field pointer prev. (c3) mengubah pointer head sehingga menunjuk ke simpul ini. (c4) mengubah pointer tail sehingga menunjuk ke simpul ini. scanf("%d", &bil); node = (struct tnode*) node->data = bil; if (head == NULL) { node->prev = NULL; head = node; tail = node; tail->next = NULL; }
malloc (sizeof(struct tnode)); /* c1 */ /* c2 */ /* c3 */ /* c4 */
C. Membentuk simpul ke-n dan mengaitkannya ke DLL [simpul tambah di belakang] (c5) memesan sebuah simpul baru dengan menggunakan instruksi malloc(), memasukkan data (nilai) ke simpul, dan memberi nilai NULL kepada field pointer next (c6) mengaitkan field pointer next pada simpul yang ditunjuk pointer tail ke simpul baru (c7) mengaitkan field pointer prev pada simpul baru ke simpul yang ditunjuk pointer tail (c8) mengubah pointer tail sehingga menunjuk simpul baru scanf("%d", &bil); node = (struct tnode*) malloc (sizeof(struct tnode)); node->data = bil; /* c5 */ if (head !== NULL) { tail->next = node; /* c6 */ node->prev = tail; /* c7 */ tail = node; /* c8 */ tail->next = NULL; }
D1. MENCETAK ISI DLL (Head to Tail) node = head; while (node != NULL) { printf("%d ", node->data); node = node->next; }
D2. MENCETAK ISI DLL (Tail to Head) node = tail; while (node != NULL) { printf("%d ", node->data); node = node->prev; }
E1. MENGHAPUS DLL (Head to Tail) while (head != NULL) { node = head; head = head->next; free(node); } tail = NULL;
E2. MENGHAPUS DLL (Tail to Head) while (tail != NULL) { node = tail; tail = tail->prev; free(node); } head = NULL;
PROGRAM : DOUBLY LINKED LIST (TAMBAH SIMPUL DI DEPAN) C. Membentuk simpul baru (pertama) dan mengaitkannya ke DLL [simpul tambah di depan]
scanf("%d", &bil); node = (struct tnode*) malloc (sizeof(struct tnode)); node->data = bil; /* c1 */ if (head == NULL) { node->next = NULL; /* c2 */ tail = node; /* c3 */ head = node; /* c4 */ head->prev = NULL; }
C. Membentuk simpul ke-n dan mengaitkannya ke DLL [simpul tambah di depan]
scanf("%d", &bil); node = (struct tnode*) malloc (sizeof(struct tnode)); node->data = bil; /* c5 */ if (head != NULL) { head->prev = node; /* c6 */ node->next = head; /* c7 */ head = node; /* c8 */ head->prev = NULL; }
Setelah semua simpul terbentuk maka bentukan simpul pertama dengan memberikan nilai NULL kepada field pointer prev. head->prev = NULL;
PROGRAM : DOUBLY LINKED LIST (TAMBAH SIMPUL DI DEPAN) # include <stdio.h> # include <stdlib.h> # include <malloc.h> struct tnode { int data; struct tnode *prev, *next; } ; int main() { struct tnode *head, *tail, *node; int bil; head = tail = NULL; while(scanf("%d", &bil) == 1) { node = (struct tnode*) malloc (sizeof(struct tnode)); node->data = bil; if (head == NULL) { tail = node; node->next = NULL; } else { head->prev = node; node->next = head; } head = node; head->prev = NULL; } printf("\ndepan ke belakang: "); node = head; while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("\nbelakang ke depan: "); node = tail; while (node != NULL) { printf("%d ", node->data); node = node->prev; } while (head != NULL) { node = head; head = head->next; free(node); } head = tail = NULL; return 1; }
CIRCULAR LINKED LIST Circular linked list adalah linked list (SLL maupun DLL) yang simpul terakhirnya dikaitkan dengan simpul pertama
PROGRAM : CIRCULAR SINGLY LINKED LIST # include <stdio.h> # include <stdlib.h> # include <malloc.h> struct tnode { int data; struct tnode* next; } ; struct tnode * isidata() { struct tnode *head, *tail, *node; int bil; head = NULL; printf("isi sejumlah bilangan, diakhiri dengan ctrl z\n"); while(scanf("%d", &bil) == 1) { node = (struct tnode*) malloc(sizeof(struct tnode)); node->data = bil; node->next = NULL; if (head == NULL) head = node; else tail->next = node; tail = node; } return head; } struct tnode * cariujung (struct tnode *head) { struct tnode *tail; tail = head; while(tail->next != NULL) tail = tail->next; tail->next = head; return tail; }
void tampildata(struct tnode *head) { int n; struct tnode *curr; printf("ingin tampilkan berapa data ? "); scanf("%d", &n); curr = head; while (n) { printf("%d ", curr->data); curr = curr->next; n--; } } void hapuslist(struct tnode **head, struct tnode **tail){ struct tnode *curr; while (*head != *tail) { curr = *head; *head = (*head)->next; free(curr); } free(*tail); *head = *tail = NULL; } int main() { struct tnode *head, *tail, *curr; head = isidata(); tail = cariujung(head); tampildata(head); hapuslist(&head, &tail); return 1; }
TOPIK LANJUTAN SINGLY LINKED LIST dan DOUBLY LINKED LIST (PENAMBAHAN SIMPUL SELAIN DI DEPAN dan BELAKANG) Biasanya berhubungan dengan “pencarian” simpul tertentu, misalnya simpul yang mengandung data tertentu. Diperlukan sebuah pointer bernama “current” untuk menunjuk simpul yang sedang ditunjuk. Pointer “current” akan ber”jalanjalan” dari posisi “head” sampai “tail” (tail adalah simpul paling belakang). Baik Singly maupun Doubly linked list memiliki operasi-operasi untuk : 1. Menghapus simpul dengan data yang dikehendaki 2. Meng-edit data pada simpul yang dikehendaki 3. Menambah simpul selain di depan dan belakang (maksudnya di tengah), biasanya penambahan simpul ini untuk penempatan simpul sesuai dengan urutan data simpul-simpul pada linked list yang telah ada, misalnya untuk pengurutan data. 4. Operasi no 1,2,3 pasti melibatkan operasi “search node”, karena harus menemukan dulu posisi simpul yang akan dihapus, atau simpul dengan data yang ingin di-edit, atau lokasi dimana simpul ingin ditambahkan.