josephus problem
Mar 2, 2013 at 3:12am UTC
here is my code,the main problem is that there is no problem in the compiler but when you input the first number, you can't put another number.
I am very curious about where did I write wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <iostream>
#include <cstdlib>
using namespace std;
template <typename T>
class linklist
{
public :
struct node
{
T data;
struct node*pointer,*next,*t;
};
node*creatnode();
T surviver(int T)const ;
};
void f(int )
{}
template <typename T>
typename linklist<T>::node*linklist<T>::creatnode()
{
node*pointer=(struct node*)malloc(sizeof (pointer));//initiate
cout<<"please input a number,and end with a number 0" <<endl;
T key;
cin>>key;
while (key!=0)
pointer->data=key;
pointer->next=creatnode();
return pointer;
};
template <typename T>
T linklist<T>::surviver(int N)const //find the last node
{
node*t=new node;
int i;
while (t!=t->next)
{
for (i=1;i<N;i++)
t=t->next;
}
cout<<t->data<<endl;
}
int main()
{
linklist<int >josephuslist;
josephuslist.creatnode();
josephuslist.surviver(5);
system("pause" );
return 0;
}
Topic archived. No new replies allowed.