Recommended Post Slide Out For Blogger

Program Single Linked List non Circular dengan Head dan Tail pada C++

Program Single Linked List non Circular dengan Head dan Tail pada C++

#include <iostream>
#include <conio>
struct TNode
{
int data;
TNode *next;
};
TNode *head, *tail;
void init(){
head = NULL;
tail = NULL;
}
int isEmpty(){
if(tail == 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=tail=baru;
tail->next=NULL;
}

  else {
baru->next = head;
head = baru;
  }
  cout<<"Data masuk\n";
}

void insertBelakang(int databaru){
TNode *baru,*bantu;
baru = new TNode;
baru->data = databaru;
baru->next = NULL;
if(isEmpty()==1){
head=baru;
tail=baru;
tail->next = NULL;
}
else {
tail->next = baru;
tail=baru;
}
cout<<"Data masuk\n";
}

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

void hapusDepan(){
TNode *hapus;
int d;
if (isEmpty()==0){
if(head!=tail){
 hapus = head;
 d = hapus->data;
 head = head->next;
 delete hapus;
} else {
 d = tail->data;
 head=tail=NULL;
}
cout<<d<<"terhapus";
} else cout<<"Masih kosong\n";
}
void hapusBelakang(){
TNode *bantu,*hapus;
int d;
if (isEmpty()==0){
bantu = head;
if(head!=tail){
while(bantu->next!=tail){
bantu = bantu->next;
}
hapus = tail;
tail=bantu;
d = hapus->data;
delete hapus;
tail->next = NULL;
}else {
d = tail->data;
head=tail=NULL;
}
cout<<d<<" terhapus\n";
} else cout<<"Masih kosong\n";
}
void main()
{
int pil,databaru;
cout<<"*-------------------------------*"<<endl;
cout<<"* Single Linked List Non Circular "<<endl;
cout<<"*-------------------------------*"<<endl;
do
{
cout<<"\n";
cout<<"\n********************************";
cout<<"\n1. Insert Depan";
cout<<"\n2. Insert Belakang";
cout<<"\n3. Delete Depan";
cout<<"\n4. Delete Belakang";
cout<<"\n5. Tampil Data";
cout<<"\n\nSilahkan Masukan Pilihan Anda :";cin>>pil;
cout<<"\n";
switch (pil)
{
case 1:
{
cout<<"Masukkan Data = ";
cin>>databaru;
insertDepan(databaru);
break;
}
case 2:
{
cout<<"Masukkan Data = ";
cin>>databaru;
insertBelakang(databaru);
break;
}
case 3:
{
hapusDepan();
break;
}
case 4:
{
hapusBelakang();
break;
}
case 5:
{
tampil();
break;
}
default :
{
cout<<"\n Maaf, Tidak ada dalam pilihan";
}
}
}
while(pil>=1 && pil<= 5);
}
Anda baru saja membaca artikel yang berkategori Kuliah Informatika / Struktur Data dengan judul Program Single Linked List non Circular dengan Head dan Tail pada C++. Anda bisa bookmark halaman ini dengan URL http://aina-tunk.blogspot.com/2012/06/program-single-linked-list-non-circular_22.html. Terima kasih!
Ditulis oleh: Tunk-Tunk - Jumat, 22 Juni 2012

2 komentar untuk "Program Single Linked List non Circular dengan Head dan Tail pada C++"