circular link list

Discussion in 'C++' started by vg_2007, Oct 15, 2008.

  1. vg_2007

    vg_2007 New Member

    Joined:
    Apr 15, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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)
    {
     
    Last edited by a moderator: Oct 16, 2008
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The code is not even complete.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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;
    
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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).
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice