1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Circular Linked List

Discussion in 'C' started by shabbir, Aug 27, 2006.

  1. harris

    harris New Member

    Joined:
    Mar 3, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Well, i need a similar sample for my assignement (not really mine but have to do it) but what i found in the internet, most of them are not working with the blood dev c so it's really difficult for me to work it out.. think ya can help me out??
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    I am not sure which while loop or function you are referring to but all of them as of the following format

    Code:
    while(current->next!=start)
    which cannot be a infinite one.

    Also I ran the program as well and it works perfectly fine on VC++ 6 Environment.
     
  3. harris

    harris New Member

    Joined:
    Mar 3, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    It's
    Code:
     While (ch=='Y' || ch=='y'); 
    When i choose 'y', there forever looping.. Well, I tried it in blood dev c environment...
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Choose something else and it would break :D
     
  5. harris

    harris New Member

    Joined:
    Mar 3, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    I know about that, i didn't choose anything else.. I chose 'y' and it's not working.. My software is blood dev c so it's really difficult for me to work it out from the sample as it's not working when i tried it.. And ya can call this stuck.. Well, btw, if ya got free time or ya're bored, ya can try that assignment, think it as a lil bit of practise or whatever.. :nice:
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    The try Flushing the variable before the input
     
  7. pandaren23

    pandaren23 New Member

    Joined:
    Mar 5, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    @harris
    The program is perfect. Maybe you might just have placed a character value on the roll number or at the mark then it would of course result into an infinite loop.

    by the way.. the program was good.. ^_^ it would help me a lot in my machine problem.. thanks for the upload. :p
     
  8. harris

    harris New Member

    Joined:
    Mar 3, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Very good, it works!! Thank ya m8.. :happy:
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    The pleasure is all mine.
     
  10. harris

    harris New Member

    Joined:
    Mar 3, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Btw, can ya give me some pointer?? i wanna to limit the queue for just 5.. Yours one seems like no limit there..
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Change the loop condition of 'y' for a variable looping 5 times.
     
  12. harris

    harris New Member

    Joined:
    Mar 3, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Code:
     
    /* Circular Queues */
    #include<iostream.h>
    #include<conio.h>
    #include <stdlib.h>
    #include <string.h>
    const int MAX = 5;
    class cqueue
    {
    int a[MAX],front,rear;
    public :
    cqueue()
    {
    front=rear=-1;
    }
    void insert(char);
    int deletion();
    void display();
    };
    void cqueue :: insert(char TaxLic)
    {
    if((front==0 && rear==MAX-1) || (rear+1==front))
    cout<<" Circular Queue is Full";
    else
    {
    if(rear==MAX-1)
    rear=0;
    else
    rear++;
    a[rear]=TaxLic;
    }
    if(front==-1)
    front=0;
    }
    int cqueue :: deletion()
    {
    int k;
    if(front==-1)
    cout<<"Circular Queue is Empty";
    else
    {
    k=a[front];
    if(front==rear)
    front=rear=-1;
    else
    {
    if(front==MAX-1)
    front=0;
    else
    front++;
    }
    }
    return k;
    }
    void cqueue :: display()
    {
    int i;
    if(front==-1)
    cout<<"Circular Queue is Empty";
    else
    {
    if(rear < front)
    {
    for(i=front;i<=MAX-1;i++)
    cout<<a[i]<<" ";
    for(i=0;i<=rear;i++)
    cout<<a[i]<<" ";
    }
    else
    {
    for(i=front;i<=rear;i++)
    cout<<a[i]<<" ";
    cout<<endl;
    }
    }
    }
    int main()
    {
    cqueue c1;
    char TaxLic [7];
    char Dname [20];
    char TaxTyp [15];
    int ch,val;
    char op;
    do
    {
    //clrscr();
    system("cls");
    cout<<"-----------Menu-------------\n";
    cout<<"1.Insertion\n";
    cout<<"2.Deletion\n";
    cout<<"3.Display\n";
    cout<<"4.Exit\n";
    cout<<"Enter Your Choice <1..4> ?";
    cin>>ch;
    switch(ch)
    {
    case 1 : cout<<"Enter Taxi License Number: ";
    fflush(stdin); 
    gets(TaxLic);
    //cin>>val;
     
    c1.insert(TaxLic);
    break;
    case 2 : val=c1.deletion();
    cout<<"Deleted Element :"<<val<<endl;
    break;
    case 3 : c1.display();
    break;
    }
    cout<<"Do you want to continue<Y/N> ?";
    fflush(stdin);
    cin>>op;
    }while(op=='Y' || op=='y');
    getch();
    }
     
    
    Help me out, i'm stuck.. Well, it's running ok but when i change the input from int type to char, then it shows error..
     
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Again the same question. What kind of error. Also if you have some problem with your code try creating a new thread rather than into this thread.

    Also intend your code so that its easy to understand
     
  14. pandaren23

    pandaren23 New Member

    Joined:
    Mar 5, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
                    /*COE116L - C1
              Machine Problem: Implementation of a
                circular linked list!*/
    
    struct Prof
    {
        int node_no,nNo,ctr,m,p,nCount,ctr2;
        char name[20],*list[];
        Prof *next;
    };
    
    Prof trans;
    
    void Initialize(Prof *start)
    {
        cout<<"\nEnter node number: ";
        cin>>start->node_no;
        cout<<"\nEnter the name of the student: ";
        gets(start->name);
        start->next=start;
        trans.nCount++;
    }
    
    Prof* ins_bef(Prof *start)
    {
        Prof *newnode, *current;
        newnode = new Prof;
        current = start;
        cout<<"\nEnter the node number before which you want to insert a node: ";
        cin>>trans.nNo;
        Initialize(newnode);
        if(current->node_no == trans.nNo)
        {
          newnode->next = start;
          while(current->next != start)
            current = current->next;
          current->next = newnode;
          start = newnode;
          return(start);
        }
        while(current->next!=start)
        {
          if(current->next->node_no == trans.nNo)
          {
            newnode->next=current->next;
            current->next=newnode;
            return(start);
          }
          current = current->next;
        }
        clrscr();
        cout<<"\trans.nNo match found!\n";
        return(start);
    }
    
    void ins_aft(Prof *start)
    {
        int  x = 0;
        Prof *newnode, *current;
        newnode = new Prof;
        cout<<"\nEnter the node number after which you want to insert a node: ";
        cin>>trans.nNo;
        Initialize(newnode);
        current = start;
        while(current->next != start)
        {
          if(current->node_no == trans.nNo)
          {
            newnode->next = current->next;
            current->next = newnode;
            x = 1;
          }
          current = current->next;
        }
        if(x == 0 && current->next == start && current->node_no == trans.nNo)
        {
          newnode->next = current->next;
          current->next = newnode;
          x = 1;
        }
        if(x == 0 && current->next == start)
        {
          clrscr();
          cout<<"\trans.nNo match found!\n";
        }
    }
    
    
    Prof* Delete(Prof *start)
    {
        Prof *delnode, *current;
        current = start;
        delnode = current;
        while(current->next != start)
          current = current->next;
        current->next = start->next;
        start = start->next;
        trans.list[trans.ctr] = delnode -> name;
        trans.ctr++;
        trans.ctr2 = trans.ctr;
        trans.nCount--;
        delete(delnode);
        return(start);
        
    }
    
    void Display(Prof *start)
    {
        Prof *current;
        int x = 0;
        current = start;
        while(current->next != start && x < 5)
        {
          cout<<"\nNode number "<<current->node_no<<": "<<current->name;
          current = current->next;
          x++;
        }
        cout<<"\nNode number "<<current->node_no<<": "<<current->name;
    }
    
    
    void Order()
    {
        int i = 0;
        if (trans.ctr2 == 0)
          cout<<"\nPlease start the count first!";
        else
        {
          cout<<"ORDER OF STUDENTS:\n";
          while (trans.ctr != 0 && i != trans.ctr2)
          {
            if (i != trans.ctr2-1)
              cout<<i+1<<".) "<<trans.list[i]<<endl;
            else
              cout<<endl<<trans.list[i]<<" is the last student!"<<endl;
            i++;
            trans.ctr--;
          }
          trans.ctr = trans.ctr2;
        }
    }
    
    void Simulation(Prof *start)
    {
        Prof *current;
        current = start;
        trans.p = 0;
        while(current->next != NULL && trans.nCount > 0)
        {
          if (trans.p == trans.m)
          {
            Delete(current);
            trans.p = 0;
          }
          current = current->next;
          trans.nNo = current->node_no;
          trans.p++;
        }
    }
    
    void main()
    {
        Prof *head;
        char ch;
        int opt, x = 0;
        trans.nCount = 0;
        clrscr();
        head = new Prof;
        head->next = NULL;
        mama:
        cout<<"\nChoose from the menu: \n"
            <<"\n1. Initialize the node\n"
            <<"\n2. Insert before a specified node\n"
            <<"\n3. Insert after a specified node\n"
            <<"\n4. Delete a particular node\n"
            <<"\n5. Order of Students\n"<<"\n6. Start the count\n"
            <<"\n7. Display\n"<<"\n8. Exit\n"<<"\t>> ";
        cin>>opt;
        if(x == 0 && opt != 1)
        {
          if (opt == 8)
          exit(0);
          else if(opt > 8)
          cout<<"\nEntered value is not in the choices!";
          else
          cout<<"\nYou must initialize at least one node!\n";
          goto mama;
        }
        if(x == 1 && opt == 1)
        {
          cout<<"\nInitialization can occur only once.\n"
              <<"Now you can insert a node\n";
          goto mama;
        }
        if((opt == 4 || opt == 6) && head->next == head)
        {
          cout<<"\nYou catrans.nNot delete the last node!\n";
          goto mama;
        }
        if(x == 0 && opt == 1)
          x = 1;
        switch(opt)
        {
          case 1:  clrscr();
               cout<<"\nEnter a value for m: ";
               cin>>trans.m;
               Initialize(head);
               goto mama;
          case 2:  clrscr();
               head=ins_bef(head);
               goto mama;
          case 3:  clrscr();
               ins_aft(head);
               goto mama;
          case 4:  clrscr();
               head = Delete(head);
               goto mama;
          case 5:  clrscr();
               Order();
               goto mama;
          case 6:  clrscr();
               Simulation(head);
               goto mama;
          case 7:  clrscr();
               Display(head);
               goto mama;
          case 8:  exit(0);
          default: clrscr();
               cout<<"\nEntered value is not in the choices!";
               goto mama;
        }
    }
    sir shabbir can you help me out with this? it's really urgent i need to pass this on wednesday. i have a problem on displaying the last node..

    the problem is this:

    The students decided themselves to be arranged in a circle and excuse the Mth person around the circle - with the size of the circle being reduced by one each time a person is excused. The problem is to find out which person will be the last remaining, or more generally, to find the order in which the people are excused.

    here's an example:

    if the circle contains : JIM JANE JACK JUNE JASON JOE JEREMY JANELLE and M = 5,
    then the order will be : JASON JIM JOE JACK JEAN JANELLE JANE with JEREMY as the last person/node that will approach the professor.

    i hava a problem in displaying the last node, but the order is correct though.. choose the 6th menu in the program if you want to do the operations after choosing it and i want to display the last node it produces an infinite loop so i placed an iteration to stop the loop ,can you please help me out?
     
  15. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    You know where shabbir said post your own thread? That didn't apply only to harris. Want help? POST YOUR OWN THREAD.
     
  16. pandaren23

    pandaren23 New Member

    Joined:
    Mar 5, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    sorry, after i've posted it yesterday i thought about what shabbir said to harris and i wanted to delete what i've posted but there is no delete function anyway then i got disconnected while posting another thread yesterday.. my bad.. sorry :p
     
  17. jacob1kao

    jacob1kao New Member

    Joined:
    Jul 7, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    PETER_APIIT
    What is the source code for the "Link_List.h" in the statement of {#include "Link_List.h"}?
    What is the environment that your program run?
    I thank you very much!
    jacob1kao@gmail.com
     
  18. enerst

    enerst Banned

    Joined:
    May 18, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    To All Great And Sincere Hackers

    Hello all I am Enerst, I am new hear my email id is (jaccy_luv@yahoo.com) please add me to you yahoo list there are tools i want to be buying from to time for working, after you have added me i shall speak with you for more enligtnement of what i am saying please add me all, it really hurt when people calls them self hackers and rip you off, please *** me all, i believe no matter how ignorant i may be you can also learn from me.

    Regards to you all hackers expecting to chat with you via yahoo.

    thanks King Enerst
     

Share This Page