Sabtu, 21 Juni 2014

link list dua arah



PRQKTIKUM MQNDIRI
LINK LIST DUA ARAH
Nama :Isni Fachri Rrizal
NIM  : F1D013049
A. Tujuan :
1. Mengimplementasikan Link LIst dua arah dalam bahasa pemrograman.
2. Membuat Link List dua arah dan pengaplikasiannya.
B. Permasalahan :
1. Membuat program dengan Link List dua arah
C. gambar Desain Program



D. Penjelasan Desain
Operasi
Deskripsi
C/C++
1.Create node         
Mendeklarasikan / membuat link list / node                                     
struct node {
double data;
node * next;
node * back;
};
2.Set size               
Pendeklarasian size node dan memberikan nilai 0
int size=0;
3.add node front
Menambahkan node di bagian kepala link list  
tmp->next=head;
head->back=tmp;
head=tmp;
size++;
4.add node back
Menambahkan node di bagian ekor link list  
tail->next=tmp;
tmp->back=tail;
tail=tmp;
size++;
5.remove back
Menghapus node di bagian ekor link list
node*tmp=tail;
   tail=tmp->back;
   tail->next=NULL;
   tmp->back=NULL;
   delete tmp;
   size--;
6. remove front
Menghapus node di bagian kepala link list
node*tmp=head;
   head=tmp->next;
   tmp->next=NULL;
   head->back=NULL;
   delete tmp;
   size--;
7. remove all
Menghapus semua node dalam link list
while(size!=0){
node*tmp=head;
if(head->next==NULL){
delete tmp;
size--;
else{
head=head->next;
tmp->next=NULL;
head->back=NULL;
delete tmp;
size--;
}
}
8. insert node
Memasukan sebuah node
tmp->next=search->next;
search->next->back=tmp;
search->next=tmp;
tmp->back=search;
size++;
9. delete node betwen
Menghapus node di tengah-tengah link list
node*tmp=search->next;
search->next=tmp->next;
tmp->next->back=search;
delete tmp;
size--;
11. swap node
Menukar nilai antara dua buah node
search1->data=key2;      search2->data=key1;
12. replace node
Mengganti nilai pada node
if(found){
search->data=dt;
}

E. Kode Program
#include <iostream>
#include <conio>

class LL2A{
      private :
      struct node{
            double data;
         node *next;
         node *back;
      };
      int size;
      node *head;
      node *tail;
   public :
      void LL(){size=0; head=NULL; tail=NULL;};
      void addfront(double dt);
      void addback(double dt);
      void insertnode(double dt, double key);
      void replacenode(double dt, double key);
      void swapingnode(double key1, double key2);
      void removefront();
      void removeback();
      void removemiddel(double key);
      void removeall();
      void print();
};

void LL2A::addfront(double dt){
      node *tmp=new node;
   tmp->data=dt;
   tmp->next=NULL;
   tmp->back=NULL;
   if(head==NULL&&tail==NULL){
      head=tmp;
      tail=tmp;
      size++;
   }
   else{
      tmp->next=head;
      head->back=tmp;
      head=tmp;
      size++;
   }
}
void LL2A::addback(double dt){
      node *tmp=new node;
   tmp->data=dt;
   tmp->next=NULL;
   tmp->back=NULL;
   if(head==NULL&&tail==NULL){
      head=tmp;
      tail=tmp;
      size++;
   }
   else{
      tail->next=tmp;
      tmp->back=tail;
      tail=tmp;
      size++;
   }
}
void LL2A::insertnode(double dt, double key){
      node *search=head;
   bool found=false;
   while(search!=NULL){
      if(search->data==key){
            found=true;
         break;
      }
      search=search->next;
   }
   if(found){
      if(search==tail){
            addback(dt);
      }
      else{
            node*tmp=new node;
         tmp->data=dt;
         tmp->next=NULL;
         tmp->back=NULL;

         tmp->next=search->next;
         search->next->back=tmp;
         search->next=tmp;
         tmp->back=search;
         size++;
      }
   }
}
void LL2A::replacenode(double dt, double key){
      node *search=head;
   bool found=false;
   while(search!=NULL){
      if(search->data==key){
            found=true;
         break;
      }
      search=search->next;
   }
   if(found){
      search->data=dt;
   }
}
void LL2A::swapingnode(double key1, double key2){
      node*search1=head;
   node*search2=tail;
   bool found1=false;
   bool found2=false;
   while(search1!=NULL){
      if(search1->data==key1){
            found1=true;
         break;
      }
      search1=search1->next;
   }
   while(search2!=NULL){
      if(search2->data==key2){
            found2=true;
         break;
      }
      search2=search2->next;
   }
   if(found1&&found2){
      search1->data=key2;
      search2->data=key1;
   }
}
void LL2A::removefront(){
      node*tmp=head;
   head=tmp->next;
   tmp->next=NULL;
   head->back=NULL;
   delete tmp;
   size--;
}
void LL2A::removeback(){
      node*tmp=tail;
   tail=tmp->back;
   tail->next=NULL;
   tmp->back=NULL;
   delete tmp;
   size--;
}
void LL2A::removemiddel(double key){
      node*search=head;
   bool found=false;
   while(search!=NULL){
      if(search->data==key){
            found=true;
         break;
      }
      search=search->next;
   }
   if(found){
      node*tmp=search->next;
      search->next=tmp->next;
      tmp->next->back=search;
      delete tmp;
      size--;
   }
}
void LL2A::print(){
      if(size==0){
      cout<<"LINK LIST KOSONG"<<endl;
   }
   else{
      node *tmp=head;
      int a=1;
      while(tmp!=NULL){
            cout<<"NIlai dari LINK LIST ke-"<<a<<" = "<<tmp->data<<endl;
         tmp=tmp->next;
         a++;
      }
      cout<<endl;
   }
}
void LL2A::removeall(){
      while(size!=0){
            node*tmp=head;
      if(head->next==NULL){
            delete tmp;
            size--;
      }
      else{
            head=head->next;
            tmp->next=NULL;
         head->back=NULL;
            delete tmp;
            size--;
         }
   }
}

void main(){
      LL2A LL;
   LL.LL();

   LL.addfront(5);
   LL.addfront(10);
   LL.addfront(20);
   LL.print();

   cout<<"==SETELAH DI ADD BACK=="<<endl;
   LL.addback(4);
   LL.addback(3);
   LL.addback(2);
   LL.print();

   cout<<"==SETELAH DI INSERT NODE=="<<endl;
   LL.insertnode(15,20);
   LL.insertnode(8,10);
   LL.print();

   cout<<"==SETELAH DI REPLACE NODE=="<<endl;
   LL.replacenode(50,20);
   LL.print();

   cout<<"==SETELAH DI SWAPING NODE=="<<endl;
   LL.swapingnode(8,2);
   LL.print();

   cout<<"==SETELAH DI REMOVE FRONT=="<<endl;
   LL.removefront();
   LL.print();

   cout<<"==SETELAH DI REMOVE BACK=="<<endl;
   LL.removeback();
   LL.print();

   cout<<"==SETELAH DI REMOVE DI TENGAH=="<<endl;
   LL.removemiddel(10);
   LL.print();

   cout<<"==SETELAH DI REMOVE DI ALL=="<<endl;
   LL.removeall();
   LL.print();

      getch();
}


F. Tampilan Ketika Dijalankan

 


G. Kesimpulan :
1.      Memudahkan User dalam melakukan perubahan terhadap data.
2.      Penyimpanan data link list terstruktur.
3.      Memnudahkan programmer dalam menyediakan lokasi untuk memasukan data baru.