- 论坛徽章:
- 0
|
俺是菜鸟,这是我的源码。。
.hpp
#ifndef _JUMPSEA_H_
#define _JUMPSEA_H_
struct node
{
int num;
node *next;
};
#endif
.cpp
#include "jumpsea.hpp"
#include <iostream.h>
int main()
{
int num=0;
node *pNode=NULL,*pHead=NULL;
cout<<"please input the total num(3-100),input \'0' for end:";
cin>>num;
if ((num>=3 && num<=100))
{
/******构建循环 链表********/
pHead=new node;
pHead->num=1;
pNode=new node;
pHead->next=pNode;
for (int i=2;i<num;i++)
{
pNode->num=i;
pNode->next=new node;
pNode=pNode->next;
}
pNode->num=num;
pNode->next=pHead;
/*******完成链表构建**/
//输出构建好的链表
pNode=pHead->next;
cout<<pHead->num<<"->"<<pNode->num;
while(pNode->next != pHead)
{
pNode=pNode->next;
cout<<"->"<<pNode->num;
}
cout<<endl;
int count=2;
int total = num;
node *pf=NULL,*pn=NULL;
pf=pHead;
pn=pHead->next;
while(total>=2)
{
pHead=pn;
pNode=pHead->next;
cout<<pHead->num<<"->"<<pNode->num;
while(pNode->next != pHead)
{
pNode=pNode->next;
cout<<"->"<<pNode->num;
}
cout<<endl;
if(count==3)
{
pf->next=pn->next;
delete pn;
pn=pf->next;
total--;
count=1;
}
else
{
pf=pf->next;
pn=pn->next;
count++;
}
}
cout<<pn->num<<endl;
delete pn;
}
else
{
cout<<"error"<<endl;
}
return 0;
} |
|