当前位置:网站首页>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);
}
}
边栏推荐
- Understand AQS principle (flow chart and synchronous queue diagram)
- Oppo mobile phone search
- AI落地制造业:智能机器人应具备这4种能力
- [MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce
- DDoS attacks and destructive ripple effects against online gamers
- 006_ radio
- 003_ color
- NCA: the nine year old has launched a DDoS attack
- Global communication infrastructure faces apt, robotics and DDoS; The weakest mobile network
- C language continues (3n+1) conjecture
猜你喜欢

004_ icon
![[mrctf2020]ezpop-1 | PHP serialization](/img/65/9b7a3ae3552d8784b0c77a1130d762.png)
[mrctf2020]ezpop-1 | PHP serialization

ROS bridge notes (01) - APT installation, source code compilation and installation, installation dependency, and operation display

一次 Keepalived 高可用的事故,让我重学了一遍它!
![Scala basics [introduction and installation]](/img/c5/9e62070719e1e0db29b0e44b0f0bc1.png)
Scala basics [introduction and installation]

(4) Blender source code analysis flash window display process

What is idempotency? Detailed explanation of four interface idempotence schemes!

Is the processor the main factor in buying a mobile phone?

Matlab 2012a drawing line segment with arrow

UE5的蓝图节点拷贝到UE4后连线和属性值全部丢失了
随机推荐
Scala basics [introduction and installation]
Est - ce que la bourse en ligne est sécurisée? Dois - je ouvrir un compte pour la spéculation boursière?
如何制作CSR(Certificate Signing Request)文件?
[MySQL 05] SUSE 12 SP5 modifies the MySQL password for the first time after installing MySQL
209. minimum length subarray - sliding window
CheapSwap 协议的诞生
DDoS threat situation gets worse
记录生产的一次OOM异常
【MySQL 05】SUSE 12 SP5 安装MySQL后第一次修改mysql密码
Want to change careers, but don't know what you want to do?
Using face_ Recognition library reports an error reason: cudnn_ STATUS_ NOT_ SUPPORTED
Is the processor the main factor in buying a mobile phone?
Understand AQS principle (flow chart and synchronous queue diagram)
C language continues (3n+1) conjecture
Copy entire directory to output folder maintain folder structure- Copy entire directory to output folder maintaining the folder structure?
想转行,但不知道自己要做什么工作比较好?
7 — filter
26. common interview questions of algorithm
Restore a 35k-55k Tencent Android Senior Engineer Interview
(1) Basic learning - figure out the difference between pin, pad, port, IO and net