Data Structures through C & C++ for beginners

If the code doesn't work, please replace the single quotes and double quotes(Actually these are not proper single and double quotes) in the code with single quotes and double quotes using your keyboard..

OPERATIONS ON CIRCULAR LINKED LIST

Posted by Vinod on January 12, 2007


/*********************************************************
-> This C++ program is to perform the following operations
   on a circular linked list
   1)insertion
   2)forward traversal
   3)reverse traversal
   4)search

->node structure :
  a node contains
  1) integer data
  2) pointer to next node

-> This program works in microsoft VC++ environment
   in windows xp

-> This program uses the following header files
   1)iostream.h
********************************************************/

#include<iostream.h>

class cll
{
private:
 int data;
 cll *next;
public:
 cll* insert_one(int,cll*);
 cll* delete_one(int,cll*);
 void ftraverse(cll*);
 void rtraverse(cll*);
 void search(int,cll*);
 void function();
};

cll*hd;

void cll::function()
{
 cout<<”******************************************\n”;
 cout<<”program to implement a circular linked list \n”;
 cout<<”******************************************\n”;

 cll * head;
 head=NULL;

 cout<<”\n\nMENU :\n”;
 cout<<”1)insertion\n”
  <<”2forward traversal\n”
  <<”3)reverse traversal\n”
  <<”4)search\n”
  <<”5)exit\n\n”;
 cout<<”Enter your option :”;
 int opt;
 cin>>opt;
 int d;

 while(opt!=5)
 {
  switch(opt)
  {
   case 1:
   cout<<”Enter data to the node :”;
   cin>>d;
   head=insert_one(d,head);
   cout<<”inserted\n”;
   break;
   case 2:
   cout<<”The forward traversal is :\n”;
   ftraverse(head);
   break;
   case 3:
   cout<<”The reverse traversal is :\n”;
   hd=head;
   rtraverse(head);
   cout<<”NULL\n”;
   break;
   case 4:
   cout<<”Enter the element to be searched :”;
   int d;
   cin>>d;
   search(d,head);
   break;
   case 5:
   break;
   default:
   cout<<”invalid option”;
   break;
  }
  cout<<”\n\nMENU :\n”;
  cout<<”1)insertion\n”
  <<”2)forward traversal\n”
  <<”3)reverse traversal\n”
  <<”4)search\n”
  <<”5)exit\n\n”;
  cout<<”Enter your option :”;
  cin>>opt;
 }
}
cll* cll::insert_one(int d,cll* c)
{
 cll*NEW;
 NEW=new cll;
 NEW->data=d;
 NEW->next=NULL;

 if(c==NULL)
 {
  c=NEW;
  c->next=c;
 }
 else
 {
  cll*c1=c;
  while(c1->next!=c)
  c1=c1->next;
  c1->next=NEW;
  NEW->next=c;
 }
 return c;
}

void cll::ftraverse(cll* c)
{
 if(c==NULL)
 {
  cout<<”\nlist empty\n”;
  return;
 }
 else
 {
  cll *c1=c;
  cout<<c1->data<<”->”;
  c1=c1->next;
  while(c1!=c)
  {
   cout<<c1->data<<”->”;
   c1=c1->next;
  }
  cout<<”NULL\n”;
 }
}

void cll::rtraverse(cll* c)
{
 if(c->next==hd)
 {
  cout<<c->data<<”->”;
  return;
 }
 else
  rtraverse(c->next);
 cout<<c->data<<”->”;
}

void cll::search(int d,cll* c)
{
 cll*c1=c;
 if(c==NULL)
 {
  cout<<”\nlist empty\n”;
  return;
 }
 else
 {
  if(c->data == d)
  {
   cout<<”found\n”;
   return ;
  }
  while(c->next !=c1)
  {
   if(c->data==d)
   {
    cout<<”found\n”;
    return ;
   }
   c=c->next;
  }
  if(c->data ==d)
  {
   cout<<”found\n”;
   return ;
  }
  cout<<” search unsuccess ful \n”;
 }
}

void main()
{
 cll list;
 list.function();
}

16 Responses to “OPERATIONS ON CIRCULAR LINKED LIST”

  1. ferancis said

    please i need the ansewer of a c++ progra using circular linked list . the quation is to remove , find,print,findkth,insert,input.keep in touch,

  2. rahul said

    it will be so kind of u if u will mention theory parts of data structure and c and c++

  3. gil olmedo said

    hi… have a nice day…….
    ahm’ can I ask for your help?
    plz help me in my final project in data structure…..
    it’s required an application.

  4. Rucha said

    Hello
    I just want a program in C that accepts two circular Linked Lists in C ,Displays them,Sorts them and merges them

  5. Richa said

    hello ,can you help me.
    i want proper algo. to reverse a circular linked list .
    pleaser eply early b’coz i have my exam on saturday.

  6. shivani said

    could u please give the algorithm to traverse the circularly linked list and doubly linked list

  7. shivani said

    plz reply coz i hav got exams after 2 days

  8. afifa said

    i want the delete function in link lisyt in such a way that we can delete the alternative linklists

  9. AMMY said

    PLZ i need al topics of DATA STUCTURE(DS) .
    help me!!!!!!!……

  10. AMMY said

    I need all topic of DS .
    help me out plz!!!!!!!!!……

  11. ruchika said

    heloo sir/madam….
    i need all the topics of DS.plz provide the content with the figures to understand vry easily …
    i dnt knw anythng abt DS …
    iam interested to learn from the basics of this.. plz help me…
    thank you
    ………

  12. shiny francis said

    i need simple algorithm of circular linked list

  13. aishu said

    pgm for circular linked in c

  14. aishu said

    pgm for circular linked list in c

  15. sirisha said

    i need a theory part to explain theory part here

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 )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 76 other followers