# 急盼答案

michelle220 2008-03-18 10:07:20

#ifndef CNODE_H
#define CNODE_H

class cnode{
public:
int lab;
cnode *next;
}

#endif

#include <iostream.h>
#include "cnode.h"
#include <stdlib.h>

int main()
{

int n;
int m = 3;
cnode *p,*q,*clist;

cin>>n;
//setup n cnode
q = new cnode;
clist = q;

for(int i = 1;i <= n;i++)
{
p = new cnode;
p->lab = i;
p->next = NULL;
q->next = p;
q = p;

if (i == n) q->next = clist->next;
}

//output the label

p = clist;
for (int K = 1; K <= n;K++)
{
for(int j = 1;j < m;j++)
p = p->next;//delete q

if(K == n)
{
cout<<q->lab<<endl;
break;
}

cout<< q->lab<< " ";
p->next = q->next;
delete q;

}

return 0;

}
...全文
95 7 打赏 收藏 举报

7 条回复

guzhilei1986 2008-03-19
• 打赏
• 举报

idot 2008-03-19
• 打赏
• 举报

#include <iostream>
#include "cnode.h"
#include "stdlib.h"

using namespace std;

int main()
{

int n;
int m = 3;
cnode *p,*q,*clist;

cin>> n;
//setup n cnode
q = new cnode;
clist = q;

for(int i = 0;i < n;i++)
{
p = new cnode;
p-> lab = i+1;
p-> next = NULL;
q-> next = p;
q = p;

}
//output the label

p = clist->next;
for (int K = 1; K <= n;K++)
{

cout<< p-> lab<< endl;

q = p;
p = p-> next;
delete q;

}

delete clist;

system("pause");
return 0;
}
daiguoliangfirst 2008-03-19
• 打赏
• 举报

clist的赋值有错误
lunhuifengzheng 2008-03-18
• 打赏
• 举报

````
michelle220 2008-03-18
• 打赏
• 举报

• 打赏
• 举报

。。。。
w66187564 2008-03-18
• 打赏
• 举报

class cnode{
public:
int lab;
cnode *next;
}; //这的分号漏了

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-03-18 10:07

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下