Salah Satu Program di C++ adalah Linked List. Nah Sekarang saya memposting program C++ dengan Single Linked List. Perlu diketahui, Linked List adalah sejumlah simpul/node yang dikaitkan dengan simpul laim dengan bantuan pointer. Untuk Single Linked List, adalah :inked List yang hanya ada satu pointer yang menghubungkan setiap simpul/node. Berikut adalah codingnya :
#include <iostream>
#include <conio.h>
#include <stdlib.h>
typedef struct node *simpul;
struct node
{
char isi;
simpul next;
};
//==prototype function
void sisip_depan(simpul &l, char elemen);
void sisip_belakang(simpul &l, char elemen);
void sisip_tengah1(simpul &l, char elemen1, char elemen2);
void sisip_tengah2(simpul &l, char elemem1, char elemen2);
void hapus_depan(simpul &l);
void hapus_tengah(simpul &l, char elemen);
void hapus_belakang(simpul &l);
void cetak(simpul l);
//==fungction main
using namespace std;
int main()
{
char huruf,huruf2;
simpul l=NULL; //pastikan bahwa l kosong
cout<<"\n\tPROGRAM C++\n";
cout<<"\nOPERASI PADA SINGLE LINKED LIST\n\n";
//sisip depan
cout<<"\nPenyisipan Simpul Di Depan\n\n";
cout<<"\nMasukkan huruf : ";cin>>huruf;
sisip_depan(l,huruf);
cout<<"\nMasukkan huruf : ";cin>>huruf;
sisip_depan(l,huruf);
cout<<"\nMasukkan huruf : ";cin>>huruf;
sisip_depan(l,huruf);
cout<<"\nMasukkan huruf : ";cin>>huruf;
sisip_depan(l,huruf);
cetak(l);
//sisip belakang
cout<<"\n\nPenyisipan Simpul Di Belakang\n\n";
cout<<"\nMasukkan huruf : ";cin>>huruf;
sisip_belakang(l,huruf);
cout<<"\nMasukkan huruf : ";cin>>huruf;
sisip_belakang(l,huruf);
cout<<"\nMasukkan huruf : ";cin>>huruf;
sisip_belakang(l,huruf);
cout<<"\nMasukkan huruf : ";cin>>huruf;
sisip_belakang(l,huruf);
cetak(l);
//sisip simpul setelah simpul tertentu
cout<<"\n\nMasukkan huruf yang akan disisipkan : ";cin>>huruf;
cout<<"\nDisisip setelah huruf : ";cin>>huruf2;
cout<<endl;
cout<<huruf<<" disisip setelah "<<huruf2<<endl;
sisip_tengah1(l,huruf,huruf2);
cetak(l);
//sisip simpul sebelum simpul tertentu
cout<<"\n\nMasukkan huruf yang akan disisipkan : ";cin>>huruf;
cout<<"\nDisisip sebelum huruf : ";cin>>huruf2;
cout<<endl;
cout<<huruf<<" disisip sebelum "<<huruf2<<endl;
sisip_tengah2(l,huruf,huruf2);
cetak(l);
//hapus simpul depan
cout<<"\n\nSetelah Hapus Simpul Depan\n";
hapus_depan(l);
cetak(l);
//hapus simpul belakang
cout<<"\n\nSetelah Hapus Simpul Belakang\n";
hapus_belakang(l);
cetak(l);
//hapus simpul tengah
cout<<"\n\nMasukkan Huruf Tengah Yang Akan Dihapus : ";cin>>huruf;
hapus_tengah(l,huruf);
cetak(l);
getch();
}
//fungsi-fungsi
//fungsi sisip simpul depan
void sisip_depan(simpul &l, char elemen)
{
simpul baru; // = new simoul
baru = (simpul) malloc(sizeof(simpul));
baru->isi = elemen;
baru->next = NULL;
if(l == NULL)
l=baru;
else{
baru->next = l;
l=baru;}
}
//fungsi sisip simpul setelah simpul tertentu
void sisip_tengah1(simpul &l,char elemen1,char elemen2)
{
simpul bantu, baru;
baru=(simpul)malloc(sizeof(simpul));
baru->isi=elemen1;
baru->next=NULL;
if(l==NULL)
cout<<"list kosong \n";
else{
bantu=l;
while(bantu->isi!=elemen2) bantu=bantu->next;
baru->next=bantu->next;
bantu->next=baru;}
}
//fungsi sisip simpul sebelum simpul tertentu
void sisip_tengah2(simpul &l, char elemen1,char elemen2)
{
simpul bantu, baru;
baru=(simpul)malloc(sizeof(simpul));
baru->isi=elemen1;
baru->next=NULL;
if(l==NULL)
cout<<"list kosong \n";
else{
bantu = l;
while(bantu->next->isi!=elemen2) bantu=bantu->next;
baru->next=bantu->next;
bantu->next=baru;
}
}
void sisip_belakang(simpul &l, char elemen)
{
simpul bantu, baru;
baru=(simpul) malloc(sizeof(simpul));
baru->isi=elemen;
baru->next=NULL;
if(l==NULL)
l=baru;
else{
bantu=l;
while(bantu->next!=NULL)
bantu=bantu->next;
bantu->next=baru;}
}
//fungsi mencetak isi llinked list
void cetak(simpul l)
{
simpul bantu;
if(l==NULL)
cout<<"\nlinked list kosong \n";
else{
bantu=l;
cout<<"\nIsi linked list : ";
while(bantu->next!=NULL)
{
cout<<bantu->isi<<"-->";
bantu=bantu->next;
}
cout<<bantu->isi;
cout<<endl;
}
}
//fungsi hapus simpul depan
void hapus_depan(simpul &l)
{
simpul hapus;
if(l==NULL)
cout<<"\nLinked List Kosong"<<endl;
else
{
hapus=l;
l=l->next;
hapus->next=NULL;
free(hapus);
}
}
//fungsi hapus simpul belakang
void hapus_belakang(simpul &l)
{
simpul bantu, hapus;
if(l==NULL)
cout<<"\nLinked List Kosong"<<endl;
else
{
bantu=l;
while(bantu->next->next!=NULL) bantu=bantu->next;
bantu->next=NULL;
free(hapus);
}
}
//fungsi hapus simpul di tengah
void hapus_tengah(simpul &l, char elemen)
{
simpul bantu, hapus;
if(l==NULL)
cout<<"\nLinked List Kosong"<<endl;
else
{
bantu=l;
while(bantu->next->isi!=elemen) bantu=bantu->next;
hapus=bantu->next;
bantu->next=bantu->next->next;
hapus->next=NULL;
free(hapus);
}
}
Cukup Rumit memang, namun dengan belajar dan berlatih, maka semua menjadi mudah. Semoga Bermanfaat Kawan !
Postingan
26 June 2015
Program C++ Single Linked List
Penulis Ady Rangga Hidayat
Diterbitkan 12:16 AM
Tags
Artikel Terkait
Subscribe to:
Post Comments (Atom)
1 komentar so far
keren gan, membantu gue banget di kampus
kunjungi juga gan punya ane di http://moviemindstill.blogspot.co.id/p/stack-tunggal.html
EmoticonEmoticon