Selasa, 27 Februari 2018

Pertemuan2.likedlist-2101660790-ihsan


alah satu jenis struktur data adalah linked list. Dalam bahasa melayunya, linked list adalah data yang bersambungan antara satu sama lain. Setiap elemen linked list dipanggil node. Manakala setiap node terbahagi kepada dua bahagian iaitu bahagian data yang akan menyimpan data dan bahagian sambungan (link) yang berfungsi sebagai penyambung elemen-elemen di dalam linked list ini.
Di dalam bahagian sambungan, biasanya ia terdiri dari hanya satu pembolehubah pointer yang akan merujuk kepada address (lokasi) node seterusnya. Linked list ini dipanggil single-linked linked list. Untuk double-linked linked list, terdapat dua pointer, di mana satu merujuk kepada address node seterusnya dan satu lagi merujuk kepada address node sebelumnya. 
Node biasanya adalah struktur C untuk menyimpan lebih daripada satu jenis pemboleh ubah Salah satu contoh mudah sebuah node adalah:
struct tnode { 
  int data; 
  struct tnode *next; 
};





Di mana data merujuk kepada bahagian data dan next merujuk kepada bahagian link. Jadi dengan ini kita boleh bayangkan yang kita membuat satu linked list dengan 3 node seperti contoh di bawah.
#include 
#include 
 
struct tnode { 
  int data; 
  struct tnode *next; 
};
 
int main(int argc, char *argv[])
{
  struct tnode *node = (struct tnode *)malloc(sizeof(struct tnode));
  
  // menyambung node
  node->data = 100;
  node->next = (struct tnode *)malloc(sizeof(struct tnode));
  
  printf("Data node1 %d \n", node->data);
  printf("Link node1 %p \n", node->next);
  printf("Lokasi node1 %p \n\n", node);
  
  node->next->data = 200;
  node->next->next = (struct tnode *)malloc(sizeof(struct tnode));
  
  printf("Data node2 %d \n", node->next->data);
  printf("Link node2 %p \n", node->next->next);
  printf("Lokasi node2 %p \n\n", node->next);
  
  node->next->next->data = 300;
  node->next->next->next = NULL;
  
  printf("Data node3 %d \n", node->next->next->data);
  printf("Link node3 %p \n", node->next->next->next);
  printf("Lokasi node3 %p \n\n", node->next->next);
  
  system("PAUSE"); 
  return 0;
}
Seperti yang boleh digambarkan seperti di bawah.
http://www.tutorialmelayu.com/wp-content/uploads/2011/10/c013.jpg
Berikut adalah hasil output kod di atas setelah dijalankan menggunakan Dev-C++.
http://www.tutorialmelayu.com/wp-content/uploads/2011/10/c014.jpg
Jadi setelah melihat bagaimana linked list berfungsi, mari kita lihat bagaimana membina satu program linked list yang lengkap. Dalam program linked list, saya membayangkan beberapa fungsi yang boleh membantu kita memanipulasi linked list yang dihasilkan.
Membina node baru
Fungsi ini bertujuan untuk memperuntukkan (allocate) ruang memori untuk node baru dan memasukkan data ke dalamnya.
struct tnode * createnode(int data){
    struct tnode * node;
    node = (struct tnode *)malloc(sizeof(struct tnode));
    node->next = NULL;
    node->data = data;
    return node;       
}
Menambah node
Fungsi ini akan menggunakan fungsi createnode di atas dan kemudiannya memasukkan node tersebut ke dalam linked list yang sedia ada di posisi yang diberi.
void tambahnode(int data, int pos)
{
    int i;
    struct tnode *temp, *prev, *current;
 
    current = head;
 
    if(pos > (length()+1) || pos <= 0){
           printf("Posisi tidak sah.\n ");
    }
    else {
         if (pos == 1){
            temp = createnode(data);
            temp->next = head;
            head = temp;
         }
         else{
            for(i=1;inext;
            }
 
            temp = createnode(data);
            prev->next = temp;
            temp->next = current;
        }
    }
}
Memaparkan data node
Fungsi ini digunakan untuk memaparkan kesemua data yang terkandung di dalam node-node yang ada.
void paparnode(void){
     struct tnode * current = head;
 
     while(current != NULL) {
          printf("Data node %d \n", current->data);
          printf("Link node %p \n", current->next);
          printf("Lokasi node %p \n\n", current);
          current = current->next;
     }     
}
Mengira jumlah node
Fungsi ini bertujuan untuk memberikan jumlah node yang berada di dalam linked list.
int length(void) {
    struct tnode* current = head;
    int count = 0;
 
    while (current != NULL) {
          count++;
          current = current->next;
    }
    return count;
}
Membuang node
Fungsi ini bertujuan untuk membuang node di posisi yang ditetapkan.
http://www.tutorialmelayu.com/wp-content/uploads/2011/10/c015.jpg
void deletenode(int pos){
     struct tnode * current = head;
     struct tnode * prev;
     int i;
  
     if(pos > (length()) || pos <= 0){
            printf("Posisi tidak sah.\n");
     } 
     else {
          if(pos == 1){
              head = current->next;
              free(current);     
          } 
          else{
               for(i=1; inext;
               }      
               prev->next = current->next;
               free(current);
          }
     }    
}
Jika kita gabungkan kesemua fungsi-fungsi di atas, kita sudahpun mempunyai satu program linked list yang lengkap.
#include 
#include 
 
