当前位置:网站首页>dhu编程练习
dhu编程练习
2022-06-30 02:08:00 【qq_43403657】
3 删除链表第index个结点
#include <stdio.h>
#include<iostream>
struct student
{
int num;
struct student *next;
};
//从键盘读入数据创建链表,新结点插入到尾部
struct student *createByTail()
{
struct student *head;
struct student *p1, *p2;
int n;
n = 0;
p1 = p2 = (struct student*)malloc(sizeof(struct student));
scanf("%d", &p1->num);
head = NULL; //首先置链表为空链表
while (p1->num != -1) //num为-1,意味着用户输入结束
{
n = n + 1;
if (n == 1) //创建第一个结点
head = p1;
else
p2->next = p1;
p2 = p1; //p2始终指向最后一个结点(即尾指针)
p1 = (struct student*)malloc(sizeof(struct student)); //p1指向新结点
scanf("%d", &p1->num);
}
p2->next = NULL; //切记:最后一个结点的next赋值为NULL
return head;
}
//输出链表中的信息(num)
void displayLink(struct student *head)
{
struct student *p;
p = head;
printf("head-->");
while (p != NULL)
{
printf("%d-->", p->num);
p = p->next;
}
printf("tail\n");
}
//删除链表中第index个结点。index从1开始。
//由于可能删除第一个结点,所以函数返回头指针给主调函数
struct student *deleteNode(struct student *head, int index)
{
//要求:如果输入的index超出了链表的范围,则不删除,输出原链表。如果是空链表,则直接输出“head-->tail”。
//首先判断非空
if (!head)
return head;
//单独考虑删除头结点的情况
struct student *p=head ,*q; //这里p始终指向头结点
if (index == 1)
{
q = head;
head = head->next;
free(q);
return head;
}
//其他情况
else
{
int n = 1;
while (p->next)//注意这里是p->next,即head->next
{
if (n == index - 1)//找到删除结点的前一个结点p
{
q = p->next;
p->next = q->next;
if (q)free(q);//判断p是否指向最后一个结点,即q是否为空
break;//注意跳出循环
}
n++;
p = p->next;
}
return head;
}
}
int main()
{
struct student *head;
int index;
head = createByTail();
while (scanf("%d", &index) != -1)
{
head = deleteNode(head, index);
displayLink(head);
}
}
边栏推荐
- 018_ rate
- Let‘sPlayCurling
- The national industrial information security development research center issued the report on industrial information security situation in 2021
- Implementation of a simple camera based on pyqt5
- Conjecture of prime pairs in C language
- Matlab 2012a 绘制带箭头的线段
- C language continues (3n+1) conjecture
- After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost
- 当大学毕业感到迷茫怎么办?
- (4) Blender source code analysis flash window display process
猜你喜欢

C language score ranking

DTW学习(dynamic time warping)——思想、代码实现
![[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect](/img/96/e3afb2b9683cce7645afd483cc0844.png)
[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect

想转行,但不知道自己要做什么工作比较好?
![[MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce](/img/e7/fc2925a10ac5fb370dd221c3f4a46a.png)
[MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce

AI landing manufacturing: intelligent robots should have these four abilities

The (3n+1) conjecture that C language kills people without paying for their lives
![[graph neural network] overview of graph classification learning [2]: graph classification based on graph neural network](/img/5f/b23b64eed7f28ffd92c122b6859e2d.png)
[graph neural network] overview of graph classification learning [2]: graph classification based on graph neural network

Design and implementation of spark offline development framework

Widget uses setimageviewbitmap method to set bug analysis
随机推荐
What is idempotency? Detailed explanation of four interface idempotence schemes!
Geotools wkt coordinate system conversion
Share the source code of the website of graduation student record
DDoS "fire drill" service urges companies to prepare
Is it safe to open an account in Sinosteel futures?
DDoS threat situation gets worse
Let‘sPlayCurling
005_ button
ROS Bridge 笔记(01)— apt 安装、源码编译安装、安装依赖、运行显示
工具与生活服务
Jenkins continuous integration environment build 8 (configure mailbox server to send build results)
Conjecture of prime pairs in C language
Electron FAQ 54 - make your own fireworks based on electron
DDoS threat situation gets worse
Blue Bridge Cup stm32g431 - three lines of code for keys (long press, short press, click, double click)
Est - ce que la bourse en ligne est sécurisée? Dois - je ouvrir un compte pour la spéculation boursière?
Design and implementation of spark offline development framework
C language number prime
widget使用setImageViewBitmap方法设置bug分析
007_ checkbox