当前位置:网站首页>寻找链表中值域最小的节点并移到链表的最前面
寻找链表中值域最小的节点并移到链表的最前面
2022-07-02 06:32:00 【程序员在旅途】
一、题目描述
已知线性链表由list指出,链节点的构造为(data,next),请写一个算法,将链表中数据域值最小的那个节点移动到链表的最前面。(不能申请额外的节点)(更好的阅读体验,请访问程序员在旅途)
二、分析解答
主要解题思路就是,遍历链表,找到最小的那个节点min,以及该节点的前驱pre_min,然后将其移到链表的最前面。
值得注意的是,由于节点结构要求的是单向单链表,因此,如果要移动min,必须要找到他的前驱。如果是双向链表,就可以不用记录前驱结点了。
int move_min_to_first(PNode head){
PNode pre_p = head->next, p = pre_p->next;
//将最小的元素节点及其前驱记录下来
PNode pre_min = head, min = pre_p;
//判断链表是否为空
if(head == NULL || head->next == NULL){
return -1;
}
//遍历链表,寻找最小元素节点
while§{
if(min->data > p->data){
pre_min = pre_p;
min = p;
}
pre_p = p;
p = p->next;
}
//移动到链表的最前面
pre_min->next = min->next;
min->next = head->next;
head->next = min;
return 1;
}
完整可执行程序代码如下:
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}Node,*PNode;
/* 方法思路: 遍历链表,找到其中最小的元素节点及其前驱结点,然后将最小的节点放置在链表最前面。 返回值: -1 链表为空,无法寻找; 0 查找失败; 1查找成功。 */
int move_min_to_first(PNode head){
PNode pre_p = head->next, p = pre_p->next;
//将最小的元素节点及其前驱记录下来
PNode pre_min = head, min = pre_p;
//判断链表是否为空
if(head == NULL || head->next == NULL){
return -1;
}
//遍历链表,寻找最小元素节点
while(p){
if(min->data > p->data){
pre_min = pre_p;
min = p;
}
pre_p = p;
p = p->next;
}
//移动到链表的最前面
pre_min->next = min->next;
min->next = head->next;
head->next = min;
return 1;
}
//头插法建立带有头结点的单链表
PNode creat_list(){
//申请头结点
PNode head = (PNode)malloc(sizeof(Node));
head->next = NULL;
scanf("%d",&(head->data)); // 头结点的数据域可以存放总结点数
//新增元素
PNode p = NULL;
int i;
for(i=0;i<(head->data);i++){
p = (PNode)malloc(sizeof(Node));
scanf("%d",&(p->data));
//这是头插法的关键所在
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)
- Qt QTimer类
- gocv拆分颜色通道
- Luogu greedy part of the backpack line segment covers the queue to receive water
- Use the kaggle training model and download your own training model
- Linux安装Oracle Database 19c RAC
- Simple implementation scheme of transcoding and streaming (I)
- [untitled]
- Openfeign is easy to use
- 顺序表基本功能函数的实现
猜你喜欢
Data asset management function
Minecraft群組服開服
Application of kotlin - higher order function
Nacos 下载启动、配置 MySQL 数据库
方法递归(斐波那契数列,青蛙跳台阶,汉诺塔问题)
Web技术发展史
Openfeign is easy to use
Valin cable: BI application promotes enterprise digital transformation
Sqli labs level 8 (Boolean blind note)
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
随机推荐
Openfeign is easy to use
Jumping | Blue Bridge Cup
OpenShift构建镜像
Dip1000 runaway
History of Web Technology
Chrome debugging
选择排序和插入排序
Detailed explanation of NIN network
文件上传-upload-labs
程序猿学英语-指令式编程
Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
Matlab - autres
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
Use the kaggle training model and download your own training model
DWORD ptr[]
sqli-labs第1关
Sqli labs level 12
16: 00 interview, came out at 16:08, the question is really too
Gateway is easy to use
一、Qt的核心类QObject