Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   circular link list (http://www.go4expert.com/forums/circular-link-list-t14561/)

vg_2007 15Oct2008 21:12

circular link list
 
HEY GUYS CHECK IT OUT AND PLZ REPLY ON HOW IT IS.

Code:

#include <iostream.h>
#include<conio.h>
#include<process.h>

struct node
{
int info;
node *link;
};
node *start=NULL;
node *last=NULL;

void display();
void ins_beg();
void ins_end();
void ins_pos();
void del_beg();
void del_end();
void del_item();
void search();
void rev();

void main()
{
int ch,ch1,ch2;
clrscr();
do
{
cout<<'\n';
cout<<'\n';
cout<<"\t  \n";
cout<<'\n';
cout<<"\t\t MAIN MENU \n";
cout<<'\n';
cout<<"\t\t 1. DISPLAY LINK LIST \n";
cout<<'\n';
cout<<"\t\t 2. INSERT IN LINK LIST \n";
cout<<'\n';
cout<<"\t\t 3. DELETE FROM LINK LIST \n";
cout<<'\n';
cout<<"\t\t 4. SEARCH THE LINK LIST \n";
cout<<'\n';
cout<<"\t\t 5. REVERSE THE LINK LIST \n";
cout<<'\n';
cout<<"\t\t 6. EXIT \n";
cout<<'\n';
cout<<"\t ENTER YOUR CHOICE FROM ABOVE OPTIONS ";
cin>>ch;
switch(ch)
{
case 1 :
      //DISPLAY THE LINK LIST
      display();
      break;
case 2 :
      //INSERT INTO LINK LIST
      clrscr();
      do
      {
      cout<<'\n';
      cout<<"\t\n";
      cout<<'\n';
      cout<<"\t\t  INSERTION MENU        \n";
      cout<<'\n';
      cout<<"\t\t  1. INSERT IN BEGINNING  \n";
      cout<<'\n';
      cout<<"\t\t  2. INSERT IN END        \n";
      cout<<'\n';
      cout<<"\t\t  3. INSERT IN POSITION    \n";
      cout<<'\n';
      cout<<"\t\t  4. EXIT                  \n";
      cout<<'\n';
      cout<<"\t ENTER YOUR CHOICE FROM ABOVE OPTIONS  \n";
      cin>>ch1;
      switch(ch1)
      { case 1 :
                //ins_beg():
                break;
        case 2 :
                // ins_end();
                break;
        case 3 :
                // ins_pos();
                break;
        }
        }while(ch1<4);
        break;
case 3:
        //DELETION IN CIRCULAR LINK LIST
        clrscr();
        do
      {
      cout<<'\n';
      cout<<"\t \n";
      cout<<'\n';
      cout<<"\t\t  DELETION MENU        \n";
      cout<<'\n';
      cout<<"\t\t  1. DELETE FROM BEGINNING  \n";
      cout<<'\n';
      cout<<"\t\t  2. DELETE FROM  END        \n";
      cout<<'\n';
      cout<<"\t\t  3. DELETE AN ITEM    \n";
      cout<<'\n';
      cout<<"\t\t  4. EXIT                  \n";
      cout<<'\n';
      cout<<"\t ENTER YOUR CHOICE FROM ABOVE OPTIONS  \n";
      cin>>ch2;
      switch(ch2)
      { case 1 :
                // del_beg():
                break;
        case 2 :
                // del_end();
                break;
        case 3 :
                // del_item();
                break;
        }
        }while(ch2<4);
        break;
case 4:
        //SEARCHING CIRCULAR LINK LIST
      //search();
        break;
case 5:
        //REVERSING A LINK LIST
        //rev();
        break;
}
}while(ch<6);
getch();
}
void display()
{
 node *ptr;
 ptr= start;
 cout<<"\n START -> ";
 while(ptr->link!=start)
 {
 cout<<ptr->info<<"->";
 ptr=ptr->link;
 }
 cout<<ptr->info;
 getch();
 }

void ins_beg()
{
node *p;
p= new node;
if(p==NULL)
{
cout<<"OVERFLOW \n";
getch();
exit(0);
}
cout<<"ENTER DATA TO BE INSERTED ";
cin>>p->info;
if(start==NULL)
{
p->link=p;
start=p;
last=p;
}
else
{
p->link=start;
start=p;
last->link=p;
}
getch();
}

void ins_end()
{
node *p;
p=new node;
if(p==NULL)
{
cout<<"OVERFLOW\n";
getch();
exit(0);
}
cout<<"ENTER DATA TO BE INSERTED ";
cin>>p->info;
if(start==NULL)
{
p->link=p;
start=p;
last=p;
}
else
{
p->link=start;
last->link=p;
last=p;
}
getch();
}

void ins_pos()
{
int pos;
cout<<"ENTER POSITION TO BE INSERTED \n";
cin>>pos;
if(pos==1)
ins_beg();
else
{
node *ptr=start->link;
int i =2;
while(i<pos && ptr->link!=start)
{


xpi0t0s 16Oct2008 02:59

Re: circular link list
 
Er, yeah. It's code. What kind of help do you want with it? Is it doing something you don't expect, or not doing something you do?

It's also code without the [ code ] and [ /code ] tags, which is a lot harder to read than code correctlky formatted.

Just paging through, I noticed you have the interesting concept of deleting from the beginning or end of a circular list. Please enlighten us all as to where the beginning and end of a circle can be found.

shabbir 16Oct2008 08:43

Re: circular link list
 
The code is not even complete.

xpi0t0s 16Oct2008 11:53

Re: circular link list
 
OP, if you want to work out how to write the code to complete the program, don't try to do it in your head, do it first on paper. It's not so easy to do in ASCII art but here is the method to insert into a linked list:
Code:

+-+      +-+
|A|--p1-->|C|
+-+      +-+

We want to insert B between A and C.  First step, point B at C:

+-+      +-+
|A|--p1-->|C|
+-+      +-+
          ^
+-+        |
|B|--p2----+
+-+

I've numbered the pointers so you can see what's going on.  Code for this
would be something like:
B->next=C;

Next step: point A at B instead of C:

+-+      +-+      +-+
|A|--p1-->|B|--p2-->|C|
+-+      +-+      +-+

Code: A->next=B;


xpi0t0s 16Oct2008 12:04

Re: circular link list
 
Similarly insertion into a double linked list goes along the lines:
Code:

+-+--nA->+-+
|A|      |C|
+-+<-pC--+-+

Link B in:

+-+---nA--->+-+
|A|        |C|
+-+<--pC----+-+
 ^          ^
 |          |
 |    +-+-nB-+
 |    |B|
 +-pB-+-+

Now redirect the links between A and C:

+-+--nA->+-+--nB->+-+
|A|      |B|      |C|
+-+<-pB--+-+<-pC--+-+

I'll leave working out the code to you, which should be really easy with this in front of you. Also to figure out for both single and double linked lists are inserting at the head and at the end, and deleting (head,middle,end).


All times are GMT +5.5. The time now is 23:53.