Deskripsi : Membuat InsertFirst, InsertLast, DeleteFirst, DeleteLast
#include"iostream"
using namespace std;
typedef struct data* pointer;
typedef pointer List;
struct data
{
int info;
pointer next;
};
void createElement(pointer& pBantu);
void InsertFirst(List& First, pointer pBaru);
void InsertLast(List& First, pointer pBaru);
void DeleteFirst(List& First, pointer pHapus);
void DeleteLast(List& First, pointer pHapus);
void traversal(List First);
void createElement(pointer& pBantu)
{
pBantu=new data;
cout<<"info = ";cin>>pBantu->info;
pBantu->next=0;
}
void InsertFirst(List& First, pointer pBaru)
{
if (First==0)
{
First=pBaru;
}
else {
pBaru->next=First;
First=pBaru;
}
}
void InsertLast(List& First, pointer pBaru)
{
pointer Last;
if(First==0)
{
First=pBaru;
}
else
{
Last=First;
while(Last->next!=0)
{
Last=Last->next;
}
Last->next=pBaru;
}
}
void DeleteFirst(List& First, pointer pHapus)
{
if (First==0)
{
pHapus=0;
cout<<"list kosong";
}
else if (First->next==0)
{
pHapus=First;
First=0;
}
else
{
pHapus=First;
First=First->next;
pHapus->next=0;
}
}
void DeleteLast(List& First, pointer pHapus)
{
pointer Last,precLast;
if(First==0)
{
pHapus=0;
cout<<"list kosong";
}
else if(First==0)
{
pHapus=First;
First=0;
}
else
{
Last=First->next;
precLast=First;
while(Last->next!=0)
{
precLast=Last;
Last=Last->next;
}
}
pHapus=Last;
precLast->next->next=0;
}
void traversal(List First)
{
pointer pBantu;
pBantu=First;
while(pBantu!=0)
{
cout<<"info : "<<pBantu->info<<endl;
pBantu=pBantu->next;
}
}
int main()
{
List First=0;
pointer p1,pHapus;
int pilih;
char pilih2;
do
{
system("cls");
cout<<"MENU :";
cout<<"\n1.Insert First";
cout<<"\n2.Insert Last";
cout<<"\n3.DeleteFirst";
cout<<"\n4.DeleteLast";
cout<<"\n5.Cetak";
cout<<"\npilih : ";cin>>pilih;
switch(pilih)
{
case 1:
{
cout<<"\nMasukkan data :"<<endl;
createElement(p1);
InsertFirst(First,p1);
break;
}
case 2:
{
cout<<"\nMasukkan data :"<<endl;
createElement(p1);
InsertLast(First,p1);
break;
}
case 3:
{
cout<<"DeleteFirst"<<endl;
DeleteLast(First,pHapus);
traversal(First);
break;
}
case 4:
{
cout<<"DeleteLast"<<endl;
DeleteFirst(First,pHapus);
traversal(First);
break;
}
case 5:
{
cout<<endl<<endl<<"outputnya :"<<endl<<endl;
traversal(First);
break;
}
default:
{
cout<<"\nmenu yang anda masukkan salah!";
break;
}
}
cout<<"\n\nakan dilanjutkan(y/n)?";cin>>pilih2;
}
while(pilih2=='y');
cout<<"Selesai";
return 0;
}
#include"iostream"
using namespace std;
typedef struct data* pointer;
typedef pointer List;
struct data
{
int info;
pointer next;
};
void createElement(pointer& pBantu);
void InsertFirst(List& First, pointer pBaru);
void InsertLast(List& First, pointer pBaru);
void DeleteFirst(List& First, pointer pHapus);
void DeleteLast(List& First, pointer pHapus);
void traversal(List First);
void createElement(pointer& pBantu)
{
pBantu=new data;
cout<<"info = ";cin>>pBantu->info;
pBantu->next=0;
}
void InsertFirst(List& First, pointer pBaru)
{
if (First==0)
{
First=pBaru;
}
else {
pBaru->next=First;
First=pBaru;
}
}
void InsertLast(List& First, pointer pBaru)
{
pointer Last;
if(First==0)
{
First=pBaru;
}
else
{
Last=First;
while(Last->next!=0)
{
Last=Last->next;
}
Last->next=pBaru;
}
}
void DeleteFirst(List& First, pointer pHapus)
{
if (First==0)
{
pHapus=0;
cout<<"list kosong";
}
else if (First->next==0)
{
pHapus=First;
First=0;
}
else
{
pHapus=First;
First=First->next;
pHapus->next=0;
}
}
void DeleteLast(List& First, pointer pHapus)
{
pointer Last,precLast;
if(First==0)
{
pHapus=0;
cout<<"list kosong";
}
else if(First==0)
{
pHapus=First;
First=0;
}
else
{
Last=First->next;
precLast=First;
while(Last->next!=0)
{
precLast=Last;
Last=Last->next;
}
}
pHapus=Last;
precLast->next->next=0;
}
void traversal(List First)
{
pointer pBantu;
pBantu=First;
while(pBantu!=0)
{
cout<<"info : "<<pBantu->info<<endl;
pBantu=pBantu->next;
}
}
int main()
{
List First=0;
pointer p1,pHapus;
int pilih;
char pilih2;
do
{
system("cls");
cout<<"MENU :";
cout<<"\n1.Insert First";
cout<<"\n2.Insert Last";
cout<<"\n3.DeleteFirst";
cout<<"\n4.DeleteLast";
cout<<"\n5.Cetak";
cout<<"\npilih : ";cin>>pilih;
switch(pilih)
{
case 1:
{
cout<<"\nMasukkan data :"<<endl;
createElement(p1);
InsertFirst(First,p1);
break;
}
case 2:
{
cout<<"\nMasukkan data :"<<endl;
createElement(p1);
InsertLast(First,p1);
break;
}
case 3:
{
cout<<"DeleteFirst"<<endl;
DeleteLast(First,pHapus);
traversal(First);
break;
}
case 4:
{
cout<<"DeleteLast"<<endl;
DeleteFirst(First,pHapus);
traversal(First);
break;
}
case 5:
{
cout<<endl<<endl<<"outputnya :"<<endl<<endl;
traversal(First);
break;
}
default:
{
cout<<"\nmenu yang anda masukkan salah!";
break;
}
}
cout<<"\n\nakan dilanjutkan(y/n)?";cin>>pilih2;
}
while(pilih2=='y');
cout<<"Selesai";
return 0;
}
Tidak ada komentar:
Posting Komentar