#include<iostream>
#include<conio>
#include<process>
struct TNode
{
int data;
int databaru;
TNode *next;
};
TNode *head;
void init()
{
head = NULL;
}
int isEmpty()
{
if(head == NULL) return 1;
else return 0;
}
void insertDepan(int databaru)
{
TNode *baru,*bantu;
baru = new TNode;
baru->data = databaru;
baru->next = NULL;
if(isEmpty()==1)
{
head=baru;
head->next = NULL;
}
else
{
baru->next = head;
head = baru;
}
cout<<"Masukkan data : ";cin>>bantu->databaru;
cout<<"Data masuk\n";
}
void insertBelakang (int databaru)
{
TNode *baru,*bantu;
baru = new TNode;
baru->data = databaru;
baru->next = NULL;
if(isEmpty()==1)
{
head=baru;
head->next = NULL;
}
else
{
bantu=head;
while(bantu->next!=NULL)
{
bantu=bantu->next;
}
bantu->next = baru;
}
cout<<"Masukkan data : ";cin>>bantu->databaru;
cout<<"Data masuk\n";
}
void hapusDepan()
{
TNode *hapus;
int d;
if (isEmpty()==0)
{
if(head->next != NULL)
{
hapus = head;
d = hapus->data;
head = head->next;
delete hapus;
}
else
{
d = head->data;
head = NULL;
}
cout<<d<<" terhapus\n";
} else cout<<"Masih kosong\n";
}
void hapusBelakang()
{
TNode *hapus,*bantu;
int d;
if (isEmpty()==0)
{
if(head->next != NULL)
{
bantu = head;
while(bantu->next->next!=NULL)
{
bantu = bantu->next;
}
hapus = bantu->next;
d = hapus->data;
bantu->next = NULL;
delete hapus;
}
else
{
d = head->data;
head = NULL;
}
cout<<d<<" terhapus\n";
} else cout<<"Masih kosong\n";
}
void clear()
{
TNode *bantu,*hapus;
bantu = head;
while(bantu!=NULL){
hapus = bantu;
bantu = bantu->next;
delete hapus;
}
head = NULL;
}
void tampil()
{
TNode *bantu;
bantu = head;
if(isEmpty()==0)
{
while(bantu!=NULL)
{
cout<<bantu->databaru<<" ";
bantu=bantu->next;
}
cout<<endl;
} else cout<<"Masih kosong\n";
}
main()
{
int a,b,c,f;
char e;
init();
menuutama:
clrscr();
cout<<"Daftar data :\n";
cout<<"1. Input data\n";
cout<<"2. Hapus Data\n";
cout<<"3. Tampil data\n";
cout<<"Pilih menu [1-2] : ";cin>>a;
switch(a)
{
case 1 :
{
menuinput:
clrscr();
cout<<"Pilihan :\n";
cout<<"1. input dari depan \n";
cout<<"2. Input dari belakang \n";
cout<<"Pilih menu [1-2] : ";cin>>b;
switch(b)
{
case 1 :
insertDepan(f);
goto out;
case 2 : insertBelakang(f);
goto out;
default:
goto menuinput;
}
break;
}
case 2 :
menuhapus:
clrscr();
cout<<"Pilihan:\n";
cout<<"1. Hapus depan\n";
cout<<"2. Hapus belakang\n";
cout<<"3. Hapus semua\n";
cout<<"Pilih menu [1-3]:";cin>>c;
switch(c)
{
case 1 :
hapusDepan();
goto out;
case 2 :
hapusBelakang();
goto out;
case 3 :
clear();
goto out;
default :
goto menuhapus;
}
case 3 :
tampil();
break;
}
out:
cout<<"Kembali ke menu utama [y|n]:";cin>>e;
if(e=='y'||e=='Y')
goto menuutama;
getch();
}
Home
»
»Unlabelled
» code linklist
Posting Lebih Baru
Previous
This is the last post.
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar