C++ 28 36 53 78 行的代码为什么用结构体数据会错,不应该啊!

[复制链接]
查看11 | 回复1 | 2009-4-19 10:13:43 | 显示全部楼层 |阅读模式
#include
using namespace std;
typedef int elemtype;
struct lnode
{
lnode *next;
typedef data;
};
void initlist(lnode *& hl)
{
hl=new lnode;
hl->next=NULL;
}
elemtype getlist(lnode *& hl,int pos)
{
int i=0;
hl=hl->next;
while(hl!=NULL)
{
i++;
if(i==pos)break;
hl=hl->next;
}
return hl->data;
}
void traverlist(lnode *hl)
{
hl=hl->next;
while(hl!=NULL)
{
coutdatanext;
}
coutnext;

j++;
}
if(p==NULL)return 0;
lnode *newptr=new lnode;
newptr->data=item;
newptr->next=p->next;
p->next=newptr;
return 1;
}
/*bool DeleteList(lnode*&hl,intpos,ElemType &item)
{

lnode *p=GetList(hl,pos-1);


if( p==NULL ) return0;

lnode *q=p->next;

item=q->data;

p->next=q->next;

deleteq;

return1;
}*/
voidCreateList(lnode * &hl,int n)
{

lnode *ptr;

hl=new lnode ;

hl->next=NULL;

for(inti=n;i>0;i--)
{

ptr=new lnode ;

cin>>ptr->data;

ptr->next=hl->next; hl->next=ptr;

}
}
void main()
{
int a[10];
lnode *s;
initlist(s);
for(int i=0;i>a;
insertlist(s,1,a);
}
traverlist(s);
}

回复

使用道具 举报

千问 | 2009-4-19 10:13:43 | 显示全部楼层
1. 把所有的lnode * &hl参数修改了。若要传递结构体的指针就声明为lnode* hl,若要传递结构体的引用则声明为lnode& hl。注意,在声明为结构体的引用时要将"->"转为"."。 2.在结构体的声明中第二个成员应该声明为elemtype data。3.你这个程序在运行的时候也会出现问题,主要是把指针中的内容丢失了,应该把insertlist和initlist这两个函数的第一个参数改为lnode** hl。...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行