new操作的总是,高手指教一下;

pusshi 2008-11-03 01:09:01
/*输入n个小孩数,从s个小孩数起,数到m个,去掉一个小孩直剩下最后一个.*/

#include <iostream>


using namespace std;

struct Node
{
int code;
Node *next;
};

int n;
int m;
int s;
Node *pcur;
Node *pivot;


int main()
{



bool getvalue();//判断数据正确性;
Node *creatnode();//创建一个链表
void countnode(int m);
void process();

if ( !getvalue() )
{
cout<<"你输入的数字不合法: ";
}
else
{
Node *pa=creatnode();
process();
delete []pa;
}
return 0;
}


bool getvalue()
{
cout<<"请输入小孩数: ";
cin>>n;

cout<<"请输入开始数: ";
cin>>s;

cout<<"请输入结束数: ";
cin>>m;

if ( (n >=2) && (m >=1 && m<n) && (s >=1 && s<n) )
{
return true;
}
else
{
return false;
}
}

void countnode(int m)
{
for (int i=1; i<m; i++)
{
pivot=pcur;
pcur=pivot->next;

}
}

Node *creatnode()
{
Node *px= new Node[n];

for (int i=1; i<=n; i++)
{
px[i-1].next = &px[i%n];
px[i-1].code = i;
cout<<px[i-1].code<<endl;
}

pivot->next = &px[n-1];//有可以是这两行不对,请说明一下,因为...所以...,^-^;
pcur->next = &px[n-1];

countnode(s-1);
return px;
}



void process()
{
for (int i=1; i<n; i++)
{
countnode(m);
static line=0;
cout<<pcur->code;
if ( (line++)%10 )
{
cout<<"\n";
}
pivot->next=pcur->next;
pivot=pcur;
}
}


以上编译没错,但一运行就出错,想了半小时,可以是creatnode()这个函数出错,高手看下,谢谢
...全文
127 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pusshi 2008-11-03
  • 打赏
  • 举报
回复
还是要靠自己,根本和那个
Node *pcur=new Node;
Node /*输入n个小孩数,从s个小孩数起,数到m个,去掉一个小孩直剩下最后一个.*/

#include <iostream>


using namespace std;

struct Node
{
int code;
Node *next;
};

int n;
int m;
int s;
Node *pcur;
Node *pivot;


int main()
{



bool getvalue();//判断数据正确性;
Node *creatnode();//创建一个链表
void countnode(int m);
void process();

if ( !getvalue() )
{
cout<<"你输入的数字不合法: ";
}
else
{
Node *pa=creatnode();


process();
delete []pa;

}
return 0;
}




bool getvalue()
{
cout<<"请输入小孩数: ";
cin>>n;

cout<<"请输入开始数: ";
cin>>s;

cout<<"请输入结束数: ";
cin>>m;

if ( (n >=2) && (m >=1 && m<n) && (s >=1 && s<n) )
{
return true;
}
else
{
return false;
}
}

void countnode(int m)
{
for (int i=1; i<=m; i++)
{
pivot=pcur;
pcur=pivot->next;

}
}

Node *creatnode()
{
Node *px= new Node[n];

for (int i=1; i<=n; i++)
{
px[i-1].next = &px[i%n];
px[i-1].code = i;
cout<<px[i-1].code<<endl;
}

pivot = &px[n-1];//注错误原来在这地方; 原为pivot->next= &px[n-1]使后面指针操作无效.....
pcur= &px[n-1];

countnode(s-1);
return px;
}



void process()
{
cout<<"小孩出序依次为: ";
for (int i=1; i<n; i++)
{

countnode(m);

cout<<pcur->code;

pivot->next=pcur->next;
pivot=pcur;
}
cout<<endl;
}
*pivot=new Node;无关........研究2小时终于找到问题在哪了.......以下是正确代码.......
pusshi 2008-11-03
  • 打赏
  • 举报
回复
我不是找方法,我是找为什么这程序运行不正确,...要方法,我用类实现更简单.....
pusshi 2008-11-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 elegant87 的回复:]
C/C++ code
/*输入n个小孩数,从s个小孩数起,数到m个,去掉一个小孩直剩下最后一个.*/

#include <iostream>


using namespace std;

struct Node
{
int code;
Node *next;
};

int n;
int m;
int s;
Node *pcur=new Node;
Node *pivot=new Node;


int main()
{
bool getvalue();//判断数据正确性;
Node *creatnode();//创建一个链表
void countnode(int m);…
[/Quote]
你运行了没,我早试过了,还是出错.......
elegant87 2008-11-03
  • 打赏
  • 举报
回复

/*输入n个小孩数,从s个小孩数起,数到m个,去掉一个小孩直剩下最后一个.*/

#include <iostream>


using namespace std;

struct Node
{
int code;
Node *next;
};

int n;
int m;
int s;
Node *pcur=new Node;
Node *pivot=new Node;


int main()
{
bool getvalue();//判断数据正确性;
Node *creatnode();//创建一个链表
void countnode(int m);
void process();
if ( !getvalue() )
{
cout <<"你输入的数字不合法: ";
}
else
{
Node *pa=creatnode();
process();
delete []pa;
}
return 0;
}


bool getvalue()
{
cout <<"请输入小孩数: ";
cin>>n;

cout <<"请输入开始数: ";
cin>>s;

cout <<"请输入结束数: ";
cin>>m;

if ( (n >=2) && (m >=1 && m <n) && (s >=1 && s <n) )
{
return true;
}
else
{
return false;
}
}

void countnode(int m)
{
for (int i=1; i <m; i++)
{
pivot=pcur;
pcur=pivot->next;

}
}

Node *creatnode()
{
Node *px= new Node[n];
for (int i=1; i <=n; i++)
{
px[i-1].next = &px[i%n];
px[i-1].code = i;
cout <<px[i-1].code <<endl;
}
pivot->next= &px[n-1];
pcur->next= &px[n-1];
countnode(s-1);
return px;
}


void process()
{
for (int i=1; i <n; i++)
{
countnode(m);
static line=0;
cout <<pcur->code;
if ( (line++)%10 )
{
cout <<"\n";
}
pivot->next=pcur->next;
pivot=pcur;
}
}

elegant87 2008-11-03
  • 打赏
  • 举报
回复
你没有分配啊!

Node *pcur;
Node *pivot;
改为:
Node *pcur=new Node;
Node *pivot=new Node;
xiaoru806 2008-11-03
  • 打赏
  • 举报
回复
不知道你的这个是不是循环数数,如果是的话,那这个问题和约瑟夫环问题一个道理,只是你的第一次从第s个数起,下面是一种约瑟夫环的解决方案,你可以参考。
#include<stdio.h>
#include<malloc.h>
Int main()
{
Int *a;
Int n,m,I,j,k=0;
Scanf(“%d%d”,&n,&m);
a=(int *)malloc(sizeof(int)*n);
for(i=1;i<n+1;i++)
a[i-1]=I;
i=1;
while(n)
{
K=k>0?m%n+k-1:m%n;
K=k%n;
Printf(“%d->%d\n”,i++,k>0?a[k-1]:a[n-1]);
If(k!=0)
For(j=k;j<n;j++)
{
A[j-1]=a[j];
}
n--;
}
Free(a);
Return 0;
}
pusshi 2008-11-03
  • 打赏
  • 举报
回复
加了,还不正确.只多输了几行,还是出错.应该怎么加;
killbug2004 2008-11-03
  • 打赏
  • 举报
回复
pivot指针没有初始化,先new个

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