读书人

这个链表的代码到底错哪了啊请指点

发布时间: 2012-03-25 20:55:17 作者: rapoo

这个链表的代码到底哪里错了啊?请各位高手指点
#include "stdafx.h "
#include <iostream.h>
#include <stdlib.h>

typedef struct ElementT{
char data;
struct ElementT *next;
}LNode,*LinkList;

LinkList p,q;

void CreateNode(LinkList &l,int n)
{
l=(LinkList)malloc(sizeof(LNode));
l-> next=NULL;
for (int i=n; i> 0; --i)
{
p=(LinkList)malloc(sizeof(LNode));
scanf(&p-> data);
p-> next=l-> next;
l-> next=p;
}

}

void print(LinkList &l)
{
LinkList p;
p=l;
while(p-> next!=NULL)
{
cout < <p-> data < <endl;
p=p-> next;
}
}

int main(int argc, char* argv[])
{
printf( "Hello World!\n ");
LinkList head;
CreateNode(head,4);
print(head);
return 0;
}

[解决办法]
void CreateNode(LinkList &l,int n)
{
l=(LinkList)malloc(sizeof(LNode));
l-> next=NULL;
for (int i=n; i> 0; --i)
{
p=(LinkList)malloc(sizeof(LNode));
scanf( "%d ",&p-> data); //这里
p-> next=l-> next;
l-> next=p;
}

}

读书人网 >C++

热点推荐