#include"conio.h"
#include"stdlib.h"
#include"iostream.h"
#include"iomanip.h"
//-----------------------------pembentukan simpul-----------------------------//
typedef struct TNode
{
int data;
TNode *next;
TNode *prev;
} TNode;
TNode *head;
void init();
int IsEmpty();
void InsertDepan(int);
void InsertBelakang(int);
void HapusDepan();
void HapusBelakang();
void clear();
void tampil();
//----------------------------looping program utama---------------------------//
void main()
{
int data;
int pil;
do{
printf("Double Linked List Circular \n");
cout<
printf("2. Insert Belakang\n");
printf("3. Hapus Depan\n");
printf("4. Hapus Belakang\n");
cout<
scanf("%d",&pil);
clrscr();
switch(pil)
{
case 1:printf("Masukkan Data di Depan : ");scanf("%d",&data);
InsertDepan(data);break;
case 2:printf("Masukkan Data di Belakang : ");scanf("%d",&data);
InsertBelakang(data);break;
case 3:"%d",HapusDepan();break;
case 4:"%d",HapusBelakang();break;
;
}
getch();
}
while(pil!=7);
}
//------------------------------inisialisasi----------------------------------//
void init()
{ head = NULL;}
//-------------------------------pengecekan kondisi---------------------------//
int IsEmpty()
{
if(head==NULL)
return 1;
else
return 0;}
//----------------------------penambahan di depan-----------------------------//
void InsertDepan(int value)
{
TNode *bantu, *baru;
baru=new TNode;
baru ->data=value;
baru ->next=baru;
baru ->prev=baru;
if(IsEmpty()==1)
{
head=baru;
head ->next=head;
head ->prev=head;
}
else
{
bantu=head ->prev;
baru ->next=head;
head ->prev=baru;
head=baru;
head ->prev=bantu;
bantu ->next=head;
} cout<
//-----------------------------penambahan data dibelakang---------------------//
void InsertBelakang(int value)
{
TNode *baru, *bantu;
baru=new TNode;
baru ->data=value;
baru ->next=baru;
baru ->prev=baru;
if(IsEmpty()==1)
{
head=baru;
head ->next=head;
head ->prev=head;
}
else
{
bantu=head ->prev;
bantu ->next=baru;
baru ->prev=bantu;
baru ->next=head;
head ->prev=baru;
}
cout<
}
//------------------------------Hapus Depan-----------------------------------//
void HapusDepan()
{
TNode *hapus, *bantu;
int d;
if(IsEmpty()==0)
{
if(head ->next !=head)
{
hapus=head;
d=hapus ->data;
bantu=head ->prev;
head=head ->next;
bantu ->next=head;
head ->prev=bantu;
delete hapus;
}
else
{
d=head ->data;
head=NULL; }
cout<
}
else
cout<
//------------------------Menghapus Data Dari Belakang------------------------//
void HapusBelakang()
{ TNode *hapus, *bantu;
int d;
if(IsEmpty()==0)
{
if(head ->next !=head)
{
bantu=head;
while(bantu->next->next !=head)
{ bantu=bantu ->next; }
hapus=bantu ->next;
d=hapus ->data;
bantu->next=head;
delete hapus;
}
else
{
d=head->data;
head=NULL;
}
printf("%d Terhapus \n",d);}
else
printf("Masih Kosong");
}
//------------------------------Menghapus Semua Data--------------------------//
void clear()
{
TNode *bantu, *hapus;
bantu=head;
if(IsEmpty()==0)
{
bantu=head;
while(bantu ->next!=head)
{ hapus=bantu;
bantu=bantu->next;
delete hapus;
}
head=NULL;
}
}
//-------------------------Tampilkan Data-------------------------------------//
void tampil()
{
TNode *bantu;
bantu=head;
if(IsEmpty()==0)
{
do
{
printf(" %d",bantu ->data);
bantu=bantu->next;
}
while(bantu !=head);
printf("\n");
}
else
printf("Data Kosong");
}
Tidak ada komentar:
Posting Komentar