Node::Node( )
{
next = NULL;
}
Node::Node(Node_entry item, Node *add_on)
{
entry = item;
next = add_on;
}
--------------
#include<iostream.h> //文件2
typedef int Node_entry;
struct Node {
// data members
Node_entry entry;
Node *next;
// constructors
Node( );
Node(Node_entry item, Node *add_on = NULL);
};
---------------------
#include"MyNode.h" //主程序
#include<iostream.h>
void main(){
int n,m;
Node *p=NULL, *p_head=NULL ,*p_tmp=NULL;
cout<<"This is a YSF(n,m) problem."<<endl;
//n people, m count, both n and m should >= 1
do{
cout<<"Please input the n(n>1).";
cin>>n;
}while(n<1);
do{
cout<<"Please input the m(m>1).";
cin>>m;
}while(m<1);
//construct the circular link structure
for(int i=n;i>=1;i--){
p_head = new Node(i,p);
p=p_head;
}
//get to the tail, which is n.
while(p->next)p=p->next;
//connect to a circular
p->next=p_head;
//go with the circular
for(i=1;i<n;i++){ //do n-1 times to get rid of n-1 elements.
for(int k=1;k<m;k++)p=p->next;
p_tmp=p->next;
p->next=p_tmp->next;
cout<<endl<<"Get rid of:"<<p_tmp->entry;
delete p_tmp;
}
//Print the left element
cout<<endl<<"The left element is:"<<p->entry<<endl;
}
我這個學期也搞數據結構
程序可以運行的