更新时间2018-04-23 17:00:15
下面代码无法运行,请问是哪里出错?
头插法和尾插法的输出方法是否有问题?
报错:
6.c(51) : warning C4047: 'function' : 'struct node *' differs in levels of indirection from 'struct node ** '
6.c(51) : warning C4024: 'CreLinklistHead' : different types for formal and actual parameter 1
6.c(52) : warning C4047: 'function' : 'struct node *' differs in levels of indirection from 'struct node ** '
6.c(52) : warning C4024: 'CreLinklistTail' : different types for formal and actual parameter 1
6.c(39) : warning C4700: local variable 'head' used without having been initialized
#include "stdio.h"
#include "malloc.h"
typedef int ElemType;
typedef struct node{
ElemType data;
struct node *next;
}LNode,*Linklist;
void CreLinklistTail(Linklist L,int n)//尾插法
{
LNode *s,*tail,*head;//s-工作指针,动态的。tail-尾指针。head-头指针。
ElemType x;
int i;
tail=L;
for(i=n;i>0;i--)
{
scanf("%d",&x);
s=(LNode *)malloc(sizeof(LNode));
s->data=x;
tail->next=s;
tail=s;
}
head=L->next;
for(i=n;i>0;i--)
{
printf("%d ",head->data);
head=head->next;
}
}
void CreLinklistHead(Linklist L,int n)//头插法
{
LNode *s,*head;//s-工作指针,动态的。head-头指针。
ElemType x;
int i;
for(i=n;i>0;i--)
{
scanf("%d",&x);
s=(LNode *)malloc(sizeof(LNode));
s->data=x;
s->next=head->next;
head->next=s;
}
for(i=n;i>0;i--)
{
printf("%d ",head->data);
head=head->next;
}
}
void main()
{
Linklist L;
CreLinklistHead(&L,3);
CreLinklistTail(&L,3);
}
形参和实参类型不匹配,main函数里L定义的是指针,再取地址就变成指针的地址了,应该把L定义成LNode
CreLinklistHead函数里head是一个指针,没有分配内存空间就调用了head->next=s;
下一篇:电脑蓝屏。。。。