|
10鱼币
// 很简单的一个C语言链表,就是创建一个数目为NUM 的链表, 然后打印出来,最后清空 ,程序就完了
// 只是运行时出了问题,显示一个8XXXXXX 的数据,程序返回一个255 , 我是在codeblock , 谁帮我找一下问题出在哪了。。一点金币1414
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct list)
struct list{
int value ;
struct list *next ;
};
typedef struct list list , *plist ;
plist creatlist( int num )
{
plist head , p , q ;
int count = 0 ;
p = (plist) malloc(LEN) ;
head = p ;
while( p != NULL && count < num )
{
++count ;
printf("input %d point list p->value " , count ) ;
scanf("%d",&p->value ) ;
q = (plist) malloc( LEN ) ;
p->next = q ;
p = q ;
}
p->next = NULL;
if( !head )
{
printf("sorry , meay be momery wrong") ;
return NULL ;
}
else
return head ;
}
void showlist( plist head )
{
plist p = head ;
while( p != NULL )
{
printf("%d\n",p->value) ;
p = p->next ;
}
}
void clearlist( plist head )
{
plist q , p = head ;
while(p != NULL )
{
q = p->next ;
free(p) ;
p = q ;
}
p->next = NULL ;
}
int main( void )
{
plist head ;
int num ;
printf("please input the number num you creatlist") ;
scanf("%d",&num) ;
head = creatlist( num ) ;
showlist(head) ;
clearlist(head) ;
return 0 ;
}
|
最佳答案
查看完整内容
creatlist函数创建链表的时候多创建了一个节点,可是你没有把这个多余的节点value赋值,所以最后会输出你说的那个错误。
|