这道题目要求设计一个完整的程序,根据用户输入学生的人数n(n>2)以及每个学生的学号、姓名和成绩建立一个单链表,统计单链表存储的学生人数,并输出所有学生信息,去除指定位置的元素。
时间: 2024-05-11 18:18:10 浏览: 13
好的,这道题我们可以分为三个部分来完成:
1. 建立单链表并统计学生人数
2. 输出所有学生信息
3. 删除指定位置的元素
首先,我们需要定义一个学生结构体,包括学号、姓名和成绩:
```c
typedef struct student {
int id; // 学号
char name[20]; // 姓名
int score; // 成绩
struct student *next; // 指向下一个学生的指针
} Student;
```
然后,我们可以编写一个函数来创建单链表,并且统计学生人数:
```c
int createList(Student **head) {
int n, i;
printf("请输入学生人数:");
scanf("%d", &n);
if(n <= 2) {
printf("学生人数必须大于2!\n");
return -1;
}
*head = (Student *)malloc(sizeof(Student)); // 创建头结点
(*head)->next = NULL;
Student *p = *head;
for(i = 1; i <= n; i++) {
Student *node = (Student *)malloc(sizeof(Student)); // 创建新结点
printf("请输入第%d个学生的信息:\n", i);
printf("学号:");
scanf("%d", &(node->id));
printf("姓名:");
scanf("%s", node->name);
printf("成绩:");
scanf("%d", &(node->score));
node->next = NULL;
p->next = node; // 将新结点插入到链表尾部
p = node;
}
return n; // 返回学生人数
}
```
接着,我们编写一个函数来输出所有学生信息:
```c
void printList(Student *head) {
if(head->next == NULL) {
printf("链表为空!\n");
return;
}
Student *p = head->next;
printf("学生信息如下:\n");
while(p) {
printf("学号:%d,姓名:%s,成绩:%d\n", p->id, p->name, p->score);
p = p->next;
}
}
```
最后,我们编写一个函数来删除指定位置的元素:
```c
void deleteNode(Student *head, int pos) {
if(head->next == NULL) {
printf("链表为空!\n");
return;
}
int i = 1;
Student *p = head->next;
Student *prev = head;
while(p) {
if(i == pos) {
prev->next = p->next; // 将前一个结点的指针指向下下个结点
free(p); // 释放被删除的结点
printf("第%d个学生删除成功!\n", pos);
return;
}
i++;
prev = p;
p = p->next;
}
printf("第%d个学生不存在!\n", pos);
}
```
最后,我们将这些函数整合起来,写出完整的程序:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct student {
int id; // 学号
char name[20]; // 姓名
int score; // 成绩
struct student *next; // 指向下一个学生的指针
} Student;
int createList(Student **head) {
int n, i;
printf("请输入学生人数:");
scanf("%d", &n);
if(n <= 2) {
printf("学生人数必须大于2!\n");
return -1;
}
*head = (Student *)malloc(sizeof(Student)); // 创建头结点
(*head)->next = NULL;
Student *p = *head;
for(i = 1; i <= n; i++) {
Student *node = (Student *)malloc(sizeof(Student)); // 创建新结点
printf("请输入第%d个学生的信息:\n", i);
printf("学号:");
scanf("%d", &(node->id));
printf("姓名:");
scanf("%s", node->name);
printf("成绩:");
scanf("%d", &(node->score));
node->next = NULL;
p->next = node; // 将新结点插入到链表尾部
p = node;
}
return n; // 返回学生人数
}
void printList(Student *head) {
if(head->next == NULL) {
printf("链表为空!\n");
return;
}
Student *p = head->next;
printf("学生信息如下:\n");
while(p) {
printf("学号:%d,姓名:%s,成绩:%d\n", p->id, p->name, p->score);
p = p->next;
}
}
void deleteNode(Student *head, int pos) {
if(head->next == NULL) {
printf("链表为空!\n");
return;
}
int i = 1;
Student *p = head->next;
Student *prev = head;
while(p) {
if(i == pos) {
prev->next = p->next; // 将前一个结点的指针指向下下个结点
free(p); // 释放被删除的结点
printf("第%d个学生删除成功!\n", pos);
return;
}
i++;
prev = p;
p = p->next;
}
printf("第%d个学生不存在!\n", pos);
}
int main() {
Student *head = NULL;
int n = createList(&head);
if(n == -1) {
return 0;
}
printList(head);
int pos;
printf("请输入要删除的学生位置:");
scanf("%d", &pos);
deleteNode(head, pos);
printList(head);
return 0;
}
```
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)