|
From: Brian M. <str...@ho...> - 2002-03-10 20:42:28
|
The code works fine for me, but i would recommend three things:
1) change delete_from to a void function
2) at the end of delete_from, include 'delete two'
3) get rid of all those silly pauses
>From: "chromatonus 712" <chr...@ho...>
>To: dev...@li...
>Subject: [Dev-C++] circular linked list
>Date: Sat, 09 Mar 2002 01:47:47 +0000
>
>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;
are these next four lines of code really necessary?
> 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
_________________________________________________________________
Join the worlds largest e-mail service with MSN Hotmail.
http://www.hotmail.com
|