circular linked list

in this post I will make an example of circular link list program. for understanding you can find on other sites. Well here’s an example program.

 

//Circular Linked List in CPP
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#define null 0
struct node{
int info;
struct node *link;
public:
}
*start,*last;


int main(){
int ch,n,m,position,i;
void create(int);
void addat(int);
void addbt(int,int);
void del(int);
void disp();
last=null;
//clrscr();
while(1){
cout<<“\nMAIN MENU”;
cout<<“\n1.Buat\n2.Tambah depan\n3.Tambah belakang\n4.Hapus\n5.Tampilkan\n6.Keluar”;
cout<<“\n\nMasukkan pilihan anda : “;
cin>>ch;
switch(ch)
{
case 1:
cout<<“\n\nMasukkan jumlah elemen : “;
cin>>n;
for(i=0;i<n;i++)
{
cout<<“\n\nMasukkan Elemen : “;
cin>>m;
create(m);
}break;
case 2:
cout<<“\n\nMasukkan Elemen : “;
cin>>m;
addat(m);
break;
case 3:
cout<<“\n\nMasukkan Elemen : “;
cin>>m;
cout<<“\n\nMasukkan Posisinya : “;
cin>>position;
addbt(m,position);
break;
case 4:
if(last==null)
{
cout<<“\n\nList kosong”;
continue;
}
cout<<“\n\nMasukkan posisi yang akan dihapus : “;
cin>>m;
del(m);
break;
case 5:
disp();
break;
case 6:
exit(0);
break;
default:
cout<<endl<<endl<<“Pilihan yang salah”;
}
}
}
void create(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=null;
if(last==null)
{
last=tmp;
tmp->link=last;
}
else
{
tmp->link=last->link;
last->link=tmp;
last=tmp;
}
return;
}
void addat(int data){
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
}
void addbt(int data,int pos){
struct node *tmp,*q;
int i;
q=last->link;;
for(i=0;i<pos-1;i++)
{
q=q->link;
if(q==last->link)
{
cout<<“\n\nDimana r kurang dari “<<pos<<“elemen “;
return;
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->link=q->link;
tmp->info=data;
q->link=tmp;
if(q==last)
last=tmp;
}
void del(int data)
{
struct node *tmp,*q;
if(last->link==last&&last->info==data)
{
tmp=last;
last=null;
free(tmp);
cout<<“\n\nElemen “<<data<” dihapus”;
return;
}
q=last->link;
if(q->info==data)
{
tmp=q;
last->link=q->link;
free(tmp);
cout<<“\n\nElemen “<<data<” dihapus “;
return;
}
while(q->link!=last)
{
if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
cout<<“\n\nElemen “<<data<<” dihapus “;
return;
}
q=q->link;
}
if(q->link==last)
{
if(q->link->info==data)
{
tmp=last;
last=q;
last->link=tmp->link;
free(tmp);
cout<<“\n\nElemen “<<data<<” dihapus “;
return;
}
}

cout<<“\n\nElemen tidak ditemukan “;

}

void disp()
{
struct node *q;
if(last==null)
{
cout<<“\n\nList kosong”;
return;
}q=last->link;
cout<<endl;
while(q!=last)
{
cout<<q->info<<” “;
q=q->link;
}
cout<<last->info;
}

 

Please try🙂
if there is an error please leave a message.
thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: