#include <conio.h>
#include <iostream.h>
typedef struct TNode {
int data;
TNode *next;
TNode *prev;
}TNode;
TNode *head, *tail;
void init() {
head = NULL;
tail = NULL;
}
bool isEmpty() {
if (tail == NULL) return true;
else return false;
}
/*insert depan*/
void add_at_front () {
TNode *baru;
baru = new TNode;
cout <<"Silahkan masukkan Data : ";
cin >> baru->data;
baru->next = NULL;
baru->prev = NULL;
if (isEmpty()==true)
head = baru;
tail = head;
head->next = NULL;
head->prev = NULL;
tail->next = NULL;
tail->prev = NULL;
} else {
baru->next = head;
head->prev = baru:
head = baru;
}
}
/*tambah data di belakang*/
void add_at_end() {
TNode *baru;
baru = new TNode;
cout << "Silahkan masukkan Data : ";
cin >> baru -> data;
baru->next = NULL;
baru->prev = NULL;
if (isEmpty() == true) {
head = baru;
tail = head;
head->next = NULL;
head->prev = NULL;
tail->next = NULL;
tail->prev = NULL;
} else {
tail->next = baru;
baru->prev=tail;
tail=baru;
tail->bext=NULL;
}
}
/*hapus simpul dari depan*/
void del_at_front() {
TNode *hapus;
int d;
if (isEmpty() == false) {
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 \";
} else {
cout << "Masih kosong \n";
}
}
void del_at_end() {
TNode *hapus;
int d;
if (isEmpty() == false) {
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 << "Data kosong . .\n";
}
}
/*hapus semua simpul*/
void clear () {
TNode *bantu, *hapus;
bantu = head;
while (bantu !=NULL) {
hapus = bantu;
bantu=bantu->next;
delete hapus;
}
head = NULL;
tail = NULL;
}
/*menampilkan simpul*/
void display () {
TNode *bantu;
bantu = head;
if (isEmpty()==false) {
while(bantu!=tail->next) {
cout << bantu->data << "" ;
bantu = bantu->next;
}
cout << endl;
}else {
cout << "Data Kosong";
}
}
void main () {
int data;
int pil;
do {
cout << "1. Tambah Depan\n";
cout << "2. Tambah Belakang\n";
cout << "3. Hapus Depan\n";
cout << "4. Hapus Belakang\n";
cout << "5. Tampil\n";
cout << "6. Hapus semua Data\n";
cout << "7. Exit\n";
cout << "pilihan : ";
cin >> pil;
clrscr ();
switch (pil) {
case 1: add_at_front(); break;
case 2: add_at_end(); break;
case 3: del_at_front(); break;
case 4: del_at_end(); break;
case 5: display(); break;
case 6: clear(); break;
}
getch ();
}while (pil !=7);
;
Sign up here with your email
ConversionConversion EmoticonEmoticon