c语言问题14

来源:百度知道 编辑:UC知道 时间:2024/05/30 10:25:03
建立一个整数链表

你的问题还真多啊!以下程序调试通过,比较简单。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct chain
{
int value;
struct chain *next;
};
struct chain *create()
{
struct chain *head, *tail, *p;
int x;
head = tail = NULL;
printf("Input data.\n");
while (scanf("%d",&x) == 1) /*如果输入的是一个整型数据,那么向下执行*/
{
p = (struct chain *)malloc (sizeof (struct chain));
/*首先为要新创建的表元p开辟一个内存空间*/
p->value = x;
p->next = NULL;
if(head == NULL)
head = tail = p;
else
/*tail为倒数第二个表元指针,tail->next始终指向最后一个表元*/
tail = tail ->next;
tail ->next = p;
}
return head;
}

void main()
{
struct chain *p,*q;
q = create();
while(q)
{
printf("%d\