#include<stdio.h>
#include<malloc.h>
#define ok 1
#define error 0
typedef int Status;
typedef struct test
{
int a;
test *next;
}test,*ptest;
Status create(ptest head)
{
if(!head)
{
return error;
}
ptest p = head;
while(1)
{
ptest q = p;
p->next = (ptest)malloc(sizeof(test));
p = p->next;
printf("input number:");
scanf("%d",&p->a);
if(p->a == 0)
{
q->next = NULL;
free(p);
return ok;
}
}
return error;
}
Status output(ptest head)
{
int sum = 0;
if(!head)
{
return error;
}
for(ptest p = head->next; p != NULL; p = p->next)
{
sum = sum + p->a;
}
printf(" %d ", sum);
return ok;
}
Status main()
{
test head;
if(create(&head))
{
printf("链表创建成功!!!\n\n");
if(output(&head))
{
printf("输出总和");
}
else
{
printf("输出失败");
}
}
else
{
printf("链表创建失败!!!\n\n");
return error;
}
return ok;
}
链表和
最新推荐文章于 2023-02-05 15:26:42 发布