动态链表

来源:百度知道 编辑:UC知道 时间:2024/08/20 15:39:42
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct M707)
#define Null 0
struct M707
{
int number;
char name[20];
int height;
struct M707 *next;
};

struct M707 *creat(void)
{
int n;
struct M707 *p;
struct M707 *q;
struct M707 *head;
n=1;
p=q=(struct M707 *)malloc(LEN);
scanf("%d,%s,%d",&p->number,&p->name,&p->height);
head=Null;
for(;p->number!=0;n++)
{if(n==1) head=p;
else q->next=p;
q=p;
p=(struct M707 *)malloc(LEN);
scanf("%d,%s,%d",&p->number,&p->name,&p->height);}
q->next=0;
return (head);
}

main()
{

struct M707 *p;
p=creat();
for(;p!=0;p=p->next)
printf("%-5d%-20s%6d"

struct M707 *creat(void)
{
int n;
struct M707 *p;
struct M707 *q;
struct M707 *head;
n=1;
p=q=(struct M707 *)malloc(LEN);
scanf("%d,%d,%s",&p->number,&p->height,&p->name);//换了下输入参数顺序
head=Null;
for(;p->number!=0;n++)
{if(n==1) head=p;
else q->next=p;
q=p;
p=(struct M707 *)malloc(LEN);
scanf("%d,%d,%s",&p->number,&p->height,&p->name);}
q->next=0;
return (head);
}

main()
{

struct M707 *p;
p=creat();
for(;p!=0;p=p->next)
printf("%-5d%-20s%6d\n",p->number,p->name,p->height);//少了一个输出参数
system("pause");
}