Struktur Data Linear


struktur data linear adalah kumpulan komponen-komponen yang tersusun membentuk satu garis linear. Bila komponen-komponen ditambahkan (atau dikurangi), maka struktur-struktur tersebut berkembang (atau menyusut).
Contohnya :

a) Double linked list Circular .
Double Linked List Circular adalah linked list dengan menggunakan pointer, dimana setiap node memiliki 3 field,yaitu 1 field pointer yang menunjuk pointer berikutnya(next), 1 field menunjuk pointer sebelumnya (prev), sertasebuah field yang berisi data untuk node tersebut.

b) Double Linked List Non Circular ( DLLNC )
DLLNC "Double linked list non circular" adalah Double Linked List yang memiliki 2 buah pointer yaitu pointer next dan prev.
Pointer next menunjuk pada node setelahnya dan pointer prev menunjuk pada node sebelumnya.

Pengertian:
Double : artinya field pointer-nya dua buah dan dua arah, ke node sebelum dan sesudahnya.
Linked List : artinya node-node tersebut saling terhubung satu sama lain.
Non Circular : artinya pointer prev dan next-nya akan menunjuk pada NULL.
Setiap node pada linked list mempunyai field yang berisi data dan pointer ke node berikutnya & ke node sebelumnya
Untuk pembentukan node baru , mulanya pointer next dan prev akan menunjuk ke nilai NULL
Selanjutnya pointer prev akan menunjuk ke node sebelumnya , dan pointer next akan menunjuk ke node selanjutnya pada list.


Deklarasi dan node baru DLLNC

Deklarasi node
Dibuat dari struct berikut ini :
typedef struct TNode {
int data ;
TNode *next ;
Tnode * prev ;
};

Pembentukan node baru
Digunakan keyword new yang berarti mempersiapkan sebuah node baru berserta alokasi memorinya .
TNode * baru ;
baru = new TNode ;
baru ->data = databaru ;
baru ->next = NULL;
baru -> prev = NULL;


Contohnya:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
struct node{
struct node *previous;
int info;
struct node *next;
};
typedef struct node *simpul;

void main()
{
simpul baru, awal=NULL, akhir=NULL, temp;
int pilih;

do
{
cout("MENU\n");
cout("1. Isi depan\n");
cout("2. tampil\n");
cout("3. cari\n");
cout("4. hapus Depan\n");
cout("PILIH: ");
scanf("%d", &pilih);
switch(pilih)
{
case 1:
int data;
cout("Data Masuk: ");
scanf("%i", &data);
baru = (simpul) malloc(sizeof (struct node));
baru->info = data;
baru->next = NULL;
baru->previous = NULL;
if (awal == NULL)
{
awal = baru;
akhir = baru;
}
else
{
baru->next = awal;
awal->previous = baru;
awal = baru;
}
break;
case 2:
clrscr();
cout("Dari AWAL\n");
temp = awal;
while(temp!=NULL)
{
cout("%i ", temp->info);
temp = temp->next;
}
cout("\nDari AKHIR\n");
temp = akhir;
while(temp!=NULL)
{
cout("%i ", temp->info);
temp = temp->previous;
}
cout("\n");
break;
case 3:
clrscr();
int cari;
cout("Cari Angka: ");
scanf("%i", &cari);
temp = awal;
while((temp!=NULL)&&(temp->info!=cari))
{
temp = temp->next;
}
if(temp != NULL && temp->info == cari)
cout("Data Ditemukan");
else
cout("Data Tidak Ditemukan");
cout("\n");
break;
case 4:
clrscr();
temp = awal;
awal = awal->next;
if (awal != NULL)
awal->previous = NULL;
if (awal == NULL)
akhir = NULL;
free(temp);
break;
}
}while (pilih!=5);
}

C) Singly Linked List Circular

Hampir sama dengan singly linked list non circular, bahwa dibutuhkan sebuah kait untuk menghubungkan node-node data yang ada, dimana pada node terakhir atau tail yang semula menunjukkan NULL diganti dengan menunjuk ke kepala atau head.
Dimana inisialisasi senarai berkait tunggal sirkular menggunakan struc adalah sebagai
berikut:

                                    Deklarasi Singly Linked List Circular



