Created
April 21, 2021 06:30
-
-
Save uluumbch/1bd47bbf7ff7ffa03f038bd6afea4007 to your computer and use it in GitHub Desktop.
Contoh program Double Linked List Circular (DLLC) sederhana dengan HEAD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <conio.h> | |
#include <stdlib.h> | |
using namespace std; | |
typedef struct TNode | |
{ | |
int data; | |
TNode *next, *prev; | |
}; | |
TNode *head, *bantu, *baru, *hapus; | |
int isEmpty() | |
{ | |
if (head == NULL) | |
return 1; | |
else | |
return 0; | |
} | |
void tampil() | |
{ | |
TNode *bantu; | |
bantu = head; | |
if (isEmpty() == 0) | |
{ | |
do | |
{ | |
cout << bantu->data << " "; | |
bantu = bantu->next; | |
} while (bantu != head); | |
cout << endl; | |
} | |
else | |
cout << "Data masih kosong\n"; | |
} | |
void insertDepan(int databaru) | |
{ | |
TNode *baru, *bantu; | |
baru = new TNode; | |
baru->data = databaru; | |
baru->next = baru; | |
baru->prev = baru; | |
if (isEmpty() == 1) | |
{ | |
head = baru; | |
head->next = baru; | |
head->prev = baru; | |
} | |
else | |
{ | |
bantu = head->prev; | |
baru->next = head; | |
head->prev = baru; | |
head = baru; | |
head->prev = bantu; | |
bantu->next = head; | |
} | |
cout << "Data " << databaru << " telah masuk"; | |
} | |
void insertBelakang(int databaru) | |
{ | |
TNode *baru, *banru; | |
baru = new TNode; | |
baru->data = databaru; | |
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 << "Data " << databaru << " telah masuk"; | |
} | |
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 << "Data " << d << " Telah terhapus"; | |
} | |
else | |
{ | |
cout << "Data masih kosong"; | |
} | |
} | |
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; | |
} | |
cout << "Data " << d << " Telah terhapus"; | |
} | |
else | |
cout << "Data masih kosong"; | |
} | |
void clearData() | |
{ | |
TNode *bantu, *hapus; | |
if (isEmpty() == 0) | |
{ | |
bantu = head; | |
while (bantu->next != head) | |
{ | |
hapus = bantu; | |
bantu = bantu->next; | |
delete hapus; | |
} | |
head = NULL; | |
} | |
cout << "Semua data telah berhasil di hapus"; | |
} | |
void menu() | |
{ | |
char pilih; | |
int data; | |
do | |
{ | |
system("cls"); | |
cout << "Double Linked List Circular" << endl; | |
cout << "Berikut merupakan Double Linked List Circular dengan Head" << endl; | |
cout << "---------------------------" << endl; | |
cout << "Menu\n"; | |
cout << "1. Input data dari depan\n"; | |
cout << "2. Input data dari belakang\n"; | |
cout << "3. Lihat data yang masuk\n"; | |
cout << "4. Hapus data dari depan\n"; | |
cout << "5. Hapus data dari belakang\n"; | |
cout << "6. Hapus semua data\n"; | |
cout << "7. Exit\n"; | |
cout << "Masukkan Pilihan Anda : "; | |
cin >> pilih; | |
switch (pilih) | |
{ | |
case '1': | |
cout << "Masukkan data : "; | |
cin >> data; | |
insertDepan(data); | |
getch(); | |
break; | |
case '2': | |
cout << "Masukkan data : "; | |
cin >> data; | |
insertBelakang(data); | |
getch(); | |
break; | |
case '3': | |
tampil(); | |
getch(); | |
break; | |
case '4': | |
hapusDepan(); | |
getch(); | |
break; | |
case '5': | |
hapusBelakang(); | |
getch(); | |
break; | |
case '6': | |
clearData(); | |
getch(); | |
break; | |
case '7': | |
cout << "Terimakasih\nProgram dibuat oleh\nBachrul Uluum | 2010817210025"; | |
exit(0); | |
break; | |
default: | |
cout << "Data yang Anda masukkan tidak valid!\n"; | |
} | |
} while (pilih != '7'); | |
} | |
int main() | |
{ | |
menu(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment