Kamis, 28 Mei 2009

Double Link List Non Circular Pada C++

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

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

TNode *head, *tail;

void init()
{
head = NULL;
tail = NULL;
}

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

//Insert 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";
}


// Insert 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";
}


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


// Hapus Depan
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<<d<<" terhapus\n";
}
else cout<<"Masih kosong\n";
}


// Hapus Belakang
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<<d<<" terhapus\n";
}
else cout<<"Masih kosong\n";
}


// Clear
void clear()
{
TNode *bantu,*hapus;
bantu = head;
while(bantu!=NULL)
{
hapus = bantu;
bantu = bantu->next;
delete hapus;
}
head = NULL;
tail = NULL;
}


void main()
{
int data;
int pil;
do
{
cout<<"1. Insert Depan\n";
cout<<"2. Insert Belakang\n";
cout<<"3. Hapus Depan\n";
cout<<"4. Hapus Belakang\n";
cout<<"5. Tampil\n";
cout<<"6. Clear\n";
cout<<"7. Exit\n\n";
cout<<"Pilihan : ";cin>>pil;
clrscr();
switch(pil)
{
case 1: printf("Data : ");scanf("%d",&data);
insertDepan(data);
break;

case 2: printf("Data : ");scanf("%d",&data);
insertBelakang(data);
break;

case 3: printf("Elemen yang keluar : ");"%d",hapusDepan();
break;

case 4: printf("Elemen yang keluar : ");"%d",hapusBelakang();
break;

case 5: tampil();
break;

case 6: clear();
printf("Data sampun dipun delete.....!!!!!\n");
break;
}
getch();
}
while(pil!=7);
}




Artikel Yang Bersangkutan



Template by : kendhin x-template.blogspot.com