进来看看这个简单的程序,该怎么解决
进来看看这个简单的程序
想实现数据结构中的一个程序
程序结果是返回链表长度
#include <iostream.h>
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 100
struct LNode
{
char date;
LNode *next;
};
int ListLength(LNode *L);
int main()
{
int Length;
char string[] = { "The ListLength Programme "};
LNode *LinkList;
LinkList = (LNode *) malloc (MAXSIZE*sizeof(LNode));
if(!LinkList)
{
cout < < "malloc space error! " < <endl;
return 0;
}
Length = ListLength(LinkList);
cout < < "The String Length is " < <Length < <endl;
return 0;
}
int ListLength(LNode *L)
{
int Length;
LNode *p;
p = L;
while(p)
{
Length++;
p = p-> next;
}
return Length;
}
编译连接都没问题 运行时内存错误
请各位帮帮看原因在哪
------解决方案--------------------
LinkList = (LNode *) malloc (MAXSIZE*sizeof(LNode));
......
Length = ListLength(LinkList); //先对 LinkList 进行适当的赋值
cout < < "The String Length is " < <Length < <endl;
return 0;
}
------解决方案--------------------
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 100
struct LNode
{
char date;
LNode *next;
};
int ListLength(LNode *L)
{
LNode *p;
int Length=0; //这里要赋初值为0,才能计数
p = L;
while(p)
{
Length++;
p = p-> next;
}
return Length;
}
int main()
{
int Length;
char string[] = { "The ListLength Programme "};
LNode *LinkList;
LinkList = (LNode *) malloc (sizeof(LNode));//作为表头结点
if(!LinkList)
{
cout < < "malloc space error! " < <endl;
return 0;
}
LinkList-> next=NULL; //如果这里不指向空,
//ListLength函数中的p=p-> next出问题
Length = ListLength(LinkList);
cout < < "The String Length is " < <Length < <endl;
return 0;
}
想实现数据结构中的一个程序
程序结果是返回链表长度
#include <iostream.h>
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 100
struct LNode
{
char date;
LNode *next;
};
int ListLength(LNode *L);
int main()
{
int Length;
char string[] = { "The ListLength Programme "};
LNode *LinkList;
LinkList = (LNode *) malloc (MAXSIZE*sizeof(LNode));
if(!LinkList)
{
cout < < "malloc space error! " < <endl;
return 0;
}
Length = ListLength(LinkList);
cout < < "The String Length is " < <Length < <endl;
return 0;
}
int ListLength(LNode *L)
{
int Length;
LNode *p;
p = L;
while(p)
{
Length++;
p = p-> next;
}
return Length;
}
编译连接都没问题 运行时内存错误
请各位帮帮看原因在哪
------解决方案--------------------
LinkList = (LNode *) malloc (MAXSIZE*sizeof(LNode));
......
Length = ListLength(LinkList); //先对 LinkList 进行适当的赋值
cout < < "The String Length is " < <Length < <endl;
return 0;
}
------解决方案--------------------
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 100
struct LNode
{
char date;
LNode *next;
};
int ListLength(LNode *L)
{
LNode *p;
int Length=0; //这里要赋初值为0,才能计数
p = L;
while(p)
{
Length++;
p = p-> next;
}
return Length;
}
int main()
{
int Length;
char string[] = { "The ListLength Programme "};
LNode *LinkList;
LinkList = (LNode *) malloc (sizeof(LNode));//作为表头结点
if(!LinkList)
{
cout < < "malloc space error! " < <endl;
return 0;
}
LinkList-> next=NULL; //如果这里不指向空,
//ListLength函数中的p=p-> next出问题
Length = ListLength(LinkList);
cout < < "The String Length is " < <Length < <endl;
return 0;
}