当前位置:网站首页>Find the node with the smallest value range in the linked list and move it to the front of the linked list
Find the node with the smallest value range in the linked list and move it to the front of the linked list
2022-07-02 08:46:00 【Programmers on the road】
One 、 Title Description
It is known that the linear linked list consists of list Pointed out that , The construction of chain nodes is (data,next), Please write an algorithm , Move the node with the smallest value of the data field in the linked list to the front of the linked list .( You cannot apply for additional nodes )( Better reading experience , Please visit Programmers are on the road )
Two 、 Analysis answers
The main solution is , Traversing the linked list , Find the smallest node min, And the precursor of this node pre_min, Then move it to the front of the linked list .
It is worth noting that , Because the node structure requires a one-way single linked list , therefore , If you want to move min, We must find his precursor . If it's a two-way list , You don't need to record the precursor node .
int move_min_to_first(PNode head){
PNode pre_p = head->next, p = pre_p->next;
// Record the smallest element node and its precursor
PNode pre_min = head, min = pre_p;
// Judge whether the list is empty
if(head == NULL || head->next == NULL){
return -1;
}
// Traversing the linked list , Find the smallest element node
while§{
if(min->data > p->data){
pre_min = pre_p;
min = p;
}
pre_p = p;
p = p->next;
}
// Move to the front of the linked list
pre_min->next = min->next;
min->next = head->next;
head->next = min;
return 1;
}
The complete executable code is as follows :
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}Node,*PNode;
/* Way of thinking : Traversing the linked list , Find the smallest element node and its precursor node , Then place the smallest node at the front of the linked list . Return value : -1 Linked list is empty. , Can't find ; 0 To find the failure ; 1 Find success . */
int move_min_to_first(PNode head){
PNode pre_p = head->next, p = pre_p->next;
// Record the smallest element node and its precursor
PNode pre_min = head, min = pre_p;
// Judge whether the list is empty
if(head == NULL || head->next == NULL){
return -1;
}
// Traversing the linked list , Find the smallest element node
while(p){
if(min->data > p->data){
pre_min = pre_p;
min = p;
}
pre_p = p;
p = p->next;
}
// Move to the front of the linked list
pre_min->next = min->next;
min->next = head->next;
head->next = min;
return 1;
}
// Head interpolation method to establish a single linked list with head nodes
PNode creat_list(){
// Application header node
PNode head = (PNode)malloc(sizeof(Node));
head->next = NULL;
scanf("%d",&(head->data)); // The data field of the header node can store summary points
// New elements
PNode p = NULL;
int i;
for(i=0;i<(head->data);i++){
p = (PNode)malloc(sizeof(Node));
scanf("%d",&(p->data));
// This is the key of head insertion
p->next = head->next;
head->next = p;
}
return head;
}
void print_list(PNode head){
PNode p = head->next;
while(p){
printf("p->data: %d \t",p->data);
p = p->next;
}
printf("\n");
}
int main(){
PNode head = creat_list();
print_list(head);
move_min_to_first(head);
print_list(head);
return 0;
}
边栏推荐
- sqli-labs第2关
- Openfeign facile à utiliser
- OpenFeign 简单使用
- Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)
- Makefile Fundamentals
- Nacos download, start and configure MySQL database
- sqli-labs第8关(布尔盲注)
- 用数字 5,5,5,1 ,进行四则运算,每个数字当且仅当用一次,要求运算结果值为 24
- kubeadm部署kubernetes v1.23.5集群
- 队列的基本概念介绍以及典型应用示例
猜你喜欢

KubeSphere 虚拟化 KSV 安装体验

Googlenet network explanation and model building

STM32 new project (refer to punctual atom)

Getting started with k8s: building MySQL with Helm

OpenShift构建镜像

Sentinel 简单使用

Kubesphere virtualization KSV installation experience

类和对象(类和类的实例化,this,static关键字,封装)

Web security -- Logical ultra vires

sqli-labs(POST类型注入)
随机推荐
Web security -- Logical ultra vires
Linux二进制安装Oracle Database 19c
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
Minecraft air Island service
Sentinel easy to use
Web技术发展史
图像变换,转置
OpenShift 部署应用
c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)
Benefits of ufcs of D
gocv opencv exit status 3221225785
Hcia - Application Layer
Dip1000 runaway
Web security -- core defense mechanism
HackTheBox-Gunship
Use Wireshark to grab TCP three handshakes
idea中注释代码取消代码的快捷键
HCIA - data link layer
Tcp/ip - transport layer
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)