|
From: chromatonus 7. <chr...@ho...> - 2002-03-09 01:48:03
|
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
|