当前位置:网站首页>2022-07-22 review linked list operation and some problems
2022-07-22 review linked list operation and some problems
2022-07-23 13:44:00 【ShaYQ】
/* File: linked.cpp Function: Review some operations of the linked list , Find one-way reciprocal N node 、 Public nodes 、 Closed loop 、 Reverse of linked list Writer: syq Time: 2022-07-22 */
#include <stdio.h>
#include <iostream>
struct link_node
{
int value;
struct link_node* pnext;
};
/* Initialize linked list , Return head pointer */
struct link_node* Initlinknode(int nsize)
{
struct link_node* phead = (struct link_node*)malloc(sizeof(struct link_node));
std::cout<<"malloc:"<<phead<<std::endl;
phead->pnext = nullptr;
phead->value = 1;
struct link_node* pcurrnt = phead;
for(int i = 0; i < nsize-1; i ++)
{
pcurrnt->pnext = (struct link_node*)malloc(sizeof(struct link_node));
std::cout<<"malloc:"<<pcurrnt->pnext<<std::endl;
pcurrnt = pcurrnt->pnext;
pcurrnt->pnext = nullptr;
pcurrnt->value = i; // Give it a value
}
return phead;
}
/* Destroy the list */
struct link_node* DeInitlinknode(struct link_node* phead)
{
struct link_node* pcur = phead;
while(pcur->pnext != nullptr)
{
free(pcur);
std::cout<<"free:"<<pcur<<std::endl;
pcur = pcur->pnext;
}
free(pcur);
std::cout<<"free:"<<pcur<<std::endl;
}
/* Print linked list */
struct link_node* Printlinknode(struct link_node* phead)
{
struct link_node* pcur = phead;
for(pcur; pcur->pnext != nullptr; pcur = pcur->pnext)
{
std::cout<<pcur->value<<" ";
}
std::cout<<pcur->value<<std::endl;
}
/* 1. How to find the penultimate in a one-way linked list N Nodes The usual way is to traverse one side to get the length , Then traverse the second time , Get the corresponding element . The time consumption of this practice is the length of the linked list 2 times - N. Use the speed pointer , Combined with step size 1 2 3 4 5 7 8 9 */
struct link_node* ReserverFind(struct link_node* plinkhead,int npos)
{
// Pointer to the current
struct link_node* pleft = plinkhead;
struct link_node* pright = plinkhead;
// Traverse , Find the last one 2N
for(;pleft;pleft = pleft->pnext)
{
pright = pleft;
for(int i = 0; i < npos; i ++)
{
pright = pright->pnext;
}
if(pright == nullptr)
break;
}
return pleft;
}
/* 2. Reverse of linked list ( Reverse in place ) Need one prev While traversing the pointer , Become the front pointer ; Need a temporary pointer to store next; You need a current pointer to traverse */
struct link_node* Reverse_list(struct link_node* phead)
{
struct link_node* prev = nullptr;
struct link_node* pcurrnt = phead;
#if 1
while(pcurrnt)
{
struct link_node*pnode = pcurrnt->pnext;
pcurrnt->pnext = prev;
prev = pcurrnt;
pcurrnt = pnode;
}
#endif
return prev;
}
/* 3. Judge whether there are links in the linked list The slow pointer moves a node backward , The fast pointer moves two at a time Method 1 , Traverse , Check whether the pointer repeats Method 2 , Use the speed pointer , The pointer 1 Each move 1 individual , The pointer 2 Each move 2 individual , When there is a ring , The pointer 1 And a pointer 2 There's always a meeting */
bool hasCycle(struct link_node *head) {
struct link_node* slow = head;
struct link_node* fast = head;
// If there is no ring , Then the fast pointer goes all the way to the end , solve the problem ;
// If there are rings , Then two people will always meet
while((fast != NULL) && (fast->pnext != NULL)){
slow = slow->pnext;
fast = fast->pnext->pnext;
if(slow == fast){
return true;
}
}
return false;
}
/* 4. Determine whether the two linked lists have common nodes 1. If Len1 = len2 , Direct traversal comparison 2. If Len1 != len2, We should first let the long linked list from the header “ go ” len1 - len2 Step Once a node coincides , Then the length is the same */
int main(int argc,char* argv[])
{
struct link_node* phead = Initlinknode(10);
Printlinknode(phead);
// Look for the last 2 Nodes
std::cout<<ReserverFind(phead,2)->value<<std::endl;
// Look for the last 4 Nodes
std::cout<<ReserverFind(phead,4)->value<<std::endl;
// Look for the last 6 Nodes
std::cout<<ReserverFind(phead,6)->value<<std::endl;
// reverse
struct link_node* pnewhead = Reverse_list(phead);
Printlinknode(pnewhead);
getchar();
DeInitlinknode(pnewhead);
return 0;
}
边栏推荐
- QNX modify system time
- 图形管线(一)后处理阶段 alpha测试 模版测试 深度测试 混合
- The principle of Google interview questions is to analyze 12 table tennis balls, one of which is defective. Weigh it with a balance for 3 times to find it
- 【MUDUO】Poller抽象类
- Special topic of MIMO Radar (0) - General Chapter
- "Computing beast" Inspur nf5468a5 GPU server open trial free application
- Why choose AMD epyc for cloud solutions?
- 第二章关系数据库课后习题
- 数据库系统原理与应用教程(051)—— MySQL 查询(十三):DML 语句中使用查询
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
猜你喜欢

Point target simulation of SAR imaging (II) -- matlab simulation

Introduction to radar part vii 3 formation and processing of SAR image

GLIB-CRITICAL g_file_test:assertion ‘filename != null‘ failed

Method of entering mathematical formula into mark down document

Target segmentation for 10000 frames of video, with less than 1.4GB of video memory | eccv2022

接口测试-简单的接口自动化测试Demo

决策树详解

Light chain dissection / tree chain dissection

The current situation of the industry is disappointing. After working, I returned to UC Berkeley to study for a doctoral degree

Parameters of high-performance JVM
随机推荐
【 Visual Dispatching Software】 Shanghai Dow Ning apporte netronic download, Trial, tutoriel pour l'Organisation SMB
Matplotlib- implement common probability distributions
Dynamic planning daily practice (1)
Point target simulation of SAR imaging (II) -- matlab simulation
数据库系统原理与应用教程(042)—— MySQL 查询(四):使用通配符构造查询条件
PHP获取当前时间戳三位毫秒 - 毫秒时间戳
了解canvas
ModuleNotFoundError: No module named ‘setuptools_rust‘
Reference and output message types in ROS
Debug No5 basic lighting
The principle of Google interview questions is to analyze 12 table tennis balls, one of which is defective. Weigh it with a balance for 3 times to find it
C # make a simple browser
Google Play应用商店可能会删除应用权限概述 转而使用新的数据安全信息组合
知识图谱:基本概念
Power BI----综合应用
ROS2自学笔记:Gazebo物理仿真平台
prometheus+node-exporter+grafana 监控服务器系统资源
DeFi 永不消亡?
docker redis
深入解读 EVM 的生态帝国