Hi,
When I ran your code I didn't get an infinite loop and it appeared to
terminate without any problems, but after a little work I did find a few
potential problems. Some are to do with memory allocation and deallocation,
which you still haven't mastered. However the mistake that could be the
cause of your problem is that you aren't using counter. When you count off
until your next victim in the for loop you are looping to 'number' when it
should be 'counter'.
As is my habit I have rewritten what you gave me and come up with something
that I believe is neater (attached). Pay particular attention to the
handling of the pointers, and note also that I don't use any references or
redundant pointers (i.e. one and two) cos they aren't needed. You may want
them though, so it's up to you.
Hope this helps, Jason.
----- Original Message -----
From: "chromatonus 712" <chr...@ho...>
To: <dev...@li...>
Sent: Saturday, March 09, 2002 1:47 AM
Subject: [Dev-C++] circular linked list
> Hi, this is kind of like a game where everyone sits in a circle and
starting
> from one person, count off by 5's for example, eliminating every 5th
person
> until there is one person left.
> there are 2 inputs for this program, one the # of elements in the circular
> list, two the # to count off by.
> My program runs in an infinite loop and I don't know why... Thanks for
your
> help!
>
> #include <iostream.h>
> #include <stdlib.h>
> #include <conio.h>
>
>
> struct node
> {
> char data;
> node *next;
> };
>
> //PROTOTYPES
>
> void add(node *&, char);
>
> node delete_from(node *&, node *, node *);
>
> //MAIN PROGRAM
>
> int main()
> {
> int i, number, counter;
> node *ring = new node;
> node *back = new node;
> node *track = new node;
> cout<< "# of victims in the circle = ";
> cin>> number;
> cout<< "count by how many? ";
> cin>> counter;
> ring -> next = ring;
> ring -> data = 'a';
> for(i=1; i<number; i++)
> add(ring, char(97+i));
> back = ring;
> for(i=1; i<=number; i++)
> back = back -> next;
> if(back -> next == ring)
> cout<< " yers";
> cout<< ring->data;
> while(ring -> next != ring)
> {
> for(i=1; i<=number; i++)
> back = back -> next;
> track = back -> next;
> delete_from(ring, back, track);
> ring = back;
> system("PAUSE");
> }
> cout<< "Survivor is " << ring -> data << "\n";
> system("PAUSE");
> }
>
> void add(node *&list, char info)
> {
> node *addon = new node;
> addon -> data = info;
> addon -> next = list -> next;
> list -> next = addon;
> }
>
> node delete_from(node *&list, node *one, node *two)
> {
> cout<< two -> data << " ";
> one -> next = two -> next;
> return *two;
> }
>
>
>
> _________________________________________________________________
> Send and receive Hotmail on your mobile device: http://mobile.msn.com
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|