VC++6.0中的问题

来源:百度知道 编辑:UC知道 时间:2024/05/25 11:57:18
代码如下:
// 设计.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;

struct data
{
double xishu;
int zhishu;
};
typedef struct node
{
data shuju;
node *next;
}node;
class linklist
{
node *H;
int len;
public:
linklist()
{
H=new node;
H->next=0;
}
void push_head(data e)
{
node *p=H;

while(p->next!=0)
{
p=p->next;
}
node *s=new node;
s->shuju.xishu=e.xishu;
s->shuju.zhishu=e.zhishu;

s->next=0;
p->next=s;
len++;
}
int length()
{
return len;
}
data get_at(int i)
{

int counter=0;
node *p=H;
while(counter!=i)
{
p=p->next;
counter++;
}
d

呵呵,你这个程序里边的错误比较多。
第一:在list类的构造函数里边你应该初始化len
第二:list的get_at()方法返回的是get_at中定义的局部变量,get_at返回时该变量所占内存已经被释放掉了。因此你加入到Lc中的元素毫无意义。
第三:get_at()传入的参数和里边的计数方法有问题,因为你是带头节点的链表。
不好意思,没时间把你的程序详细改了,你自己再检查 看看吧!

一楼说完了,我走了

语法和算法都有问题

1