Tuesday, May 5, 2020

semester 2

1.Array

What is Array?

Array is a data structure that consist a group of elements and have a same data type,such as an integer or string.Array usually used in data structure to make searching and sorting more easier.

 

2.Pointer

What is Pointer?

A pointer is a programming language object that stores a memory address.Usually pointer used in C language and has a name and a value a associated with it.When we declare a variable,a specific block of memory in computer is allocated to hold the value. We use '&' as the address operator and '*' to dereferencing operator. 

 Introduction to Structure

Structure is a place to store data.Structure can store any type of data even when their type are different.Structure has an advantage to store data than array.Structure refers to a collection consisting of elements of heterogenous data type. Instantiation in structure objects is possible,unlike array that impossible in instantiation on object.

 3.Linked List

What is Linked List:

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image:
In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.
Why Linked List?
Arrays can be used to store linear data of similar types, but arrays have the following limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage.
2) Inserting a new element in an array of elements is expensive because the room has to be created for the new elements and to create room existing elements have to be shifted.
Advantages vs Disadvantages of Linked List?

Advantages of Linked List

Dynamic Data Structure
Linked list is a dynamic data structure so it can grow and shrink at runtime by allocating and deallocating memeory. So there is no need to give initial size of linked list.
Insertion and Deletion
Insertion and deletion of nodes are really easier. Unlike array here we don’t have to shift elements after insertion or deletion of an element. In linked list we just have to update the address present in next pointer of a node.
No Memory Wastage
As size of linked list can increase or decrease at run time so there is no memory wastage. In case of array there is lot of memory wastage, like if we declare an array of size 10 and store only 6 elements in it then space of 4 elements are wasted. There is no such problem in linked list as memory is allocated only when required.
Implementation
Data structures such as stack and queues can be easily implemented using linked list.

Disadvantages of Linked List

Memory Usage
More memory is required to store elements in linked list as compared to array. Because in linked list each node contains a pointer and it requires extra memory for itself.
Traversal
Elements or nodes traversal is difficult in linked list. We can not randomly access any element as we do in array by index. For example if we want to access a node at position n then we have to traverse all the nodes before it. So, time required to access a node is large.
Reverse Traversing
In linked list reverse traversing is really difficult. In case of doubly linked list its easier but extra memory is required for back pointer hence wastage of memory.

There are types of Linked List, which is :

- Singly Linked List
It is the most common. Each node has data and a pointer the next code
- Doubly Linked List
Add a pointer to the previous node in a doubly-linked list. Thus, we can go in either direction, forward and backward
- Circular Singly Linked List
The last node contains a pointer to the first node. We can have a circular singly linked list as well as a circular doubly linked list. There is no storing of NULL values in the list
- Circular Doubly Linked List
It is similar to a circular single linked list, but the total pointer in each node here is two pointers. The more complex type of data structure in which a node contains pointers to its previous node as well to the next node. The circular doubly linked list doesn't contain NULL in any of the nodes. The last node of the list contains the address of the first node of the list. The first node of the list also contain address of the last node in its previous pointer.

4.Binary Tree

APA ITU BINARY TREE

Seperti kata binary yaitu dua,binary tree adalah pohon yang memiliki dua cabang.Pohon ini digunakan dalam mengatur data agar mudah dicari dan dihapus.

APA FUNGSI BINARY TREE DAN CONTOH BINARY TREE

Binary tree digunakan untuk mengurutkan jumlah data yang banyak agar dapat dicari dan dihapus dengan mudah.Binary tree merupakan teori dasar untuk memudahkan penyusunan data agar lebih tertata.Contoh binary tree seperti ini:
Image result for binary tree

  
Binary tree diatas dibuat dengan program jika lebih besar dari atasnya akan berpindah ke kanan dan jika lebih kecil dari angka yang diatasnya akan berpindah ke kiri.
Misalkan A adalah 5,B akan berisikan angka yang lebih kecil dari 5 dan C akan berisikan angka jika lebih besar dari 5.Begitu pula B dan C sistemnya akan sama dengan A

3.Hash Table

Hashing adalah teknik yang digunakan untuk secara unik mengidentifikasi objek tertentu dari sekelompok objek serupa.

Hash Function


Fungsi hash adalah fungsi apa pun yang dapat digunakan untuk memetakan kumpulan data dari ukuran arbitrer ke kumpulan data dengan ukuran tetap, yang termasuk dalam tabel hash. Nilai yang dikembalikan oleh fungsi hash disebut nilai hash, kode hash, jumlah hash, atau hanya hash.

Untuk mencapai mekanisme hashing yang baik, penting untuk memiliki fungsi hash yang baik dengan persyaratan dasar berikut:

1. Mudah dikomputasi: Seharusnya mudah dikomputasi dan tidak harus menjadi algoritma itu sendiri.
2. Distribusi seragam: Ini harus menyediakan distribusi seragam di seluruh tabel hash dan tidak boleh menghasilkan pengelompokan.
3. Kurang tabrakan: Tabrakan terjadi ketika pasangan elemen dipetakan ke nilai hash yang sama. Ini harus dihindari.


