Single Linked List dan Double Linked List


I.Pointer

Pointer merupakan suatu tipe data yang digunakan untuk mengambil suatu address/alamat.Suatu variabel akan disimpan dalam memori RAM (Random Access Memory) dimana isi dari variabel tersebut akan disimpan sesuai dengan alamat dari variabel tersebut.Untuk mengakses data variabel tersebut,dibutuhkan pointer yang biasa ditandakan dengan aestrisk(*).

A.Single Linked List


Single Linked List merupakan struktur data yang terhubung satu sama lain melalui pointer dimana pointer tersebut akan menunjuk ke struktur data / node lainnya. Ilustrasinya sebagai berikut :



Image result for single linked list
Contoh Single Linked List.

disebut dengan Single Linked List karena hanya memiliki single link untuk menunjuk alamat ke struktur data / node selanjutnya. Node terakhir menunjuk alamat ke NULL dimana NULL digunakan sebagai kondisi berhenti saat pembacaan isi dari linked list tersebut. 

Image result for insert node in front of the list single linked list animation.gif
Memasukan node ke awal barisan.
Berikut Implementasi Single Linked List dalam suatu program C :

#include<stdio.h>
#include<stdlib.h>
struct Node{
int value;
struct Node *next;
};
struct Node *head;
void printList()
{
struct Node *temp = head;

while(temp != NULL)
{
if(temp->next == NULL)
{
printf("%d\n",temp->value);
}
else
printf("%d->",temp->value);
temp = temp->next;
}
}
void insertAwal(int n)
{
struct Node* temp = (struct Node*) malloc(sizeof(struct Node));
temp->value = n;
temp->next = head;
head = temp;
}
int main ()
{
head = NULL;
struct Node *node = (struct Node*) malloc(sizeof(struct Node));
int n;
int x;
int y;
scanf("%d",&n);
for(int i = 0 ; i < n ; i ++)
{
scanf("%d",&x);
insertAwal(x);
}
printList();
}

Sampel Input :
4
5 3 7 2

Output:
5->3->7->2

Circular Single List terjadi saat node pertama yang diinput mengambil alamat node yang paling terakhir diinput.Sehingga konsep nya seperti berikut:


Image result for circular linked list
Contoh Circular Single List


Untuk lebih jelasnya,berikut penjelasan yang lebih lanjut :
https://www.tutorialspoint.com/data_structures_algorithms/circular_linked_list_algorithm.htm



Untuk mempermudah pemahaman tentang Linked List,berikut video untuk memahami lebih lanjut linked list dan implementasinya
https://www.youtube.com/watch?v=VOpjAHCee7c - Understanding and Implementing Linked List in C and Java

B.DOUBLY LINKED LIST


Doubly Linked List mirip dengan Singly Linked List namun perbedaan nya adalah Doubly Linked List mengambil 2 alamat dari struktur data/ node lainnya (yaitu prev & next).Untuk penjelasan yang lebih jelas dapat dilihat dari,https://www.geeksforgeeks.org/doubly-linked-list/

Image result for doubly linked list
Contoh Doubly Linked List

Contoh double linked list : Insertion.



dll_add_front
Memasukan data ke linked list.Sumber : https://media.geeksforgeeks.org/wp-content/cdn-uploads/gq/2014/03/DLL_add_front1.png
Contoh implementasinya dalam C :
#include<stdio.h>
#include<stdlib.h>
struct Node{
int value;
struct Node *next,*prev;
};
struct Node *head;
struct Node *tail;
void insertToFront(int value){
struct Node *temp = (struct Node*) malloc(sizeof(struct Node));
temp->value = value;
temp->next = head;
if(head != NULL){
head->prev = temp;
}
head = temp;
}
void printList()
{
struct Node *temp = head;
while(temp != NULL)
{
if(temp->next == NULL)
{
printf("%d\n",temp->value);
}
else
printf("%d<->",temp->value);
temp = temp->next;
}
}
int main ()
{
head = NULL;
tail = NULL;
int x;
scanf("%d",&n);
for(int i = 0 ; i < n ; i ++)
{
scanf("%d",&x);
insertToFront(x);
}
printList();
}

Sampel Input :
4
5 3 7 2 
Output:
2 ⬌ 7 ⬌ 3 ⬌ 5

Circular Double Linked List

Circular double linked list mirip circular single linked list namun, setiap node menyimpan 2 alamat dari node lainnya.
Image result for circular doubly linked list
Contoh Circular Double Linked List.source =https://media.geeksforgeeks.org/wp-content/uploads/Circular-doubly-linked-list.png


Untuk mempermudah pemahaman tentang Doubly Linked List,berikut video untuk memahami lebih lanjut linked list dan implementasinya:

-https://www.youtube.com/watch?v=JdQeNxWCguQ&t=78s - Data Structures: Introduction to Doubly Linked List

Comments

Popular posts from this blog

AVL TREE

Rangkuman Semester 2