Single Linked List
Double Linked List (DLL)
Double Linked List is simliar to Single Linked List, the different is that instead of one pointer, Double Linked List as the name suggested possess 2 pointer instead of one, where the other point can point backward too in addition to pointing forward to the next value.
Double Linked List code example :
/* Node of a doubly linked list */
struct Node {
int data;
struct Node* next; // Pointer to next node in DLL
struct Node* prev; // Pointer to previous node in DLL
};
Apa itu Push?
Push adalah action memasukan suatu inputan ke dalam List, push diawali dengan membuat gerbong nya terlebih dahulu dimana gerbong ini akan diisi oleh inputan dan jumlah nya disesuai kan dengan memory alokasi yang kita butuhkan, nah setelah gerbong tersedia, baru lah kita dapat melakukan Push.
Ada berapa type Push?
Push memiliki 3 type : Depan, Belakang, dan Tengah.
Push depan adalah Push dimana inputan dimasukan tepat di depan head, setelah inputan diletakan di depan head, inputan tersebut disambungkan dengan inputan belakang nya dan menjadi head yang baru.
Push depan adalah Push dimana inputan dimasukan tepat di depan head, setelah inputan diletakan di depan head, inputan tersebut disambungkan dengan inputan belakang nya dan menjadi head yang baru.
Push belakang adalah Push dimana inputan dimasukan tepat di belakang tail, setelah inputan diletakan di belakang tail, inputan tersebut disambungkan dengan inputan didepan nya dan menjadi tail yang baru.
Push tengah adalah Push dimana inputan dimasukan diantara posisi head dan tail, untuk mengetahui dengan pasti posisi inputan maka dilakukan proses sorting terlebih dahulu, dimana inputan dibandingkan dengan value yang berada di head terlebih dahulu, sampai ditemukan angka yang lebih besar dari inputan, baru lah inputan dimasukan tepat di depan angka yang lebih besar dari diri nya dan dibelakang angka yang lebih kecil dari nya.
Double Linked List (DLL)
Double Linked List is simliar to Single Linked List, the different is that instead of one pointer, Double Linked List as the name suggested possess 2 pointer instead of one, where the other point can point backward too in addition to pointing forward to the next value.
Double Linked List code example :
/* Node of a doubly linked list */
struct Node {
int data;
struct Node* next; // Pointer to next node in DLL
struct Node* prev; // Pointer to previous node in DLL
};
Advantage over Single Linked List :
Advantages over singly linked list
1. A DLL can be traversed in both forward and backward direction.
2. The delete operation in DLL is more efficient if pointer to the node to be deleted is given.
3, We can quickly insert a new node before a given node.
In singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer.
Hash Table and Binary Search Tree
Hash table is a structure data which consist of table and function aiming to map unique key value from each record to hash, the record location is located within the table.
Hash Operation consist of :
Insert : Insert a value into the table
Find : Search a value in the table
Remove : Delete a value in the table
GetIterator : Return Iterator which check all value one by one.
The hash table uses an associative array data structure that associates a record with a unique key field in the form of a number (Hash) which is a representation of that record. For example, there is data in the form of a string to be stored in a hash table. The string is represented in a key field K.
The way to get this key field varies greatly, but the end result is a hash number that is used to determine the location of a record. This hash number is entered into the hash function and produces a record location index in the table.
k (x) = key field generator function (1)
h (x) = hash function (2)
Question : Does Blockchain technology employ hash table?
The answer is Yes as explained by the picture below, hash function connect the keys with the bucket.
Binary Search Tree
1. A DLL can be traversed in both forward and backward direction.
2. The delete operation in DLL is more efficient if pointer to the node to be deleted is given.
3, We can quickly insert a new node before a given node.
In singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer.
Hash Table and Binary Search Tree
Hash table is a structure data which consist of table and function aiming to map unique key value from each record to hash, the record location is located within the table.
Hash Operation consist of :
Insert : Insert a value into the table
Find : Search a value in the table
Remove : Delete a value in the table
GetIterator : Return Iterator which check all value one by one.
The hash table uses an associative array data structure that associates a record with a unique key field in the form of a number (Hash) which is a representation of that record. For example, there is data in the form of a string to be stored in a hash table. The string is represented in a key field K.
The way to get this key field varies greatly, but the end result is a hash number that is used to determine the location of a record. This hash number is entered into the hash function and produces a record location index in the table.
k (x) = key field generator function (1)
h (x) = hash function (2)
Question : Does Blockchain technology employ hash table?
The answer is Yes as explained by the picture below, hash function connect the keys with the bucket.
Binary Search Tree
Binary Search Tree (BST) is a node-based binary tree data structure which has the following properties:
- The left subtree of a node contains only nodes with keys lesser than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- The left and right subtree each must also be a binary search tree.
Binary Search Tree Example :
Tugas codingan:
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//create struct
struct item
{
char itemName[100];
int itemQ;
struct item *next;
struct item *prev;
}*head, *tail, *curr;
void input(char itemName[100], int itemQ)
{
//pesan memory
struct item *curr = (struct item*)malloc(sizeof(struct item));
//inisialisasi
strcpy(curr->itemName, itemName);
curr->itemQ = itemQ;
curr->next = NULL;
curr->prev = NULL;
//check if head is empty;
if(head==NULL)
{
head = tail = curr;
}
//insert head
else if(strcmp(head->itemName, itemName) >0)
{
curr->next = head;
head->prev = curr;
head = curr;
}
//insert tail
else if(strcmp(tail->itemName, itemName)<0)
{
tail->next = curr;
curr->prev = tail;
tail = curr;
}
//insert di tgh"
else
{
struct item *temp = head;
while(temp->next!=NULL && strcmp(curr->itemName, itemName)>0)
{
temp = temp->next;
}
curr->next = temp->next;
curr->prev = temp;
temp->next = curr;
curr->next->prev = curr;
}
}
void printAll()
{
curr = head;
printf("Nama Barang Jumlah");
printf("\n");
while(curr!=NULL)
{
printf("%s %d\n",curr->itemName, curr->itemQ);
curr=curr->next;
}
}
void pop(char *hapus)
{
curr = head;
//tidak ada link list
if(head==NULL)
{
printf("Tidak ada Linked List!\n");
}
//pophead
else if(head!=NULL && strcmp(curr->itemName, hapus)==0)
{
head = head->next;
free(curr);
}
else
{
struct item *temp = curr;
while(curr!=NULL && strcmp(curr->itemName, hapus)!=0)
{
temp = curr;
curr = curr->next;
}
if(curr==tail)
{
tail = temp;
}
else
{
curr->next->prev = temp;
}
temp->next=curr->next;
free(curr);
}
}
void update(char *key, int Q)
{
curr = head;
if(head==NULL)
{
printf("Tidak ada linked list!\n");
}
while(curr!=NULL)
{
if(strcmp(curr->itemName, key)==0)
{
curr->itemQ = Q;
}
if(curr->next!=NULL)
{
curr = curr->next;
}
else
{
return;
}
}
printf("Data does not exist in the list\n");
}
int main(int argc, char** argv) {
int choice;
char itemName[100];
int itemQ;
do{
printAll();
printf("1.Input barang untuk dibeli\n");
printf("2.Update jumlah barang\n");
printf("3.Delete barang\n");
printf("4.Check Out\n");
do{
printf("Masukan Inputan[1..4] : ");
scanf("%d",&choice);getchar();
}while(choice<1 || choice>4);
if(choice==1)
{
printf("Masukan nama item : ");
scanf("%s",&itemName);getchar();
printf("\n");
printf("Masukan Quantity nya : ");
scanf("%d", &itemQ);getchar();
printf("\n");
input(itemName, itemQ);
}
else if(choice==2)
{
printf("Masukan nama item yang ingin diupdate: ");
scanf("%s", &itemName);getchar();
printf("\n");
printf("Masukan Quantity baru nya :");
scanf("%d", &itemQ);getchar();
printf("\n");
update(itemName, itemQ);
}
else if(choice==3)
{
printf("Masukan nama item yang ingin dihapus : ");
scanf("%s", &itemName);getchar();
printf("\n");
pop(itemName);
}
else
{
printf("\n");
printf("Terima kasih sudah datang mengunjungi toko kami!\n");
return 0;
}
}while(choice!=4);
return 0;
}



Comments
Post a Comment