Friday, July 20, 2012
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;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment