当前位置:网站首页>DHU programming exercise
DHU programming exercise
2022-06-30 02:09:00 【qq_ forty-three million four hundred and three thousand six hun】
2 Insert a node at the specified position in the linked list
#include <stdio.h>
#include <stdlib.h>
struct student
{
int num;
struct student *next;
};
// Read data from the keyboard to create a linked list , Insert new node to tail
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; // First set the linked list as an empty linked list
while (p1->num != -1) //num by -1, It means that the user input ends
{
n = n + 1;
if (n == 1) // Create the first node
head = p1;
else
p2->next = p1;
p2 = p1; //p2 Always point to the last node ( Tail pointer )
p1 = (struct student*)malloc(sizeof(struct student)); //p1 Point to the new node
scanf("%d", &p1->num);
}
p2->next = NULL; // Of the last node next The assignment is NULL
return head;
}
// Output the information in the linked list (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");
}
// In the list No index Insert s The node pointed to by the pointer .index from 1 Start .
// Because it may be inserted at the first node , So the function returns the header pointer to the calling function
struct student *insertNode(struct student *head, struct student *s, int index)
{
// Calculate the number of nodes in the original linked list , For later judgment index Whether the subscript paves the way normally
struct student *p;
p = head;
int n=0;
while (p)
{
n++;
p = p->next;
}
// Insert node , Consider the special case of the first position insertion
struct student *q;
q = head;
if (index <= n + 1)// Guarantee index Feasibility of subscript
{
if (index == 1)
{
s->next = q;
head = s;
}
else if (index!=0) {
//Warning: here index Subscripts must be judged carefully !!
for (int i = 2; i < index; i++)// Guarantee q Point to the precursor node where the node is inserted
{
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;
}
边栏推荐
- 云存储架构能解决 DevOps 的什么问题?
- 工具与生活服务
- Thinking carefully and fearfully: a software can be transmitted online to monitor whether employees want to "run away"
- Let‘sPlayCurling
- CTF入门学习(Web方向)
- Scala basics [introduction and installation]
- 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)
- CTF introductory learning (WEB direction)
猜你喜欢

012_ switch

007_ checkbox

Share the source code of the website of graduation student record

Understand AQS principle (flow chart and synchronous queue diagram)
![[MySQL 05] SUSE 12 SP5 modifies the MySQL password for the first time after installing MySQL](/img/37/d24c9e5fad606d2623900ad018b6af.png)
[MySQL 05] SUSE 12 SP5 modifies the MySQL password for the first time after installing MySQL

Gesture digital enlightenment learning machine
![[pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars](/img/8f/c0cc1c8d19060a60d92c0d72f8b93d.png)
[pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars

Jenkins continuous integration environment construction VII (Jenkins parametric construction)

DMX configuration

什么是幂等性?四种接口幂等性方案详解!
随机推荐
Where can I find a pre training model for pytoch model training?
Mobaihe cm201-2-ch-hi3798mv300-300h-emmc and NAND_ Infrared Bluetooth voice_ Brush firmware package
The birth of the cheapswap protocol
018_ rate
记录生产的一次OOM异常
Electron FAQ 54 - make your own fireworks based on electron
DDoS surge in mobile and data centers
Implementation of a simple camera based on pyqt5
26. common interview questions of algorithm
What problems can cloud storage architecture solve for Devops?
Blue Bridge Cup stm32g431 - three lines of code for keys (long press, short press, click, double click)
Want to change careers, but don't know what you want to do?
013_ slider
002_ container
9 — 正则校验集合
006_ radio
[graph neural network] summary of graph classification study [3]: evaluation of graph classification methods and future research directions
SCP remote copy command record
How does payment splitting help B2B bulk commodity transactions?
Learning C language from scratch day 026