C语言创建节点

建立一个5个节点的单向链表,每个节点包含姓名,年纪,工资,编写两个函数,一个用于建立链表的creat函数,一个用于输出的output函数;用主函数main调用 creat函数和output函数 急需

在这个程序的基础上改下,刚好无聊写了下。关键还是自己去想。#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 50
struct person
{
char name[MAX];
int number;
struct person *next;
};struct person * input()
{
struct person *p,*q,*head;
head = NULL;
char temp[MAX];
puts("In put name:");
while(gets(temp)!=NULL && temp[0] != '\0')
{
p = (struct person*)malloc(sizeof(struct person));
if(head == NULL)
head = p;
else
q->next = p;
p->next = NULL;
strcpy(p->name,temp);
puts("number:");
scanf("%d",&p->number);
while(getchar()!='\n')
continue;
puts("The next name:");
q = p; }
return head;
}
void out(struct person *head)
{
struct person *p;
if(head == NULL)
puts("Not name in line");
else
{
p = head;
puts("---------------------");
puts("the person with number:");
while(p)
{
{
printf("name is %s the number is %d\n",p->name,p->number);
p=p->next;
}

}
puts("out end");
}
}

void cleanup(struct person *head)
{
struct person *p;
p=head;
while(p!=NULL)
{
free(p);
p=p->next;
}
}
main()
{
struct person *head;

head = input();
out(head);
cleanup(head);

return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-17
应该再加一个函数比较好、加一个插入元素到链表的函数、创建链表和添加元素是不同的概念、过程也不同、、
相似回答