Thursday 29 November 2012

Singly Circular Linked List


/*Singly Circular Linked List
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class csingly
{
 struct cs_node
 {
  int data;
  cs_node *next;
 }*last;
 int num,pos,choice;
 public:
 void creation();
 void add_beginning();
 void add_after();
 void deletion();
 void display();
 void count();
 csingly();
};

void csingly::creation()
{
 cout<<"enter element in list:";
 cin>>num;
 cs_node *q,*t;
 t=new cs_node[sizeof(cs_node)];
 t->data=num;
 if(last==NULL)
 {
  last=t;
  t->next=last;
 }
 else
 {
  t->next=last->next;
  last->next=t;
  last=t;
 }
}

void csingly::add_beginning()
{
 cout<<"Enter item to add in list at begining";
 cin>>num;
 cs_node *t;
 t=new cs_node[sizeof(cs_node)];
 t->data=num;
 t->next=last->next;
 last->next=t;
}

void csingly::add_after()
{
 cout<<"enter position after which u want to add:";
 cin>>pos;
 cout<<"\nEnter item:";
 cin>>num;
 cs_node *q,*t;
 q=last->next;
 if(last==NULL)
 {
  cout<<"\nList is empty";
  return;
 }
 for(int i=1;i<pos;i++)
 {
  q=q->next;
  if(q==last->next)
  {
   cout<<"\nThere are less than "<<pos<<" elements";
   return;
  }
 }
 t=new cs_node[sizeof(cs_node)];
 t->data=num;
 t->next=q->next;
 q->next=t;
 if(q==last)
 last=t;
}

void csingly::deletion()
{
 int num;
 cout<<"Enter item which you want to Delete :";
 cin>>num;
 cs_node *q,*t;
 if(last==NULL)
 {
  cout<<"list is empty\n";
  return;
 }
 if(last->data==num)
 {
  if(last->next==last)
  {
   t=last;
   last=NULL;
   cout<<"Item is removed";
   delete t;
   return;
  }
 }
 q=last->next;
 if(q->data==num)
 {
  t=q;
  last->next=q->next;
  cout<<"Item is removed";
  delete t;
  return;
 }
 while(q->next!=last)
 {
  if(q->next!=last)
  {
   t=q->next;
   q->next=last->next;
   delete t;
   last=q;
   cout<<"Item "<<num<<" is removed";
   return;
  }
 }
 if(q->next->data==num)
 {
  t=q->next;
  q->next=last->next;
  delete t;
  last=q;
  cout<<"Item "<<num<<" is removed\n";
  return;
 }
 cout<<"Item "<<num<<" is not found\n";
}

void csingly::display()
{
 cs_node *q;
 if(last==NULL)
 {
  cout<<"List is empty";
  return;
 }
 else
 {
  cout<<"\nItem in circular list:";
  q=last->next;
  do
  {
   cout<<q->data<<"  ";
   q=q->next;
  }while(q!=last);
 }
}

void csingly::count()
{
 cs_node *q;
 int n=0;
 q=last->next;
 while(q!=last)
 {
  n++;
  q=q->next;
 }
 cout<<"\nNumber of nodes in list= "<<n;
}

csingly::csingly()
{
 last=NULL;
 int choice;
 while(1)
 {
  cout<<"\n\t\tOperations on Doubly linked List..\n";
  cout<<"\t\t1. To Create a linked list\n";
  cout<<"\t\t2. To Insert an Element bebinning\n";
  cout<<"\t\t3. To insert after\n";
  cout<<"\t\t4. To Delete the Elements of  linked list\n";
  cout<<"\t\t5. To Display the elements\n";
  cout<<"\t\t6. To count the nodes\n";
  cout<<"\t\t7. To Main menu\n";
  cout<<"\t\t8. To Exit from Programme\n";
  cout<<"\n\t\t Enter Your Choice= ";
  cin>>choice;
  switch(choice)
  {
   case 1 : creation();break;
   case 2 : add_beginning();break;
   case 3 : add_after();break;
   case 4 : deletion();break;
   case 5 : display();break;
   case 6 : count();break;
   case 7 : return;
   case 8 : exit(1);
  }
 }
}
/*=========================11Nov.,2010=============================*/

No comments:

Post a Comment

You are welcome, your comment helps me to make this blog more better.