struct tnode { 
    int data; 
    struct tnode *next; 
} *head;
 
struct tnode * createnode(int data){
    struct tnode * node;
    node = (struct tnode *)malloc(sizeof(struct tnode));
    node->next = NULL;
    node->data = data;
    return node;       
}
 
void tambahnode(int data, int pos)
{
    int i;
    struct tnode *temp, *prev, *current;
 
    current = head;
 
    if(pos > (length()+1) || pos <= 0){
           printf("Posisi tidak sah.\n ");
    }
    else {
         if (pos == 1){
            temp = createnode(data);
            temp->next = head;
            head = temp;
         }
         else{
            for(i=1;inext;
            }
 
            temp = createnode(data);
            prev->next = temp;
            temp->next = current;
        }
    }
 }
 
void paparnode(void){
     struct tnode * current = head;
     printf("*****************************\n");
     while(current != NULL) {
          printf("Data node %d \n", current->data);
          printf("Link node %p \n", current->next);
          printf("Lokasi node %p \n\n", current);
          current = current->next;
     }     
}
 
int length(void) {
    struct tnode* current = head;
    int count = 0;
    while (current != NULL) {
          count++;
          current = current->next;
    }
    return count;
}
 
void deletenode(int pos){
     struct tnode * current = head;
     struct tnode * prev;
     int i;
  
     if(pos > (length()) || pos <= 0){
            printf("Posisi tidak sah.\n");
     } 
     else {
          if(pos == 1){
              head = current->next;
              free(current);     
          } 
          else{
               for(i=1; inext;
               }      
               prev->next = current->next;
               free(current);
          }
     }    
}
 
int main(int argc, char *argv[])
{
    head = NULL;   
    
    tambahnode(100, 1);
    tambahnode(200, 1);
    tambahnode(300, 1);
    tambahnode(400, length() + 1);
 
    paparnode();
    deletenode(3);
    paparnode();
 
    system("PAUSE");    
    return 0;
}


Selasa, 20 Februari 2018

1-array&pointer-2101660790-ihsan


Pinter and array
Array adalah kumpulan dari nilai-nilai data bertipe sama dalam urutan tertentu yang menggunakan sebuah nama yang sama.
Nilai-nilai data di suatu larik disebut dengan elemen-elemen larik.
Letak urutan dari suatu elemen larik ditunjukkan oleh suatu subscript atau suatu index.
Menurut dimensinya, array dapat dibedakan menjadi :
1.      Array berdimensi satu
o       Setiap elemen array dapat diakses melalui index
o       Index array secara default dimulai dari 0
      Declaration:
      int arr[5];
      Accessing:
      arr[0] = 7;
      arr[1] = 2;
      arr[2] = 13;
      arr[3] = 13;
      arr[4] = 13;
2.      Array berdimensi dua
          Array dua dimensi merupakan array yang terdiri dari m buah baris dan n buah buah kolom. Bentuknya dapat berupa matriks atau tabel.
      Declaration:
      int arr[3][6];
      Accessing:
      arr[0][2] = 2;
      arr[2][1] = 9;
      arr[1][5] = 13;
      arr[2][4] = 10;

3.      Array multidimensi
        Array multidimensi merupakan array yang mempunyai ukuran lebih dari dua. Bentuk pendeklarasian array multidimensi sama saja dengan deklarasi array dimensi satu maupun dimensi dua.
      Declaration:
      int arr[4][3][7][10];
      Accessing:
      arr[0][2][2][9]= 2;
      arr[2][1][6][0]= 9;
      arr[3][0][0][6]= 13;
      arr[2][1][3][8]= 10;
Operations in Array
There are a number of operations that can be performed
on arrays. They are:
         Traversal
         Insertion
         Searching
         Deletion
         Merging
         Sorting

Pointer

·         Pointer adalah suatu variabel yang menunjuk ke alamat memory variabel yang lainnya.
·         Suatu pointer bukan berisi dengan suatu nilai data seperti halnya pada variabel biasa, variabel pointer berisi dengan suatu alamat.
·         Untuk mendeklarasikan variabel pointer kita menggunakan tanda asterik / bintang (*) didepan variabel yang di deklarasikan pada tipe data tertentu.
Contoh :
int a  = 10;
int *p = &a;
printf( “%d\n”, *p );
a  = 17;
*p = 20;
printf( “%d\n”, a );

DATA Type
      Data Type is a collection of objects and a set of operations that act on those objects.
      For example, the data type int consists of:
         objects             : 0, +1, -1, +2, -2, etc
         operations        : +, -, *, /, %, etc
      Example of predefined data types are int, char, float.

Abstract Data Type
      Abstract Data Type (ADT) is a data type that is organized in such a way that the specification of the objects and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operations.
      C/C++ has a concept called class and struct which assist the programmer in implementing abstract data type.
      Supposed we want to create an ADT of natural number which has integer number as objects and several functions as operation.
      structure Number is
            objects : an integer x
            functions         :
                        bool is_zero()  if ( x == 0 ) return TRUE else return FALSE
                        bool equal(y)   if ( x == y ) return TRUE else return FALSE
                        void set(y)       x = y
                        void add(y)     x = x + y
                        int get ()                      return x

nama: ihsan 
nim: 2101660790