当前位置:网站首页>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;
}
边栏推荐
- C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
- C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
- 将一串数字顺序后移
- Routing foundation - dynamic routing
- Qt QTimer类
- Minecraft plug-in service opening
- Linux binary installation Oracle database 19C
- commands out of sync. did you run multiple statements at once
- File upload Labs
- Sqli labs level 12
猜你喜欢

Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)

Qt——如何在QWidget中设置阴影效果

Linux安装Oracle Database 19c RAC

顺序表基本功能函数的实现

Jumping | Blue Bridge Cup

Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away

c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)

File upload Labs

Linux binary installation Oracle database 19C

Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
随机推荐
Kubernetes deploys Loki logging system
sqli-labs第12关
HCIA—应用层
Tcp/ip - transport layer
Sentinel 简单使用
Method recursion (Fibonacci sequence, frog jumping steps, tower of Hanoi problem)
Use the kaggle training model and download your own training model
ARP and ARP Spoofing
web安全--逻辑越权
Shortcut key to comment code and cancel code in idea
Realize bidirectional linked list (with puppet node)
Service de groupe minecraft
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
Linux安装Oracle Database 19c RAC
Linux二进制安装Oracle Database 19c
sqli-labs第2关
NPOI 导出Word 字号对应
Minecraft空岛服开服
Sqli labs (post type injection)
Finishing the interview essentials of secsha system!!!