Tuesday, 17 September 2013

Circuler List

HTML Online Editor Sample

 

#include<iostream>

using namespace std;
class circularlist
{
private:
struct node
{int  element;
node* next;
}*head;
public:
circularlist();
void insert(int element);
int remove(int element);
bool isempty();
void search(int key);
int  sizeoflist();
void display();
~circularlist();};
circularlist::circularlist()
{head=NULL;}
bool  circularlist::isempty()
{if(head==NULL)
return true;
else return false;}
 
void circularlist::insert(int element)
{if(isempty())
{
head=new node;
head->element=element;
head->next=head;
return;
}
node* curr=head;
while(curr->next!=head)
curr=curr->next;
node* t=new node;
t->element=element;
t->next=head;
curr->next=t;
return;
}
void circularlist::search(int key)
{ int count=1;
  if(isempty())
  {cout<<"\n list is empty";return;}node* curr;
curr=head->next;
if(head->element==key){
cout<<"\n element found at::\t"<<count;
return;}
count++;
while(curr!=head)
{if(curr->element==key)
  {
    cout<<"\n element found at::\t"<<count;
    return;
  }
  curr=curr->next;
  count++;
}
cout<<"\n sorry!!!elemet is not present\n";
}
int circularlist::remove(int element)
{
if(isempty())
{
  cout<<"\n list is empty  ";
  return -10000;}
  if(head->element==element)
  {
    if(head->next==head)
    {    int data=head->element;delete head;
    head=NULL;
    return data;}
 
    node*t=head;
  node* curr;
  curr=head;
  head=head->next;
 
  while(curr->next!=t)
  curr=curr->next;
  curr->next=head;
  int data;
  data=t->element;
  delete(t);
  return data;}
  node* curr;
  node* pcurr;
  curr=head->next;
  pcurr=head;
  while(curr!=head)
  {
    if(curr->element==element){
    if(curr->next==head)
    {
      pcurr->next=head;
      int data=curr->element;
      delete(curr);
      return data;
    }
 
    pcurr->next=curr->next;
    int data=curr->element;
    delete(curr);
    return data;
 
  }
  pcurr=curr;
  curr=curr->next;
 
} return -20000;}
void circularlist::display()
{
  if(isempty()){
  cout<<"\n list is empty";return;}
  node *curr;
curr=head;
cout<<"->"<<curr->element<<"->";
curr=head->next;
while(curr!=head)
{cout<<"->"<<curr->element<<"->";
curr=curr->next;}}
int circularlist::sizeoflist()
{ int count=0;if(isempty())
{return 0;
 
}
node* curr=head;
count++;
curr=head->next;
while(curr!=head)
{
  curr=curr->next;
  count++;
}
return count;
}
circularlist::~circularlist()
{
}
int main()
{
  circularlist cl;
  int a,b,c,d,e,f;
  char ch;
  cout<<"\n\t\t******WELCOME TO CIRCULER LIST OPERATION*******\n";
  do
  {cout<<"\t\t\tpress 1 to insert in list\n\t\t\t press 2 to display the list\n\t\t\tpress 3 to remove from lis\n\t\t\tpress 4 to  find the length of the lis\n\t\t\tpress 5 to search an element\n\t\t\t\n";
  cout<<"\n enter your choice\t\t";
  cin>>a;
  switch(a)
  {
    case 1:cout<<"\n enter the element you want to insert\t\t";
    cin>>b;
    cl.insert(b);
    break;
    case 2:cout<<"\n____________________________________________\n";
    cl.display();
    cout<<"\n________________________________________________";
    break;
    case 3:cout<<"\n enter the element you want to delete from list::\t\t";
  cin>>c;
    d=cl.remove(c);
    if(d==-10000)
    cout<<"\n list is empty";
    if(d==-20000)
    cout<<"\n element is not present";
    if(d!=-10000&&d!=-20000)
    cout<<"\n\t\t"<<d<<"has been removed from list";
    break;
    case 5:
    cout<<"\n enter the element you want to find\t\t";
    cin>>e;
    cl.search(e);
    break;
    case 4:
    f=cl.sizeoflist();
    cout<<"\n size of list is\t\t"<<f;
    break;
    default:
    cout<<"\n you selested wromg choice";
 
  }
cout<<"\n do you want to continue if yes press y";
cin>>ch;}
while(ch=='y');
  return 0;
}

No comments:

Post a Comment