Wawan

Friday, July 20, 2012

Contoh Program Single Linked List Circular


// Program for singly linked Circular list


#include <iostream>
#include <string>


struct SLinkCircularList
{
    std::string data;
    SLinkCircularList *next;


    SLinkCircularList() : data(""), next(this) { }
    SLinkCircularList(const char *value) : data(value), next(this) { }


    SLinkCircularList* InsertNext(const char *data)
    {
        SLinkCircularList *node = new SLinkCircularList(data);
        if(this->next == this) // only one node in the circular list
        {
            // Easy to handle, after the two lines of executions,
            // there will be two nodes in the circular list
            node->next = this;
            this->next = node;
        }
        else
        {
            // Insert in the middle
            SLinkCircularList *temp = this->next;
            node->next = temp;
            this->next = node;
        }
        return node;
    }


    bool DeleteNext()
    {
        if(this->next == this)
        {  
            std::cout << "\nThe node can not be deleted as there is only one node in the circular list\n";
            return false;
        }
        SLinkCircularList *pNode = this->next;
        this->next = this->next->next; 
        delete pNode;
    }


    void Traverse(SLinkCircularList *node = NULL)
    {
        if(node == NULL)
            node = this;
        std::cout << "\n\nTraversing in Forward Direction\n\n";


        SLinkCircularList *startnode = node;
        do
        {
            std::cout << node->data;
            std::cout << "\n";
            node = node->next;
        }
        while(node != startnode);
    }


    int GetNumberOfNodes(SLinkCircularList *node = NULL)
    {
        if(node == NULL)
            node = this;
        int count = 0;
        SLinkCircularList *startnode = node;
        do
        {
            count++;
            node = node->next;
        }
        while(node != startnode);


        std::cout << "\nCurrent Node Value: " << node->data.c_str();
        std::cout << "\nTotal nodes in Circular List: " << count;
        std::cout << "\n";
        return count;
    }
   
};


int main()
{
    SLinkCircularList *node1 = new SLinkCircularList("ONE");
    node1->DeleteNext(); // Delete will fail in this case.
    SLinkCircularList *node2 = node1->InsertNext("TWO");
    node1->DeleteNext(); // It will delete the node2.
    node2 = node1->InsertNext("TWO"); // Insert it again
    SLinkCircularList *node3 = node2->InsertNext("THREE");
    SLinkCircularList *node4 = node3->InsertNext("FOUR");
    SLinkCircularList *node5 = node4->InsertNext("FIVE");


    node1->GetNumberOfNodes();
    node3->GetNumberOfNodes();
    node5->GetNumberOfNodes();


    node1->Traverse();
    node3->DeleteNext(); // delete the node "FOUR"
    node2->Traverse();
    std::cout << "\n";  


    node1->GetNumberOfNodes();
    node3->GetNumberOfNodes();
    node5->GetNumberOfNodes();


    std::cout << "\n";
   
    return 0;
}

contoh program Double Linked List


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>


struct node{
        struct node *prev;
        int data;
        struct node *next;        
};

typedef struct node node;

node *pHead = NULL;

node *alokasiNodeBaru(){ 
  node *pNew = NULL; 
  pNew = (node *) malloc(sizeof(node)); 
  return(pNew); 
}


void tambahAwal(node *pNew){
     printf("masukkan bilangan: "); scanf("%d",&pNew->data);
     
     if(pHead == NULL){
              pNew->prev = pHead;
              pNew->next = pHead;
              pHead = pNew;
     }
     else{
              //cari node yang menunjuk ke pHead
              pNew->prev = pHead;
              pNew->next = pHead;
              pHead->prev= pNew;
              pHead = pNew;   
     }
}



void cetak(){
     node *pWalker = pHead; int i=1;
     while(pWalker!=NULL){
         printf("node ke-%d = %d\n",i,pWalker->data);
         i++;
         pWalker=pWalker->next;        
     }
     printf("NULL\n");
}


void tambahTengah(node *pNew){
     node *pWalker;
     pWalker=pHead;
     int nilai,sisip;
     printf("masukkan nilai yang ingin ditambahkan: "); scanf("%d",&pNew->data);
     cetak(); 
     printf("data disisipkan setelah nilai : "); scanf("%d",&sisip);
     while(pWalker!=NULL && pWalker->data!=sisip){
                         pWalker=pWalker->next; }
                       
     if(pWalker==NULL) {printf("\ndata tidak ditemukan"); getch();}
     else {
           pNew->next=pWalker->next;
           pWalker->next->prev=pNew;
           pWalker->next=pNew;
           pNew->prev=pWalker;
           }

}

void tambahAkhir(node *pNew){
     node *pEnd;
     pEnd=pHead;
     printf("masukkan nilai yang ingin ditambahkan: "); scanf("%d",&pNew->data);
     
     while(pEnd->next!=NULL){
                         pEnd=pEnd->next; }
     pEnd->next=pNew;
     pNew->prev=pEnd;
     pNew->next=NULL;        
}

