Double Linked List
PROGRAM DOUBLE LINKED LIST
#include <iostream>
using namespace std;
struct data
{
string jenis;
string jumlah;
string umur;
string harga;
data *next;
data *prev;
};
data *baru,*hapus,*after;
data *head=NULL,*tail=NULL;
void tambah()
{
baru=new data;
cout<<"
jenis :
";cin>>baru->jenis;
cout<<"
umur : ";cin>>baru->umur;
cout<<"
jumlah : ";cin>>baru->jumlah;
cout<<"
harga :
";cin>>baru->harga;
baru->next = NULL;
baru->prev = NULL;
if(head==NULL)
{
tail=baru;
}
else
{
baru->next=head;
head->prev=baru;
}
head=baru;
}
void tambah_belakang()
{
baru=new data;
cout<<"
jenis :
";cin>>baru->jenis;
cout<<"
umur : ";cin>>baru->umur;
cout<<"
jumlah : ";cin>>baru->jumlah;
cout<<"
harga :
";cin>>baru->harga;
baru->next=NULL;
baru->prev=NULL;
if(head==NULL)
{
head=baru;
}
else
{
tail->next=baru;
baru->prev =
tail;
}
tail=baru;
}
void tambah_tengah(int cari)
{
baru=new data;
after=head;
if(head==NULL)
{
cout<<"
Data kosong "<<endl;
}
else{
cout<<"
jenis :
";cin>>baru->jenis;
cout<<"
umur : ";cin>>baru->umur;
cout<<"
jumlah : ";cin>>baru->jumlah;
cout<<"
harga :
";cin>>baru->harga;
baru->next=NULL;
baru->prev=NULL;
for(int x=1;x<cari-1;x++)
{
after=after->next;
}
baru->prev=after;
baru->next=after->next;
after->next->prev=baru;
after->next=baru;
}
}
void hapus_depan()
{
hapus=head;
if(head==NULL)
{
cout<<"
Data kosong"<<endl;
}else{
head=hapus->next;
hapus->next->prev=NULL;
hapus=NULL;
}
}
void hapus_belakang()
{
hapus=tail;
if(head==NULL)
{
cout<<"
Data kosong"<<endl;
}else{
tail->prev->next=NULL;
tail=hapus->prev;
hapus=NULL;
}
}
void hapus_tengah(int cari)
{
data *temp;
temp=head;
after=head;
int bantu=0;
if(head==NULL)
{
cout<<"
Data masih kosong"<<endl;
}else{
while(temp!=NULL)
{
bantu++;
temp=temp->next;
}
if(bantu<3)
{
cout<<" Tidak ada data tengah"<<endl;
}
else{
for(int
x=1;x<cari-1;x++)
{
after=after->next;
}
after->next->prev=after->prev;
after->prev->next=after->next;
after=NULL;
}
}
}
void view()
{
baru=head;
if(head==NULL)
{
cout<<"
Data kosong\n";
}else{
cout<<"
Output dari depan"<<endl;
while(baru!=NULL)
{
cout<<" jenis :
"<<baru->jenis<<endl;
cout<<" umur :
"<<baru->umur<<endl;
cout<<" jumlah :
"<<baru->jumlah<<endl;
cout<<" harga :
"<<baru->harga<<endl;
cout<<"_______________________\n";
baru=baru->next;
}
baru=tail;
cout<<"
Output dari belakang "<<endl;
while(baru!=NULL)
{
cout<<" jenis :
"<<baru->jenis<<endl;
cout<<" umur : "<<baru->umur<<endl;
cout<<" jumlah : "<<baru->jumlah<<endl;
cout<<" harga : "<<baru->harga<<endl;
cout<<"_______________________\n";
baru=baru->prev;
}
}
}
main()
{
int pilih,cari;
do
{
cout<<"\n1. Tambah depan \n";
cout<<"2. Tambah belakang \n";
cout<<"3. Tambah tengah \n";
cout<<"4. Hapus depan
\n";
cout<<"5. Hapus
belakang\n";
cout<<"6. Hapus
tengah\n";
cout<<"7. view\n";
cout<<"pilih : ";
cin>>pilih;
switch(pilih)
{
case 1:
tambah();
break;
case 2:
tambah_belakang();
break;
case 3:
cout<<"input setelah data ke- ? ";cin>>cari;
tambah_tengah(cari);
break;
case 4:
hapus_depan();
break;
case 5:
hapus_belakang();
break;
case 6:
cout<<"hapus data ke- ? ";cin>>cari;
hapus_tengah(cari);
break;
case 7:
view();
break;
}
}
while(pilih<8);
}
Comments
Post a Comment