Sunday, March 15, 2020

Hash Table

Hash Table
Hash Table is a data structure that storing data in array. the purpose of hash table is to make the fast searching if you want to search for the data in the array with using a key to find a direct index.

                                          Picture 1.1,

Using hash table we calculate our data into the key with anyway and the key will refer to the index. that is why we can find our data with speed O(1). but there is some problem when we make hash table, it is collision. collision is when your data data is being stored in the same array with the another data because the key is the same.

How to fix a collision in our hash table? to fix the problem we can use linked list into our hash table.
to be exact you can see the picture number 1.1 it's already describe the solution if collision occur.


Binary Tree
Binary tree is implementation of linked list. in Binary tree we have 2 references which refer to left and right. the top of the node is called "root" and the bottom one is called "leaf". to find a data binary tree speed is O(n log(n)) but the data is already been sorted from the start.

The example of binary tree:


Block Chain
Block chain is linked every record (or box) with each other using public key and private key.
in current technology, block chain is used for making cryptocurrency such as bit coin. block chain can be implement at business at financial support example is Asset Management: Trade Processing and Settlement.

Source :
https://www.cs.cmu.edu/~adamchik/15-121/lectures/Trees/trees.html
https://www.researchgate.net/profile/Erin_Hastings/publication/228958917/figure/fig1/AS:340199050629120@1458121182977/An-example-of-mobile-objects-in-a-grid-a-hash-table-and-the-object-index.png
https://blockgeeks.com/guides/blockchain-applications/

Learn More :
https://www.youtube.com/watch?v=KyUTuwz_b7Q
https://blockgeeks.com/guides/blockchain-applications/

Wednesday, March 4, 2020

Linked List

Linked List memiliki banyak jenis namun yang akan dibahas pada kali ini adalah single linked list dan double linked list dengan menggunakan coding.

Dalam Linked List terdapat operasi yang simple yaitu Push (input) , Pop (delete), dan Print. Berikut merupakan coding untuk membuat push, pop , dan print dalam single linked list.

A. Push memiliki banyak jenis yaitu push head, push tail, push mid

1) Push head

  • Push head merupakan sebuah operasi untuk menambahkan data pada sebuah linked list dengan menambahkannya ke paling depan dan menggantikan posisi data pertama sebelumnya.

void push_head(int value){
struct Data *curr;
curr = (struct Data*) malloc(sizeof(struct Data));

curr->number = value; //masukan nilai v ke Curr
curr->next = NULL;
if(!head){ //head == NULL
head = tail = curr;
}
else{
curr->next = head;
head = curr;
}
}

2) Push tail

  • Push tail merupakan sebuah operasi untuk menambahkan data pada sebuah linked list dengan menambahkannya ke paling terakhir dan menggantikan posisi data terakhir sebelumnya.

void push_tail(int value){
struct Data *curr;
curr = (struct Data*) malloc(sizeof(struct Data));
curr->number = value;
curr->next = NULL;
if(!head){ //head == NULL
push_head(value);
}
else{
tail ->next = curr;
tail = curr;
}

}

3) Push mid

  • Push mid merupakan sebuah operasi untuk menambahkan data di tengah-tengah sebuah linked list 

void push_mid(int value){
struct Data *curr = head;
curr = (struct Data*) malloc (sizeof(struct Data));
if(!head) push_head(value);
else{
struct Data *temp = head;
while(temp->next->number < value) temp = temp->next;
curr->next = temp->next;
temp->next = curr;
}


}

B. Pop memiliki tiga jenis pop, pop head, pop tail , dan pop mid :

1)Pop head

  • Pop head adalah sebuah operasi untuk menghapus data paling pertama pada sebuah linked list dan memutuskan hubungannya.

void pop_head(){
struct Data *curr = head;
if(!head)return;
else if(head == tail){
free(head);
}
else{
head = head->next;
curr->next = NULL;
free(curr);
}

}

2) Pop Tail

  • Pop Tail merupakan sebuah operasi untuk menghapus data paling terakhir pada sebuah linked list dan memutuskan hubungannya.

void pop_tail(){
struct Data *curr = head;
if(!head)return;
else if(head == tail) free(tail);
else{
while(curr->next != tail){
curr = curr->next;
}
curr->next = NULL;
free(tail);
tail = curr;
}
}

3) Pop Mid

  • Pop mid merupakan suatu operasi untuk menghapus sebuah data pada linked list yang berada ditengah-tengah.

void pop_mid(int value){
struct Data *curr = head;
struct Data *temp = head;
if(!head) return;
else if(head == tail) pop_head();
else{
while(curr->number != value){
if(curr == NULL) return;
curr = curr->next;
}

while(temp->next->number != value){
temp = temp->next;
}
temp->next = curr->next;
curr->next = NULL;
free(curr);

}

}

C. print merupakan sebuah operasi untuk menampilkan sebuah data dan disini print merupakan operasi untuk menampilkan semua data.

void print_all(){
struct Data *curr = head;
while (curr != NULL){
printf("%d ", curr->number);
curr = curr->next;
}
}


Sekian materi-materi pada linked list, pada penggunaan double linked list hanya perlu menambah arah dari sebuah linked list dari yang hanya bisa menunjuk ke setelahnya menjadi ke setelahnya dan ke sebelumnya dan sedikit memperbaiki arah nya ketika melakukan push maupun pop.