Hash Table

enter image description here

Tabel hash adalah struktur data yang digunakan untuk menyimpan pasangan kunci / nilai. Ini menggunakan fungsi hash untuk menghitung indeks ke dalam array di mana elemen akan dimasukkan atau dicari. Dengan menggunakan fungsi hash yang baik, hashing dapat bekerja dengan baik. Di bawah asumsi yang masuk akal, waktu rata-rata yang diperlukan untuk mencari elemen dalam tabel hash adalah O (1)


Here is the code example project of market menu: 
#include<string.h>
#include<stdio.h>
#include<stdlib.h>

struct Data {
    char product[100];
    int number;
    int price;
    struct Data *next, *prev;
   
}*head,*tail,*cur;

 void Add(int prc,char name[], int quantity){
     cur=(struct Data*)malloc(sizeof(struct Data));
    strcpy(cur->product,name);
    cur->number=quantity;
    cur->price=prc;
   
     if(head==NULL){
         head=tail=cur;
        
    }
    else{
        if(strcmp(name,head->product)<0){
            cur->next=head;
            head->prev=cur;
            head=cur;
        }
        else if(strcmp(name,tail->product)>0){
            tail->next=cur;
            cur->prev=tail;
            tail=tail->next;
        }
        else{
            struct Data*temp;
            temp=head;
            while((strcmp(name,temp->product)>0)&&temp->next!=NULL){
                temp=temp->next;
               
            }
            cur->next=temp;
            temp->prev=cur->prev;
            temp->prev=cur;
            temp=cur->prev;
            temp->next= cur;
           
        }
   
    }
    tail->next= head->prev=NULL;
    
 }

void Print(){
    int i=0;
    cur=head;
    int total=0;
        printf("==========================================================\n");
        printf("|                      Product List                      |\n");
        printf("==========================================================\n");
        printf("|No.|    Name    |   Price   |   Quantity   |    Total   |\n");
        printf("==========================================================\n");       

    while(cur!=NULL){
        i++;
        printf("|%-3d|%-12s|Rp %-8d|%-14d|Rp %-9d|\n",i,cur->product,cur->price, cur->number,cur->number*cur->price);
       
        cur=cur->next;
    }
}
void Remove(char name[]){
   
    cur=head;
    if(head==NULL){
        printf("Data kosong,tidak ada yang bisa dihapus\n");
    }
    else{

        while(strcmp(cur->product,name)!=0 && cur->next !=NULL){
           
            cur=cur->next;
        }
        if(strcmp(cur->product,name)==0){
            struct Data* temp;
            temp=head;
            if(cur==head){
                head=head->next;
                head->prev=NULL;
                free(cur);
            }
            else if(cur==tail){
                tail=tail->prev;
                tail->next=NULL;
                free(tail);
            }
            else {
                temp=cur->next;
                temp->prev=cur->prev;
                temp=cur->prev;
                temp->next=cur->next;
                free(cur);
               
            }
        }
        else{
            printf(" Produk itu tidak ditemukan\n");
        }
    }
}
void Edit(char name[], int quantity){
    cur=head;
    if(head==NULL){
        printf("Data kosong,tidak ada yang bisa diedit\n");
    }
    else{

        while(strcmp(cur->product,name)!=0 && cur->next !=NULL){
           
            cur=cur->next;
        }
        if(strcmp(cur->product,name)==0){
            cur->number=quantity;
        }
        else{
            printf("Product cannot be found!\n");
        }
    }
   
}
int main(){
    int x=0;
    char name[100];
    int quantity=0;
    int prc;
    do{
        printf("\n");
        printf("==========================\n");
        printf("| Dreammers Market        |\n");
        printf("==========================\n");
        printf("| 1. Add product          |\n");
        printf("| 2. Edit product         |\n");
        printf("| 3. remove product       |\n");
        printf("| 4. Exit program         |\n");
        printf("==========================\n");
        printf("|Please enter a number :  |\n");
        scanf("%d",&x);getchar();

       
        if(x==1){
            printf("\nPlease input the product: ");
            scanf("%[^\n]",name);getchar();
            printf("Please input the quantity: ");
            scanf("%d",&quantity);getchar();
            prc=rand()%1000*1000;
            Add(prc,name,quantity);   
            Print();   
        }
        else if(x ==2){
            printf("\nInput to edit product: ");
            scanf("%[^\n]",name);getchar();
            printf("Input the correct quantity: ");
            scanf("%d",&quantity);getchar();   
            Edit(name,quantity);
                    Print();   

        }
        else if(x==3){
            printf("Input to delete product: ");
            scanf("%[^\n]",name);getchar();
            Remove(name);
            Print();   

        }
    }while(x!=4);
    Print();
    printf("==========================================================\n\n");

    printf("        TOTAL: KINDNESS IS FREE" );
   
    return 0;
}

No comments:

Post a Comment