当前位置:网站首页>dhu编程练习
dhu编程练习
2022-06-30 02:08:00 【qq_43403657】
2 在链表指定位置插入结点
#include <stdio.h>
#include <stdlib.h>
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处插入s指针所指向的结点。index从1开始。
//由于可能插在第一个结点,所以函数返回头指针给主调函数
struct student *insertNode(struct student *head, struct student *s, int index)
{
//计算原链表有多少结点,为后面判断index下标是否正常铺垫
struct student *p;
p = head;
int n=0;
while (p)
{
n++;
p = p->next;
}
//插入结点,考虑第一个位置插入的特殊情况
struct student *q;
q = head;
if (index <= n + 1)//保证index下标的可行性
{
if (index == 1)
{
s->next = q;
head = s;
}
else if (index!=0) {
//Warning:这里index下标必须要注意判断!!
for (int i = 2; i < index; i++)//保证q指向插入结点位置的前驱结点
{
q = q->next;
}
s->next = q->next;
q->next = s;
}
}
return head;
}
int main()
{
struct student *head;
int index, data;
head = createByTail();
while (scanf("%d%d", &index, &data) != -1)
{
struct student * s = (struct student *) malloc(sizeof(struct student));
s->num = data;
head = insertNode(head, s, index);
displayLink(head);
}
return 0;
}
边栏推荐
- After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost
- Geotools wkt coordinate system conversion
- If mybaits cannot query the data, it can query how to change it in the database
- If you want to install a set of monitoring, what is the process? How much is it?
- Leetcode 46 Full arrangement (February 15, 2022)
- Method of converting songs from DTS to MP3
- What problems can cloud storage architecture solve for Devops?
- 【MySQL 04】使用MySQL Workbench 8.0 CE 備份及恢複Linux中的MySQL數據庫
- UE5的蓝图节点拷贝到UE4后连线和属性值全部丢失了
- DTW学习(dynamic time warping)——思想、代码实现
猜你喜欢

What problems can cloud storage architecture solve for Devops?

Design and implementation of spark offline development framework

【MySQL 04】使用MySQL Workbench 8.0 CE 備份及恢複Linux中的MySQL數據庫

How does payment splitting help B2B bulk commodity transactions?

001_ layout

Implementation of a simple camera based on pyqt5

Derivation of univariate polynomial in C language
![[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux](/img/e7/fc2925a10ac5fb370dd221c3f4a46a.png)
[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux

8 — router

Conjecture of prime pairs in C language
随机推荐
C language I want to pass
C language output integer in another format
Blitzkrieg companies with DDoS attacks exceeding 100gbps in 2014
7 — filter
Want to change careers, but don't know what you want to do?
Record an oom exception in production
Fake divorce turns into real divorce. What about property
26. common interview questions of algorithm
搞透AQS原理(流程圖及同步隊列圖解)
Geotools wkt coordinate system conversion
Jenkins continuous integration environment build 8 (configure mailbox server to send build results)
Scala基础【入门及安装】
Module import reload method
26.算法常用面试题
(1) Basic learning - figure out the difference between pin, pad, port, IO and net
What is idempotency? Detailed explanation of four interface idempotence schemes!
当大学毕业感到迷茫怎么办?
C language score ranking
The birth of the cheapswap protocol
widget使用setImageViewBitmap方法设置bug分析