当前位置:网站首页>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;
}
边栏推荐
- Service de groupe minecraft
- Chrome debugging
- Minecraft install resource pack
- 用数字 5,5,5,1 ,进行四则运算,每个数字当且仅当用一次,要求运算结果值为 24
- Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
- Mutex
- Minecraft安装资源包
- sqli-labs(POST类型注入)
- TCP/IP—传输层
- [blackmail virus data recovery] suffix Hydra blackmail virus
猜你喜欢
随机推荐
HCIA—應用層
Googlenet network explanation and model building
[flask] ORM one-to-one relationship
OpenShift 容器平台社区版 OKD 4.10.0部署
Makefile基本原理
Network security - summary and thinking of easy-to-use fuzzy tester
sqli-labs第12关
Don't know mock test yet? An article to familiarize you with mock
Web security -- core defense mechanism
链表经典面试题(反转链表,中间节点,倒数第k个节点,合并分割链表,删除重复节点)
HCIA - data link layer
Judge whether it is Sudoku
Sqli labs level 1
Qt QTimer类
sqli-labs第8关(布尔盲注)
小米电视不能访问电脑共享文件的解决方案
Jumping | Blue Bridge Cup
C# 高德地图 根据经纬度获取地址
gocv拆分颜色通道
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)