# include <iostream.h>
# include <conio.h>
struct TNode{
char nim[15];
char nama[30];
int usia;
TNode *next;
};
TNode *head;
int opsi = 0;
void init(){
head = NULL;
}
bool isEmpty(){
if (head ==NULL) return true;
else return false;
}
void add_at_front(){
TNode *baru;
baru = new TNode;
cout << "Masukkan NIM : ";
cin >> baru-> nim;
cout << "Masukkan Nama : ";
cin >> baru-> nama;
cout << "Masukkan usia : ";
cin >> baru-> usia;
baru->next = NULL;
if(isEmpty()==true){
head=baru;
head->next = NULL;
}else {
baru->next = head;
head = baru;
}
}
void add_at_end(){
TNode *baru,*bantu;
baru = new TNode;
cout << "Masukkan NIM : ";
cin >> baru-> nim;
cout << "Masukkan Nama : ";
cin >> baru-> nama;
cout << "Masukkan usia : ";
cin >> baru-> usia;
baru->next = NULL;
if(isEmpty()== true){
head=baru;
head->next = NULL;
} else {
bantu=head;
while(bantu->next!=NULL){
bantu=bantu->next;
}
bantu->next = baru;
}
}
void add_at_middle(){
TNode *baru, *bantu;
int tmp;
if(isEmpty()== false){
cout<<"Setelah data ke berapa data diinsert? : "; cin>>tmp;
bantu=head;
baru=new TNode;
for(int i=1;i<tmp;i++){
if(bantu->next!=NULL)
bantu=bantu->next;
else break;
}
cout << "Masukkan NIM : ";
cin >> baru-> nim;
cout << "Masukkan Nama : ";
cin >> baru-> nama;
cout << "Masukkan usia : ";
cin >> baru-> usia;
baru->next=bantu->next;
bantu->next=baru;
bantu=baru;
}
else cout<<"Data masih kosong, boro-boro bisa masukkan data tengah! ";
}
void del_at_front () {
TNode *hapus;
if (isEmpty() == false){
if(head->next !=NULL){
hapus = head;
head = head->next;
delete hapus;
} else {
head = NULL;
}
}else {
cout<<"masih kosong\n";
}
}
void del_at_end(){
TNode *hapus, *bantu;
if (isEmpty()==false){
if(head->next !=NULL){
bantu = head;
while(bantu->next->next!=NULL){
bantu = bantu->next;
}
hapus = bantu->next;
bantu->next = NULL;
delete hapus;
} else {
head = NULL;
}
} else {
cout<<"masih kosong\n";
}
}
void del_at_middle(){
int banyakdata,tmp,poshapus,pdata;
TNode *hapus, *bantu;
if(isEmpty()== false){
cout<<"Data ke berapa data dihapus? : "; cin>>tmp;
banyakdata=1;
bantu=head;
while(bantu->next!=NULL)
{
bantu=bantu->next;
banyakdata++;
}
if((tmp<1)||(tmp>banyakdata)){
cout<<"Posisi di luar jangkauan\n";
}else {
bantu=head;
poshapus=1;
while(poshapus<(tmp-1))
{
bantu=bantu->next;
poshapus++;
}
hapus=bantu->next;
bantu->next=hapus->next;
delete hapus;
}
}
else cout<<"Data masih kosong, gak bisa hapus data dari tengah! ";
}
void display_list(){
TNode *bantu;
bantu = head;
if(isEmpty()==true){
cout<<"masih kosong\n";
} else {
cout<<endl<<"--------------------------\n";
while(bantu!=NULL){
cout<<"NIM : " << bantu->nim << " ";
cout<<"NAMA : " << bantu->nama<< " ";
cout<<"USIA : " << bantu->usia<< " ";
bantu=bantu->next;
}
cout<<endl;
}
}
void main(){
int();
do{
cout<<endl;
cout<<"silahkan pilih menu :" <<endl;
cout<<"0. exit program." <<endl;
cout<<"1. tambah simpul dari depan." <<endl;
cout<<"2. tambah simpul dari belakang." <<endl;
cout<<"3. tambah simpul dari tengah." <<endl;
cout<<"4. hapus simpul dari depan." <<endl;
cout<<"5. hapus simpul dari belakang." <<endl;
cout<<"6. hapus simpul dari tengah." <<endl;
cout<<"7. tampil data." <<endl;
cout<<endl;
cout<< " >> ";
cin >> opsi;
switch(opsi){
case 1 : add_at_front();break;
case 2 : add_at_end();break;
case 3 : add_at_middle();break;
case 4 : del_at_front();break;
case 5 : del_at_end();break;
case 6 : del_at_middle();break;
case 7 : display_list();break;
}
}while (opsi != 0);
}
Enjoy :)
Sign up here with your email
ConversionConversion EmoticonEmoticon