Program for the implementation of Circular Linked List in C++
language. In this program you can insert in circular link list, although
the output seems as the singular link list but the code is written for
circular list. Further addition if circular display function for viewing
the operation don in the list. In Circular Linked List ending node not
having the null value. Last node again point to starting node that's why
it called as Circular Linked list.
Source Code:-
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
struct node
{int info;
struct node *next;
}*start, *temp,*current;
void circular(int);
void circulardisplay();
void main()
{clrscr();
start=NULL;
int item,choice,location,element,position;
cout<<endl<<"Circular Singly Linked List by-Tarun Rawat\n";
again:
cout<<"\n1.Insert Item in Circular Linked List\n2.Display circular Linked List \n3.Exit\n";
cout<<"Enter choice : ";
cin>>choice;
switch(choice)
{case 1:cout<<"Enter item to insert: ";
cin>>item;
circular(item);
goto again;
case 2:cout<<"\nInserted item = ";
circulardisplay();cout<<"\n";
goto again;
case 3:cout<<"\nTHANK YOU";
default:break;
}
getch();
}
void circular(int item)
{temp=(node*)malloc(sizeof(node));
temp->info=item;
temp->next=start;
node* current;
if(start==NULL)
{start=temp;
temp->next=start;
}
else
{current=start;
while(current->next!=start)
{current=current->next;
}
current->next=temp;
}
}
void circulardisplay()
{current= start;
do{
cout<<current->info<<" ";
current=current->next;
}while(current!=start);
}
Source Code:-
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
struct node
{int info;
struct node *next;
}*start, *temp,*current;
void circular(int);
void circulardisplay();
void main()
{clrscr();
start=NULL;
int item,choice,location,element,position;
cout<<endl<<"Circular Singly Linked List by-Tarun Rawat\n";
again:
cout<<"\n1.Insert Item in Circular Linked List\n2.Display circular Linked List \n3.Exit\n";
cout<<"Enter choice : ";
cin>>choice;
switch(choice)
{case 1:cout<<"Enter item to insert: ";
cin>>item;
circular(item);
goto again;
case 2:cout<<"\nInserted item = ";
circulardisplay();cout<<"\n";
goto again;
case 3:cout<<"\nTHANK YOU";
default:break;
}
getch();
}
void circular(int item)
{temp=(node*)malloc(sizeof(node));
temp->info=item;
temp->next=start;
node* current;
if(start==NULL)
{start=temp;
temp->next=start;
}
else
{current=start;
while(current->next!=start)
{current=current->next;
}
current->next=temp;
}
}
void circulardisplay()
{current= start;
do{
cout<<current->info<<" ";
current=current->next;
}while(current!=start);
}
No comments:
Post a Comment