Struct tnode
{
int data;
tnode *next;
};
void main()
{
head = new tnode;
                                                      head->next = head;
                                                      }


Menambah node dan membuat tail dari singly linked list circular

                                         Deklarasi penambahan node baru



Void main()
{
node = new tnode;
tail = new tnode;
node->next = head->next;
head->next = node;
tail = node;
}

                                            Menyisipkan Node baru :


       Deklarasi menyisipkan node baru menggunakan sintak berikut:

Void main()
{
node = new tnode;
node->next = head->next;
head->next = node;
}

                 Menghapus Node dari Singly Linked List Circular :




       Deklarasi menghapus node dari singly linked list circular, menggunakan sintaks berikut :

Void main()
{
     hapus = new tnode;
if( head != tail)
{
     hapus = head;
     head = head->next;
     tail->next = head;
     delete hapus;
}else
{
    head = NULL;
    tail = NULL;
   }
       }


Sumber : Antonius Rachmat C. Handout Struktur Data. Prodi Teknik Informatika
               Universitas Kristen Duta Wacana.
               Abdul Kadir. 1991. Pemrograman Dasar Turbo C untuk IBM PC. Yogyakarta:
               Penerbit Andi.





Contohnya:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <alloc.h>

int pil;
void pilih();
void buat_baru();
void tambah_belakang();
void tambah_depan();
void hapus_belakang();
void hapus_depan();
void tampil();

struct simpul
{
    char nim[8], nama [20];
    int umur;
    struct simpul *next;
} mhs, *baru, *awal=NULL, *akhir=NULL,*hapus,*bantu;


int main()
{
    do
    {
        clrscr();
        cout<<"MENU SINGLE LINKEDLIST"<<endl;
        cout<<"1. Tambah Depan"<<endl;
        cout<<"2. Tambah Belakang"<<endl;
        cout<<"3. Hapus Depan"<<endl;
        cout<<"4. Hapus Belakang"<<endl;
        cout<<"5. Tampilkan"<<endl;
        cout<<"6. Selesai"<<endl;
        cout<<"Pilihan Anda : ";
        cin>>pil;
        pilih();
    } while(pil!=6);
    return 0;
}

void pilih()
{
    if(pil==1)
        tambah_depan();
    else if(pil==2)
        tambah_belakang();
    else if(pil==3)
        hapus_depan();
     else if(pil==4)
        hapus_belakang();
      else if(pil==5)
        tampil();
    else
        cout<<"selesai";
}

void buat_baru()
{
    baru=(simpul*)malloc(sizeof(struct simpul));
    cout<<"input nim   : ";cin>>baru->nim;
    cout<<"input nama  : ";cin>>baru->nama;
    cout<<"input umur  : ";cin>>baru->umur;
    baru->next=NULL;
}

void tambah_belakang()
{
    buat_baru();
    if(awal==NULL)
    {
        awal=baru;
    }
    else
    {
        akhir->next=baru;
    }
    akhir=baru;
    akhir->next=NULL;
    cout<<endl<<endl;
    tampil();
}

void tambah_depan()
{
    buat_baru();
    if(awal==NULL)
    {
        awal=baru;
        akhir=baru;
        akhir->next=NULL;
    }
    else
    {
        baru->next=awal;
        awal=baru;
    }
    cout<<endl<<endl;
    tampil();
}

void hapus_depan()
{
    if (awal==NULL)
        cout<<"Kosong";
    else
    {
        hapus=awal;
        awal=awal->next;
        free(hapus);
    }
   cout<<endl<<endl;
    tampil();
}

void hapus_belakang()
{
    if (awal==NULL)
        cout<<"Kosong";
    else if(awal==akhir)
    {
         hapus=awal;
         awal=awal->next;
         free(hapus);
    }
    else
    {
        hapus=awal;
        while(hapus->next!=akhir)
             hapus=hapus->next;
        akhir=hapus;
        hapus=akhir->next;
        akhir->next=NULL;
        free(hapus);
    }
    cout<<endl<<endl;
    tampil();
}

void tampil()
{
     if (awal==NULL)
          cout<<"Kosong";
     else
     {
         bantu=awal;
         while(bantu!=NULL)
         {
            cout<<"nim : "<<bantu->nim;
            cout<<"  nama : "<<bantu->nama;
            cout<<"  umur : "<<bantu->umur<<endl;
            bantu=bantu->next;
         }
     }
     getch();
} 

D)          Single : artinya field pointer-nya hanya satu buah saja dan satu arah serta pada akhir node, pointernya menunjuk NULL
Linked List : artinya node-node tersebut saling terhubung satu sama lain

contoh: 
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <alloc.h>

int pil;
void pilih();
void buat_baru();
void tambah_belakang();
void tambah_depan();
void hapus_belakang();
void hapus_depan();
void tampil();

struct simpul
{
    char nim[8], nama [20];
    int umur;
    struct simpul *next;
} mhs, *baru, *awal=NULL, *akhir=NULL,*hapus,*bantu;


int main()
{
    do
    {
        clrscr();
        cout<<"MENU SINGLE LINKEDLIST"<<endl;
        cout<<"1. Tambah Depan"<<endl;
        cout<<"2. Tambah Belakang"<<endl;
        cout<<"3. Hapus Depan"<<endl;
        cout<<"4. Hapus Belakang"<<endl;
        cout<<"5. Tampilkan"<<endl;
        cout<<"6. Selesai"<<endl;
        cout<<"Pilihan Anda : ";
        cin>>pil;
        pilih();
    } while(pil!=6);
    return 0;
}

void pilih()
{
    if(pil==1)
        tambah_depan();
    else if(pil==2)
        tambah_belakang();
    else if(pil==3)
        hapus_depan();
     else if(pil==4)
        hapus_belakang();
      else if(pil==5)
        tampil();
    else
        cout<<"selesai";
}

void buat_baru()
{
    baru=(simpul*)malloc(sizeof(struct simpul));
    cout<<"input nim   : ";cin>>baru->nim;
    cout<<"input nama  : ";cin>>baru->nama;
    cout<<"input umur  : ";cin>>baru->umur;
    baru->next=NULL;
}

void tambah_belakang()
{
    buat_baru();
    if(awal==NULL)
    {
        awal=baru;
    }
    else
    {
        akhir->next=baru;
    }
    akhir=baru;
    akhir->next=NULL;
    cout<<endl<<endl;
    tampil();
}

void tambah_depan()
{
    buat_baru();
    if(awal==NULL)
    {
        awal=baru;
        akhir=baru;
        akhir->next=NULL;
    }
    else
    {
        baru->next=awal;
        awal=baru;
    }
    cout<<endl<<endl;
    tampil();
}

void hapus_depan()
{
    if (awal==NULL)
        cout<<"Kosong";
    else
    {
        hapus=awal;
        awal=awal->next;
        free(hapus);
    }
   cout<<endl<<endl;
    tampil();
}

void hapus_belakang()
{
    if (awal==NULL)
        cout<<"Kosong";
    else if(awal==akhir)
    {
         hapus=awal;
         awal=awal->next;
         free(hapus);
    }
    else
    {
        hapus=awal;
        while(hapus->next!=akhir)
             hapus=hapus->next;
        akhir=hapus;
        hapus=akhir->next;
        akhir->next=NULL;
        free(hapus);
    }
    cout<<endl<<endl;
    tampil();
}

void tampil()
{
     if (awal==NULL)
          cout<<"Kosong";
     else
     {
         bantu=awal;
         while(bantu!=NULL)
         {
            cout<<"nim : "<<bantu->nim;
            cout<<"  nama : "<<bantu->nama;
            cout<<"  umur : "<<bantu->umur<<endl;
            bantu=bantu->next;
         }
     }
     getch();
}



0 komentar:

Posting Komentar