Jumat, 24 Oktober 2014

Contoh Program Linked List Non Circular






Ini adalah contoh sederhana penerapan program linked list...

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

typedef struct TNode{
int data;
TNode *next;
}TNode;
TNode *head;

void init(){
head = NULL;
}

int isEmpty(){
if(head == NULL) return 1;
else return 0;
}

void insertdepan(int databaru){
 TNode *baru;
 baru = new TNode;
 baru->data = databaru;
 baru->next = NULL;
 if(isEmpty()==1){
head=baru;
head->next = NULL;
 }
 else {
baru->next = head;
head = baru;
 }
     printf("\n");
 printf("Data Anda Tersimpan");
}

void insertbelakang (int databaru){
  TNode *baru,*bantu;
  baru = new TNode;
  baru->data = databaru;
  baru->next = NULL;
  if(isEmpty()==1){
    head=baru;
    head->next = NULL;
  }
  else {
    bantu=head;
    while(bantu->next!=NULL){
bantu=bantu->next;
    }
    bantu->next = baru;
  }
  printf("\n");
  printf("Data Anda Tersimpan");
}

void hapusdepan (){
TNode *hapus;
int d;
if (isEmpty()==0){
if(head->next != NULL){
hapus = head;
d = hapus->data;
head = head->next;
delete hapus;
} else {
d = head->data;
head = NULL;
}
printf("Data %d Terhapus",d);
} else cout<<"Data Anda Masih kosong";
}
void hapusbelakang(){
TNode *hapus,*bantu;
int d;
if (isEmpty()==0){
if(head->next != NULL){
bantu = head;
while(bantu->next->next!=NULL){
 bantu = bantu->next;
}
hapus = bantu->next;
d = hapus->data;
      bantu->next = NULL;
delete hapus;
} else {
d = head->data;
head = NULL;
}
printf("Data %d Terhapus",d);
} else printf("Data Anda Masih kosong");
}

void cari(int caridata){

TNode *bantu;
bantu = head;
int ketemu = 0;
int index=1;
if(isEmpty()==0)
{
while(bantu!=NULL)
{
bantu->data;

if (caridata == bantu->data)
{
cout<<endl;
cout<<" Data Ditemukan...\n"<<endl;

cout<<" Index Ke - "<<index;

ketemu = 1;

break;
}
bantu=bantu->next;
index++;
}
cout<<endl;
if (ketemu == 0)
cout<<" Data Tidak Ditemukan..."<<endl;
cout<<endl;
} else cout<<" Linked List Masih kosong...\n";
getch();

}

void tampil(){
TNode *bantu;
bantu = head;
if(isEmpty()==0){
while(bantu!=NULL){
cout<<bantu->data<<" ";
bantu=bantu->next;
}
printf("");
}
      else printf("Data Anda Masih kosong");
}


  main(){
int pil;
      do{
      clrscr();
      int n;
      cout<<"MENU LINKED LIST SEDERHANA DENGAN PENCARIAN DATA TERTENTU"<<endl;
      cout<<"__________________________________________________________\n"<<endl;
      cout<<"1. Insert Depan\n "<<endl;
      cout<<"2. Insert Belakang\n "<<endl;
      cout<<"3. Hapus Data Depan\n "<<endl;
      cout<<"4. Hapus Data Belakang\n "<<endl;
      cout<<"5. Mencari Data Tertentu\n "<<endl;
      cout<<"6. Tampilkan Data\n "<<endl;
      cout<<"7. Keluar\n"<<endl;
      cout<<" Masukan Pilihan : ";pil=getche();
      switch(pil){

      case'1': clrscr();
      cout<<"Masukan Data : ";cin>>n;
      insertdepan(n);
      break;

      case'2': clrscr();
      cout<<"Masukan Data : ";cin>>n;
      insertbelakang(n);
      break;

      case'3': clrscr();
      hapusdepan();
      break;

      case'4': clrscr();
      hapusbelakang();
      break;

      case'5': clrscr();
      cout<<"Data Yang Dicari : ";cin>>n;
      cari(n);
      break;

      case'6': clrscr();
      tampil();
      break;
      }

      getch();
      }while(pil!='7');
      return 0;
      }

Semoga bermanfaat.. :)

Tidak ada komentar:

Posting Komentar