//动态链表 #include "stdio.h" #include "stdlib.h" #define LEN sizeof(struct Student) struct Student{ long num; float score; struct Student *next; }; int n; //全局变量 struct Student*creat(void) //建立链表 { //此函数无需放入形参 struct Student *head ,*p1,*p2; n=0; p1=p2=(struct Student *)malloc(LEN); scanf("%ld%f",&p1->num,&p1->score); while(p1->num!=0) //此处判断遇 0 代表结束 { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct Student *) malloc(LEN); scanf("%ld%f",&p1->num,&p1->score); } p2->next=NULL; return head; //返回值是链表头 } void del(struct Student *head) //删除结点 { struct Student *p1,*p2,*p; int k,a=n; printf("请输入要删除的结点:\n"); scanf("%d",&k); p1=head; while(n==a&&p1->next!=NULL) { if(head->num==k) //删除的是头结点 { p1=head; // 原头结点 p2= p1->next; // 新的头结点 free(p1); // 释放原头结点内存空间 head =p2; // 头指针向后移一位 n-=1; } p2=p1; p1=p1->next; if(p1->num==k) { p=p1->next; p2->next=p; n-=1; } } } void print(struct Student *head)//输出链表 { struct Student *p; p=head; printf("\nNow,These %d reconds are:\n",n); if(head!=NULL) do { printf("%ld %5.1f\n",p->num,p->score); p=p->next; }while(p!=NULL); } int main() { struct Student *pt; pt=creat(); del(pt); print(pt); return 0; }怎么删除头节点
时间: 2024-02-14 22:12:02 浏览: 27
要删除头结点时,需要先保存下一个结点的指针,然后释放头结点的内存空间,最后将头指针指向下一个结点。具体操作可以参考以下代码:
```
if(head->num==k) // 删除的是头结点
{
p1=head; // 原头结点
p2= p1->next; // 新的头结点
free(p1); // 释放原头结点内存空间
head =p2; // 头指针向后移一位
n-=1;
}
```
相关问题
#include <stdio.h> #include <stdlib.h> /*其它说明*/ #include <string.h> /*字符串函数*/ #include <time.h> #define LEN sizeof(STUDENT) typedef struct stu /*定义结构体数组用于缓存数据*/ { char num[6]; //学号 char name[5]; //姓名 int score[3]; //三科成绩 int sum; //总成绩 float average; //平均成绩 int order; //排序 int order1; //排名 struct stu *next; } STUDENT; STUDENT *Init();/*初始化函数*/ int Menu_Select();/*菜单选择函数*/ STUDENT *Create(); /*输入函数*/ void Print(STUDENT *head); /* 显示全部记录函数*/ void Search(STUDENT *head);/*查找记录函数*/ STUDENT *Modify(STUDENT *head);/*修改记录函数*/ STUDENT *Delete(STUDENT *head);/*删除记录函数*/ STUDENT *Sort(STUDENT *head);/*排序函数*/ STUDENT *Insert(STUDENT *head, STUDENT *New); /*插入记录函数*//*TODO: 排序排名 功能描述: 按照降序给链表排序,排序存在order,排名存在order1 参数说明:head-STUDENT型指针 返回值说明:STUDENT型指针 */ STUDENT *Sort(STUDENT *head) { return (head); }补全函数
这个函数似乎没有实现排序排名的功能,我来帮你补充一下:
```
STUDENT *Sort(STUDENT *head)
{
STUDENT *p, *q, *tail;
int len = 0, i, j;
// 获取链表长度
for (p = head; p != NULL; p = p->next) {
len++;
}
// 外层循环控制比较轮数
for (i = 0; i < len - 1; i++) {
tail = head;
p = head->next;
q = p;
// 内层循环进行两两比较
for (j = 0; j < len - i - 1; j++) {
if (p->sum > q->sum) {
// 交换节点数据
if (p == head->next) {
head->next = q;
p->next = q->next;
q->next = p;
tail = q;
q = p->next;
} else {
tail->next = q;
p->next = q->next;
q->next = p;
tail = q;
q = p->next;
}
} else {
tail = p;
p = q;
q = p->next;
}
}
}
// 排名
p = head;
for (i = 1; i <= len; i++) {
p->order = i;
p = p->next;
}
// 排序完成,返回头节点
return head;
}
```
这个函数的实现思路是使用冒泡排序对链表进行排序,同时进行排名。排序的依据是每个节点的 `sum` 字段,也就是总成绩。排完序之后,遍历链表,给每个节点的 `order1` 字段赋上相应的排名即可。
#include <stdio.h> #include <stdlib.h> #define LEN sizeof(nod) typedef struct node { int data; struct node *next; }nod; nod *create() { /**/ /**/ } int count(nod *head) { /**/ /**/ } int main() { nod *head; head=/**/ /**///调用create函数 printf("%d",/**/ /**/); //调用count函数 return 0; }函数count的功能是:返回链表head中结点的个数。需在第一题写完create函数基础上将count函数与main函数补充完整。
下面是完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(nod)
typedef struct node {
int data;
struct node *next;
} nod;
nod *create() {
nod *head, *p1, *p2;
int n, i;
head = NULL;
printf("请输入链表中结点的个数:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
p1 = (nod *) malloc(LEN);
printf("请输入第%d个结点的值:", i);
scanf("%d", &p1->data);
p1->next = NULL;
if (head == NULL)
head = p1;
else
p2->next = p1;
p2 = p1;
}
return head;
}
int count(nod *head) {
int num = 0;
nod *p = head;
while (p != NULL) {
num++;
p = p->next;
}
return num;
}
int main() {
nod *head;
head = create();
printf("链表中结点的个数为:%d", count(head));
return 0;
}
```
在create函数中,我们使用了一个for循环来创建链表。首先,我们输入了链表中结点的个数n。然后,我们使用一个指针p1来创建新结点,并输入结点的值。如果链表为空,我们将head指向该结点;否则,我们让p2指向最后一个结点,并将p1挂在p2的后面。最后,我们返回head指针,即链表的头指针。
在count函数中,我们使用了一个while循环来遍历链表。我们定义了一个计数器num,并将其初始化为0。然后,我们使用一个指针p来遍历链表,每经过一个结点,我们就将num加1。最后,我们返回num,即链表中结点的个数。
在main函数中,我们首先调用create函数来创建链表,并将返回的头指针赋值给head。然后,我们调用count函数来计算链表中结点的个数,并将结果输出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)