void hapusAwal(){
     node *pHapus;
     pHapus=pHead;
     pHead=pHead->next;
     pHead->prev=NULL;
     free(pHapus);


void hapusTengah(){
     node *pCari;int hapus;
     pCari=pHead;
     cetak();
     printf("masukkan bilangan yang ingin dihapus: "); scanf("%d",&hapus);
     while(pCari!=NULL && pCari->data!=hapus){
                               pCari=pCari->next;
                               }                      
    pCari->prev->next=pCari->next;
    pCari->next->prev=pCari->prev;
     free(pCari);
     
}

void hapusAkhir(){
     node *pEnd;
     pEnd=pHead;
     while(pEnd->next!=NULL){
                              pEnd=pEnd->next;
                              }
     pEnd->prev->next=NULL;
     free(pEnd);
}

     
     
int main(int argc, char *argv[])
{
  node *pNew; int pilih,bil;
  do{system("cls");
   printf("----------MENU-----------");
    printf("\n1.tambah awal");
    printf("\n2.tambah tengah");
    printf("\n3.tambah akhir");
    printf("\n4.hapus awal ");
    printf("\n5.hapus tengah");
    printf("\n6.hapus akhir");
    printf("\n7.cetak");
    printf("\n9.exit");
    printf("\npilihan : ");scanf("%d",&pilih);
    
    if(pilih==1){pNew=alokasiNodeBaru();
                 tambahAwal(pNew); 
                 }
    else if(pilih==2){pNew=alokasiNodeBaru();
                 tambahTengah(pNew);
                 }
    else if(pilih==3){pNew=alokasiNodeBaru();
                 tambahAkhir(pNew);
                 }
    else if(pilih==4){hapusAwal();}
    else if(pilih==5){hapusTengah();}
    else if(pilih==6){hapusAkhir();}
    else if(pilih==7){cetak();getch();}

               
                 
}while(pilih!=9);
  
  
  printf("\n");
  system("PAUSE");    
  return 0;
}




sumber : http://belajarinformatics.blogspot.com/2012/05/program-c-double-linked-list.html

Potongan - potongan Source Code DLLNC (Double Linked List Non Circular)


//Fungsi Inisialisasi DLLNC 
TNode *head, *tail; 
void init(){ 
head = NULL; 
tail = NULL; 



//Function untuk mengetahui kosong tidaknya DLLNC 
int isEmpty (){ 
if(tail == NULL) return 1; 
else return 0; 



//Tambah Depan 
void insertDepan ( int databaru ){ 
TNode * baru ; 
baru = new TNode ; 
baru ->data = databaru ; 
baru ->next = NULL; 
baru -> prev = NULL; 
if( isEmpty ()==1){ 
head= baru ; 
tail=head; 
head->next = NULL; 
head-> prev = NULL; 
tail-> prev = NULL; 
tail->next = NULL; 

else { 
baru ->next = head; 
head-> prev = baru ; 
head = baru ; 

cout <<"Data masuk \n"; 



//Tambah node Belakang
void insertBelakang ( int databaru ){ 
TNode * baru ; 
baru = new TNode ; 
baru ->data = databaru ; 
baru ->next = NULL; 
baru -> prev = NULL; 
if( isEmpty ()==1){ 
head= baru ; 
tail=head; 
head->next = NULL; 
head-> prev = NULL; 
tail-> prev = NULL; 
tail->next = NULL; 

else { 
tail->next = baru ; 
baru -> prev = tail; 
tail = baru ; 
tail->next = NULL; 

cout <<"Data masuk \n"; 





//Function untuk menampilkan isi linked list 
void tampil (){ 
TNode *bantu; 
bantu = head; 
if( isEmpty ()==0){ 
while(bantu!=tail->next){ 
cout <data<<" "; 
bantu=bantu->next; 

cout << endl ; 
} else cout <<" Masih kosong \n"; 
}


//Function untuk menghapus data di data terdepan 
void hapusDepan (){ 
TNode * hapus ; 
int d; 
if ( isEmpty ()==0){ 
if(head->next != NULL){ 
hapus = head; 
d = hapus ->data; 
head = head->next; 
head-> prev = NULL; 
delete hapus ; 
} else { 
d = head->data; 
head = NULL; 
tail = NULL; 

cout <<<" terhapus \n"; 
} else cout <<" Masih kosong \n"; 



//Function untuk menghapus node terbelakang 
void hapusBelakang (){ 
TNode * hapus ; 
int d; 
if ( isEmpty ()==0){ 
if(head->next != NULL){ 
hapus = tail; 
d = tail->data; 
tail = tail-> prev ; 
tail->next = NULL; 
delete hapus ; 
} else { 
d = head->data; 
head = NULL; 
tail = NULL; 

cout <<<" terhapus \n"; 
} else cout <<" Masih kosong \n"; }
}


//Function untuk menghapus semua elemen LinkedList 
void clear(){ 
TNode *bantu,* hapus ; 
bantu = head; 
while(bantu!=NULL){ 
hapus = bantu; 
bantu = bantu->next; 
delete hapus ; 

head = NULL; 
tail = NULL; 

Contoh Program sederhana Pointer


#include<constream.h>
void main()
{
clrscr();
char *nama = "UNIVERSITAS";
char cari = 'S', tmp;

cout<<"Kata = "<<nama;
cout<<"\nHuruf yang dicari adalah = "<<cari<<endl<<endl;
for(int i=0; i<11; i++){
if(nama[i]==cari)
tmp=nama[i];
//cout<<nama[i]<<" TRUE\n";
//else cout<<nama[i]<<" FALSE\n";
}
if(tmp==cari)
cout<<"Huruf Ditemukan";
else
cout<<"Huruf tidak Ditemukan";
getch();
}