|
1鱼币
本帖最后由 乘风 于 2012-8-16 20:37 编辑
看得书是《数据结构(c语言版)》,中国水利水电出版社 ,赵坚。
这本书真是恶心,所有的算法都只给出分C语言的描述,从来不给个完整源码验证一下。
而且给出的部分也是各种垃圾(也许是我自己太垃圾)。
书中定义结构体:
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*Linklist;
LNode *head;
给出的初始化链表的代码:
void initlist(LinkList *L)
{
*L=(LNode *)malloc(sizeof(LNode));
(*L)->next=NULL;
}
下面是我自己的程序:
- #include "stdafx.h"
- #include <stdio.h>
- #include <MALLOC.H>
- typedef struct LNode{
- int data;
- LNode *next;
- }LNode, *Linklist;
- Linklist *a1;
- void CreatLinklist(Linklist *L, int n)
- {
- LNode *p;
- (*L)=(LNode *)malloc(sizeof(LNode));
- (*L)->next=NULL; //链表初始化
- p=(*L);
- int num;
- int pos=0;
- LNode *s;
- printf("输入一组数,大余10个,以0作为结束标志:\n");
- //scanf("%d", &num);
- while (num!=0&&pos<n)
- {
- scanf("%d", &num);
- pos++;
- s=(LNode *)malloc(sizeof(LNode));
- s->data=num;
- s->next=p->next;
- p->next=s;
- }
- }
- void DisplayLinklist(Linklist *L)
- {
- LNode *p;
- p=(*L)->next;
- int num=0;
- while (p!=NULL)
- {
- printf("%d ",p->data);
- p=p->next;
- }
- }
- int main(int argc, char* argv[])
- {
- //Linklist a1;
- int n;
- printf("请输入链表元素的个数\n");
- scanf("%d", &n);
- CreatLinklist(a1,n);
- // DisplayLinklist(a1);
- return 0;
- }
复制代码 我调试过了,程序执行到CreatLinklist()函数中的(*L)=(LNode *)malloc(sizeof(LNode));这一句出错。但是这和书中给出的参考代码方式完全一样啊。为什么就不行呢。而且,我自己分析也觉得这种方式应该不会出错啊。求指点,我真是郁闷很久了。。大家只帮我看这一处就可以的,别的地方就不麻烦了。谢谢
|
|