Our Blog

Double Link List Non Circular

Double Link List Non Circular
 
 struct node{
       int data;
       node *next, *prev;
       } *head=NULL,*tail=NULL,*baru, *bantu, *temp;




 void insertDepan(int x){ //doubly linked list non-circular
     baru = new node; //membuat node baru
     baru->data=x; //masukkan data ke node baru
     baru->next=NULL;
     baru->prev=NULL;
     if(head==NULL){ //apabila belum pernah membuat node, maka awal dan akhir adalah baru
        head = tail = baru;          
        }
     else {
          baru->next=head; //baru->next menunjuk ke awal
          head->prev=baru; //awal->prev menunjuk ke baru
          head=baru; //baru menjadi awal
          }
 }

void insertBelakang(int x){ //doubly linked list non-circular
     baru = new node; //membuat node baru
     baru->data=x; //masukkan data ke node baru
     baru->next=NULL;
     baru->prev=NULL;
     if(head==NULL){ //apabila belum pernah membuat node, maka awal dan akhir adalah baru
        head = tail = baru;         
        }
     else {
          tail->next=baru; //akhir->next menunjuk ke baru
          baru->prev=tail; //baru->prev menunjuk ke akhir
          tail=baru; //baru menjadi akhir
          }
}
    
void insertTengah(int x)
{
     int count = 2, pos;
     baru=new node;         //membuat node baru
     baru->data=x;                              //masukkan data ke node
     baru->next=NULL;
     baru->prev=NULL;         
     cout<<"Masukan data pada posisi ke = ";
     cin>>pos; //masukkan node ke posisi
   
     bantu=head;
     while(bantu){
                  if(count == pos) //berheenti di posisi sebelum node yang akan ditambahkan
                  {
                              baru->next=bantu->next; //baru->next mengarah seperti arah bantu->next
                              bantu->next->prev=baru; //node setelah bantu prevnya mengarah ke node baru
                              baru->prev=bantu; //baru->prev mengarah ke bantu
                              bantu->next=baru; //bantu->next mengarah ke baru
                              break; //menghentikan while-do
                  }
                  bantu=bantu->next; //pindah ke node selanjutnya
                  count++;
                  }
  
}

 void deleteDepan(){
    temp = head;
    head = head->next;
    head->prev = NULL;
    delete temp;
}
   
void deleteTengah(int number){
     int count=1;
     bantu=head;
     while(bantu){
             if(count==number-1){ //berhenti di posisi sebelum node yang akan dihapus
                                 temp=bantu->next; //hapus menjadi node setelah bantu
                                 temp->next->prev=bantu; //node setelah hapus prevnya mengarah ke bantu
                                 bantu->next=temp->next; //bantu->next mengarah sama seperti hapus->next
                                 delete temp; //delete hapus
                                 break;
             }
             count++;
    bantu=bantu->next;                      
     }
}

void deleteBelakang(){
    temp = tail;
    tail = tail->prev;
    tail->next = NULL;
    delete temp;
}

void print(){
    bantu=head;
    if(head!=NULL){
       while(bantu!=NULL){
             cout<<"["<<bantu->data<<"] "; //print dimulai dari depan
             bantu=bantu->next;       //pindah ke belakang dan seterusnya
       };
    }

}


int main()

    {

        int choice, num;//declaration variable

        while (1)//looping while condition

        {
            system("cls");//to clear screen
            cout<<"Double Link List Non Circular Menu "<<endl<<endl;
           
            cout<<"1.Insert Depan "<<endl;

            cout<<"2.Insert Belakang "<<endl;

            cout<<"3.Insert Tengah"<<endl;

            cout<<"4.Delete Depan"<<endl;

            cout<<"5.Delete Belakang "<<endl;

            cout<<"6.Delete Tengah"<<endl;

            cout<<"7.Print"<<endl;
           
            cout<<"8.Exit"<<endl;
           
            cout<<"Enter your choice : ";

            cin>>choice;//to input the value of choise based on menu

            switch(choice)//switch condition

            {

            case 1://to insert data in forward
                cout<<"Masukkan data : ";
                cin>>num;
                insertDepan(num);
                break;

            case 2://to insert data in backward
                cout<<"Masukkan data : ";
                cin>>num;
                insertBelakang(num);
                break;

            case 3://to insert data in middle or in position what user wants
                cout<<"Masukkan data : ";
                cin>>num;
                insertTengah(num);//called function
                break;
           
            case 4://to delete data in forward
                deleteDepan();//called function
                break;

            case 5://to delete data or list in backward
                deleteBelakang();
                break;

            case 6://to delete list
                cout<<"Hapus data ke "; cin>>num;
                deleteTengah(num);
                break;
           
            case 7://to display list
                print();
                getch();
                break;

            case 8://to exit program

                exit(1);

            default://if user not choose of the choices

                cout<<"Wrong choice"<<endl;//display the sentence

            }

        }

    }
 

No comments:

Post a Comment

All about Math Designed by Templateism | MyBloggerLab Copyright © 2014

Theme images by richcano. Powered by Blogger.