
C语言链表的问题,高手来看看我怎么就错了!!!!!!
#include<stdio.h>#include<stdlib.h>structnode{inta;structnode*next;};ints=0;voidmain(...
#include<stdio.h>
#include<stdlib.h>
struct node
{
int a;
struct node *next;
};
int s=0;
void main()
{
struct node *creat();
int print();
struct node *head=NULL;
head=creat(head);
s=print(head);
printf("%d",s);
}
struct node *creat(struct node *head)
{
struct node *tail,*p;
head=tail=NULL;
p=(struct node *)malloc(sizeof(struct node));
printf("请任意输入一个数:");
scanf("%d",&p->a);
while(a!=0)
{
p->next=NULL;
if(head==NULL)
head=tail=p;
else
{
tail->next=p;
tail=p;
}
printf("请再输入一个数:");
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->a);
}
return head;
}
int print(struct node *head)
{
struct node *p;
p=head;
while(p!=NULL)
{
s=s+p->a;
p=p->next;
}
return s;
}
错误一:error C2660: 'creat' : function does not take 1 parameters
错误二:error C2660: 'print' : function does not take 1 parameters
错误三:error C2065: 'a' : undeclared identifier
望高手帮忙看一下哈!!!!!
程序的意思:建立一个单向链表,再计算该链表中每个结点的 a 的值 加起来的总和:s=? 展开
#include<stdlib.h>
struct node
{
int a;
struct node *next;
};
int s=0;
void main()
{
struct node *creat();
int print();
struct node *head=NULL;
head=creat(head);
s=print(head);
printf("%d",s);
}
struct node *creat(struct node *head)
{
struct node *tail,*p;
head=tail=NULL;
p=(struct node *)malloc(sizeof(struct node));
printf("请任意输入一个数:");
scanf("%d",&p->a);
while(a!=0)
{
p->next=NULL;
if(head==NULL)
head=tail=p;
else
{
tail->next=p;
tail=p;
}
printf("请再输入一个数:");
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->a);
}
return head;
}
int print(struct node *head)
{
struct node *p;
p=head;
while(p!=NULL)
{
s=s+p->a;
p=p->next;
}
return s;
}
错误一:error C2660: 'creat' : function does not take 1 parameters
错误二:error C2660: 'print' : function does not take 1 parameters
错误三:error C2065: 'a' : undeclared identifier
望高手帮忙看一下哈!!!!!
程序的意思:建立一个单向链表,再计算该链表中每个结点的 a 的值 加起来的总和:s=? 展开
3个回答
展开全部
见下面代码中的注释
#include<stdio.h>
#include<stdlib.h>
struct node
{
int a;
struct node *next;
};
int s=0;
void main()
{
struct node *creat(); //应该改成struct node *creat(struct node *head);
int print(); //应该改成int print(struct node *head);
struct node *head=NULL;
head=creat(head);
s=print(head);
printf("%d",s);
//这里最好加个删除链表的函数,否则内存泄漏
}
struct node *creat(struct node *head)
{
struct node *tail,*p;
head=tail=NULL;
p=(struct node *)malloc(sizeof(struct node));
printf("请任意输入一个数:");
scanf("%d",&p->a);
while(a!=0) //这里应该改为while(p->a!=0)
{
p->next=NULL;
if(head==NULL)
head=tail=p;
else
{
tail->next=p;
tail=p;
//这里应该加一句 p->next=NULL;否则print()会错。
}
printf("请再输入一个数:");
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->a);
}
return head;
}
int print(struct node *head)
{
struct node *p;
p=head;
while(p!=NULL)
{
s=s+p->a;
p=p->next;
}
return s;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int a;
struct node *next;
};
int s=0;
void main()
{
struct node *creat(); //应该改成struct node *creat(struct node *head);
int print(); //应该改成int print(struct node *head);
struct node *head=NULL;
head=creat(head);
s=print(head);
printf("%d",s);
//这里最好加个删除链表的函数,否则内存泄漏
}
struct node *creat(struct node *head)
{
struct node *tail,*p;
head=tail=NULL;
p=(struct node *)malloc(sizeof(struct node));
printf("请任意输入一个数:");
scanf("%d",&p->a);
while(a!=0) //这里应该改为while(p->a!=0)
{
p->next=NULL;
if(head==NULL)
head=tail=p;
else
{
tail->next=p;
tail=p;
//这里应该加一句 p->next=NULL;否则print()会错。
}
printf("请再输入一个数:");
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->a);
}
return head;
}
int print(struct node *head)
{
struct node *p;
p=head;
while(p!=NULL)
{
s=s+p->a;
p=p->next;
}
return s;
}
追问
//这里最好加个删除链表的函数,否则内存泄漏
为什么会泄漏呢??????????求解析!!!!
追答
程序中用malloc分配节点的空间,如果不用free来释放,系统不会回收再利用。这就叫内存泄漏。详细请看百度百科http://baike.baidu.com/view/1068433.htm
展开全部
struct node *creat();
int print();
struct node *creat(struct node *head)
int print(struct node *head)
函数声明和定义都要完全一致,你的声明没有参数变量
while(a!=0)
a变量没有定义, 你是要p->a呢还是a是另一个变量(这个需要在函数中声明)
int print();
struct node *creat(struct node *head)
int print(struct node *head)
函数声明和定义都要完全一致,你的声明没有参数变量
while(a!=0)
a变量没有定义, 你是要p->a呢还是a是另一个变量(这个需要在函数中声明)
追问
函数声明时,不需要有参数吧
追答
如果你把形参漏了,那声明和定义就不是同一函数了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
我给你一个参考代码,希望对你有所帮助
养成一个良好的编程习惯对你有用
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode;
typedef LNode *LinkList;
//****************************************************************************
LinkList CreateList()//头插入法 建立带头节点的链表
{
LinkList L;
LinkList P;
int x;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;//建立空链表
printf("请输入第一个元素并以0作为输入结束的标志\n");
scanf("%d",&x);
while(x!=0)//以输入0作为输入结束标志
{
P=(LinkList)malloc(sizeof(LNode));
P->data=x;
P->next=L->next;
L->next=P;
scanf("%d",&x);
}
return L;
}
void printsum(LinkList L)//求链表各元素的和
{
LinkList p;
int sum=0;
p=L->next;
while(p){sum=sum+p->data;p=p->next;}
printf("%d\n",sum);
}
int main()
{
LinkList a,b,c,d,f;
ElemType e;
a=CreateList();
printsum(a);
return 0;
}
我给你一个参考代码,希望对你有所帮助
养成一个良好的编程习惯对你有用
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode;
typedef LNode *LinkList;
//****************************************************************************
LinkList CreateList()//头插入法 建立带头节点的链表
{
LinkList L;
LinkList P;
int x;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;//建立空链表
printf("请输入第一个元素并以0作为输入结束的标志\n");
scanf("%d",&x);
while(x!=0)//以输入0作为输入结束标志
{
P=(LinkList)malloc(sizeof(LNode));
P->data=x;
P->next=L->next;
L->next=P;
scanf("%d",&x);
}
return L;
}
void printsum(LinkList L)//求链表各元素的和
{
LinkList p;
int sum=0;
p=L->next;
while(p){sum=sum+p->data;p=p->next;}
printf("%d\n",sum);
}
int main()
{
LinkList a,b,c,d,f;
ElemType e;
a=CreateList();
printsum(a);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询