建立一个有5个结点的单向链表。每个结点包含姓名、年龄和工资。编写两个函数,一个用
建立一个有5个结点的单向链表。每个结点包含姓名、年龄和工资。编写两个函数,一个用于建立链表,另一个用于输出链表。要求:1 建立链表的函数名为creat,数据输入和链表建立在函数中实现;
2 输出链表的函数名为output,用以实现creat建立的链表数据的输出;
3 在main函数中调用creat函数和output函数。
楼主才1money啊? #include<stdio.h>
#define M 5
#define NULL 0
#define LEN sizeof(struct worker)
struct worker
{
char name;
int age;
float wages;
struct worker *next;
};
int n;
struct worker *creat()
{
int i;
int wages;
struct worker *head;
struct worker *p1, *p2;
n = 0;
head = p1 = ( struct worker *)malloc(LEN);
head->next = NULL;
for ( i = 0; i < M; i++ )
{
p2 = ( struct worker *)malloc(LEN);
printf("input name & age & wages\n");
scanf("%s,%d,%d", p2->name, &p2->age, &wages);
n++;
p1->next = p2;
p1 = p2;
}
p1->next = NULL;
head->age = n;
return(head);
}
void output( struct worker *head )
{
struct worker *p1;
int i = 1;
p1 = head->next;
do
{
printf("%s,%d,%d\n",p1->name,p1->wages);
p1 = p1->next;
i++;
}
while ( p1 != NULL );
}
main()
{
struct worker *head;
head = creat();
printf("there are %d structs\n",n);
output (head);
}
页:
[1]