有一链式结构,定义如下 : struct stu{ char name[20]; int no; struct stu *next; }; 创建一个函数create(),实现无头结点的链表,有m个结点,函数的返回值为链表的头指针。 函数接口定义: 在这里描述函数接口。例如: struct stu * create() 在这里解释接口参数。在函数中输入m,及m行数据,完成链表的创建。 裁判测试程序样例: 在这里给出函数被调用进行测试的例子。例如: #include <stdio.h> #include <stdlib.h> struct stu{ char name[20]; int no; struct stu *next;}; struct stu * create(); void show(struct stu *p){ while(p){ printf("%s %d\n",p->name,p->no); p=p->next; } } int main() { struct stu *p=NULL; p=create(); show(p); return 0; } /* 请在这里填写答案 */ 输入样例:
时间: 2024-03-17 16:47:08 浏览: 70
以下是实现题目要求的代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct stu {
char name[20];
int no;
struct stu *next;
};
struct stu *create() {
int m;
scanf("%d", &m);
struct stu *head = NULL, *tail = NULL;
for (int i = 0; i < m; i++) {
struct stu *p = (struct stu *)malloc(sizeof(struct stu));
scanf("%s %d", p->name, &p->no);
p->next = NULL;
if (head == NULL) {
head = tail = p;
} else {
tail->next = p;
tail = p;
}
}
return head;
}
void show(struct stu *p) {
while (p) {
printf("%s %d\n", p->name, p->no);
p = p->next;
}
}
int main() {
struct stu *p = NULL;
p = create();
show(p);
return 0;
}
```
输入样例:
```
3
Alice 1001
Bob 1002
Charlie 1003
```
输出样例:
```
Alice 1001
Bob 1002
Charlie 1003
```
阅读全文