当前位置:网站首页>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;
}
边栏推荐
- IP协议与IP地址
- Kubesphere virtualization KSV installation experience
- OpenShift 部署应用
- Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
- Realize bidirectional linked list (with puppet node)
- PCL calculates the intersection of three mutually nonparallel planes
- Nacos download, start and configure MySQL database
- OpenShift构建镜像
- Function ‘ngram‘ is not defined
- NPOI 导出Word 字号对应
猜你喜欢
Minecraft安装资源包
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
Sqli labs level 8 (Boolean blind note)
Data asset management function
Chrome debugging
HCIA - application layer
OpenFeign 簡單使用
Use Wireshark to grab TCP three handshakes
Nacos 下载启动、配置 MySQL 数据库
Minecraft群組服開服
随机推荐
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
Gateway is easy to use
idea中注释代码取消代码的快捷键
Hcia - Application Layer
File upload Labs
路由基础—动态路由
Openshift build image
Zipkin is easy to use
gocv opencv exit status 3221225785
[flask] ORM one-to-one relationship
ARP及ARP欺骗
HCIA - application layer
Openfeign is easy to use
Minecraft群组服开服
Sentinel 简单使用
ARP and ARP Spoofing
HCIA—应用层
Don't know mock test yet? An article to familiarize you with mock
Qt的拖动事件
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)