当前位置:网站首页>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);
}
}
边栏推荐
- Unity2d-- add keys to animation and bind events
- Realization of a springboard machine
- Tencent released the first Office Photo 23 years ago. It's so chronological
- Encapsulate a complete version of the uniapp image and video upload component, which can be used immediately, switch between images and videos, customize the upload button style, delete the button sty
- 006_ radio
- 9 — 正则校验集合
- [mrctf2020]ezpop-1 | PHP serialization
- 工具与生活服务
- Unity2D--给动画添加关键帧并绑定事件
- A summary of the quantification of deep network model
猜你喜欢
Understand AQS principle (flow chart and synchronous queue diagram)
Circular right shift of array elements in C language
Restore a 35k-55k Tencent Android Senior Engineer Interview
012_ switch
Using face_ Recognition library reports an error reason: cudnn_ STATUS_ NOT_ SUPPORTED
006_ radio
[naturallanguageprocessing] [multimodality] ofa: unified architecture, tasks and modes through a simple sequence to sequence learning framework
CTF introductory learning (WEB direction)
What is idempotency? Detailed explanation of four interface idempotence schemes!
[protection mode] segment descriptor
随机推荐
C language score ranking
[MySQL 06] backup and restore MySQL database in Linux + docker container environment
網上炒股安全麼?炒股需要開戶嗎?
33Mysql
9 — 正则校验集合
DTW learning (dynamic time warping) -- Thought and code implementation
Design and implementation of spark offline development framework
If mybaits cannot query the data, it can query how to change it in the database
Knowledge payment cannot escape the essence of "anxiety"
Pytoch modifies the hook source code to obtain per layer output parameters (with layer name)
A summary of the quantification of deep network model
Let‘sPlayCurling
Learning C language from scratch day 026
002_ container
Blitzkrieg companies with DDoS attacks exceeding 100gbps in 2014
Some practical knowledge about PR
Who can use redis expired monitoring to close orders and get out of here!
Looking for thesaurus data [closed]
Matlab 2012a drawing line segment with arrow
(1) Basic learning - figure out the difference between pin, pad, port, IO and net