struct stu *node(int n)
时间: 2024-02-01 16:01:14 浏览: 34
函数node(int n)返回一个指向结构体类型为stu的指针。这个结构体包含了表示学生信息的各种字段,例如:“姓名”、“年龄”、“学号”等等。参数n表示要创建的节点数量。这个函数可以用于动态创建存储学生信息的节点,每一个节点都指向一个学生的信息。通过遍历这些节点,可以获取每个学生的信息。函数的返回类型是struct stu *,即指向结构体类型为stu的指针。这个指针可以用来访问和操作学生信息的节点,比如添加、删除和修改学生信息。函数的使用可以大大简化对学生信息的管理和操作,使得对学生信息的存储和处理更加高效和方便。
相关问题
修改如下代码:#include<stdio.h> #include<stdlib.h> struct stu { char name[20]; int no; struct stu *next; }; int create(struct stu *head,int n); void research(struct stu *head); int main() { int a,i; struct stu *head; head->next=NULL; scanf("%d",&a); create(head,a); research(head); return 0; } int create(struct stu *head,int n) { int i; head->next=NULL; struct stu *node; for(i=0;i<n;i++) { node= (struct stu*)malloc(sizeof(node)) ; scanf("%d",&node->no); node->next=head->next; head->next=node; } return 1; } void research(struct stu *head) { struct stu *p; p=head->next; while(p!=NULL) { printf("%d\n",p->no); p=p->next; } }
修改后的代码如下:
```c
#include<stdio.h>
#include<stdlib.h>
struct stu {
char name[20];
int no;
struct stu *next;
};
int create(struct stu *head, int n);
void research(struct stu *head);
int main() {
int a,i;
struct stu *head = (struct stu*)malloc(sizeof(struct stu)); // 为头结点分配内存
head->next = NULL;
scanf("%d",&a);
create(head, a);
research(head);
return 0;
}
int create(struct stu *head, int n) {
int i;
struct stu *node;
for(i=0; i<n; i++) {
node = (struct stu*)malloc(sizeof(struct stu));
scanf("%d",&node->no);
node->next = head->next;
head->next = node;
}
return 1;
}
void research(struct stu *head) {
struct stu *p;
p = head->next;
while(p != NULL) {
printf("%d\n",p->no);
p = p->next;
}
}
```
主要的修改如下:
1. 在`main`函数中为头结点动态分配内存,并将`head->next`初始化为`NULL`。
2. 在`create`函数中为每个新结点动态分配内存。
3. 在函数参数中将`*head`改为`head`,因为在函数内部并不会修改头指针的值,只需要修改头指针所指向的结点即可。
有一链式结构,定义如下 : 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; } /* 请在这里填写答案 */ 输入样例: 在这里给出一组输入。例如: 2 liming 23 maming 35
以下是实现无头结点的链表,有m个结点的函数create()的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu{
char name[20];
int no;
struct stu *next;
};
struct stu * create() {
int m;
scanf("%d", &m);
struct stu *head = NULL, *p = NULL;
for (int i = 0; i < m; i++) {
struct stu *node = (struct stu *)malloc(sizeof(struct stu));
scanf("%s %d", node->name, &node->no);
if (head == NULL) {
head = node;
} else {
p->next = node;
}
p = node;
}
if (p != NULL) {
p->next = NULL;
}
return head;
}
```
在主函数中,可以通过调用create()函数来创建链表,并且将返回值赋给指向链表头结点的指针p。然后,可以通过调用show()函数来输出链表中的内容。
完整的程序如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu{
char name[20];
int no;
struct stu *next;
};
struct stu * create() {
int m;
scanf("%d", &m);
struct stu *head = NULL, *p = NULL;
for (int i = 0; i < m; i++) {
struct stu *node = (struct stu *)malloc(sizeof(struct stu));
scanf("%s %d", node->name, &node->no);
if (head == NULL) {
head = node;
} else {
p->next = node;
}
p = node;
}
if (p != NULL) {
p->next = NULL;
}
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;
}
```
输入样例:
```
2
liming 23
maming 35
```
输出样例:
```
liming 23
maming 35
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)