单链表反转和插入

#include

struct node
{
int value = -1;
node* next_ptr = nullptr;
};

//创建结点

void create(int i, node** header_ptr) {
node* current = header_ptr;
//第一个结点
if (current == nullptr) {
node
ptr = new node;
ptr->value = i;
ptr->next_ptr = nullptr;
header_ptr = ptr;
}else {
while (current!= nullptr){
if (current->next_ptr == nullptr){
node
ptr = new node;
ptr->value = i;
ptr->next_ptr = nullptr;
current->next_ptr = ptr;
break;
}
current = current->next_ptr;
}
}
}

//反转链表
void rever_list(node** header) {
if (header == nullptr){
return;
}
node* pre = nullptr;
node* header_ptr = header;
while (header_ptr != nullptr){
node
temp_ptr = header_ptr->next_ptr;
header_ptr->next_ptr = pre;
pre = header_ptr;
header_ptr = temp_ptr;
}
*header = pre;
}

//插入链表(后插入法 )
void insert_list_back(node**header, int prev_value,int insert_value)
{
node* pre = header;
while (pre!= nullptr){
if (pre->value == prev_value){
node
temp_ptr = new node();
temp_ptr->value = insert_value;
if (temp_ptr->next_ptr != nullptr){
temp_ptr->next_ptr = pre->next_ptr;
pre->next_ptr = temp_ptr;
}else {
pre->next_ptr = temp_ptr;
}
break;
}
pre = pre->next_ptr;
}
}

//插入链表(前插入法)
void insert_list_fornt(node** header, int prev_value, int insert_value)
{
//前指针
node* pre = nullptr;
node* temp_header = header;
//后指针
node
next = temp_header->next_ptr;
while (temp_header != nullptr){
if (next->value ==prev_value){
//第一节点插入
if (pre == nullptr){
node* ptr = new node;
ptr->value = insert_value;
ptr->next_ptr = temp_header;
header = ptr;
}else {
node
ptr = new node;
ptr->value = insert_value;
ptr->next_ptr = temp_header;
pre->next_ptr = ptr;
//已经是二级指针了,否则会报错
// *header = pre;
}
break;
}
pre = temp_header;
temp_header = temp_header->next_ptr;
next = temp_header->next_ptr;
}
}

//链表删除

void delete_list(node** header, int value)
{
node* current_ptr = header;
node
pre = nullptr;
while (current_ptr != nullptr){
if (current_ptr->value == value){
//第一个节点删除
if (pre == nullptr){
node* ptr = current_ptr->next_ptr;
header = ptr;
}else {
node
ptr = current_ptr->next_ptr;
pre->next_ptr = ptr;
current_ptr->next_ptr = nullptr;
}
delete current_ptr;
current_ptr = nullptr;
break;
}
pre = current_ptr;
current_ptr = current_ptr->next_ptr;
}
}

int main()
{
node* header = new node();
header->value = 0;
header->next_ptr = nullptr;
node* ptr{ nullptr };
for (int i =1;i<6;++i){
ptr =create(i, header);
}
//插入链表(后)
//insert_list_back(&header,5,6);
//插入链表(前)
insert_list_fornt(&header, 2, 7);
int i = 100;
//反转链表
//rever_list(&header);

}

//说明:玩单链表很简单,传参只需要传二级指针即可,然后去理解每个节点的意思

//记住一定要多敲,否则成不了大神…

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/548260.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

liunx命令联系

1&#xff0c;liunx桌面和命令行之间的切换 init 3 init4 init5 2&#xff0c;liunx下用户的切换是su 用户 转载于:https://blog.51cto.com/lvjian118/1057319

测试boot库下I/O模型类型

#include #include #include <boost/asio.hpp> int main() { std::string output; #if defined(BOOST_ASIO_HAS_IOCP) output “iocp” ; #elif defined(BOOST_ASIO_HAS_EPOLL) output “epoll” ; #elif defined(BOOST_ASIO_HAS_KQUEUE) output “kqueue” ; #elif…

循环语句:For循环

Pascal中的for循环严格地建立在计数器基础上&#xff0c;循环每执行一次&#xff0c;计数器不是增加一个值就是减小一个值。 例子&#xff1a; vark,i: integer; beginK:0;for i:1 to 10 dok :ki;同样的for语句可以用正好相反的计数器来写&#xff1a; vark,i: integer; begink…

beast单元库的总结

1:凡是带有async_read 和async_write 开头都是阻塞的&#xff0c;因为在向socket底层读写缓冲区时&#xff0c;一定要读完指定的大小位置&#xff0c;因为底层写了一个for循环 2:如果要使用async_read_some开头的函数&#xff0c;一定要使用先读写包头&#xff0c;再读写包体的…

Ubuntu 找不到libc.so.6

2019独角兽企业重金招聘Python工程师标准>>> 在Ubuntu 上&#xff0c;其实这个库是存在的&#xff0c;只是地方换了&#xff0c;在"/lib/i386-linux-gnu/"下面&#xff0c;我们只需创建一个链接即可。使用下面的命令&#xff1a; rootubuntuJack:/lib/i3…

c++实现插入和冒泡排序

插入排序的最好的情况是O(n)&#xff0c;最坏的情况O(n^2),因此是稳定的 冒泡排序最好的情况是O(n)&#xff0c;最坏的情况O(n^2)因此是稳定的 //插入排序 void insert_sort(int* arr, int n) { int temp -1; for (int i1;i<n;i){ temp arr[i]; int j i - 1; //从后往前…

.net 动软代码生成器

http://www.maticsoft.com/codematic.aspx

c++实现二叉树操作

//前序 //用广度搜索 template void AVLTree::preOrder(AVLTreeNode* tree) const { /**********递归形式走/ //if (nullptr ! tree) //{ // cout << tree->key << " "; // preOrder(tree->left); // preOrder(tree->right); //} if (tree nu…

Centos下lnmp编译安装详细过程

整理下lnmp安装步骤&#xff1a; 相关软件用的lnmp一键安装的全包&#xff0c;懒得去到处找软件源 完整版&#xff1a;http://soft.vpser.net/lnmp/lnmp0.7-full.tar.gz(66.64MB) 1.系统安装必要软件 sudo -s LANGC yum -y install gcc gcc-c autoconf lib…

fstream下的读写操作

#include #include #include int main() { std::string path “D/ssss/reflection.hpp”; //这情况下&#xff0c;默认下是std::ios_base::in读入 trun:在打开时舍弃流的内容 std::ifstream open_file(path, std::ios_base::ate | std::ios_base::binary); std::size_t si…

「常微分方程」(阿諾爾德) Page 6 問題4 經過擴張相空間的每一點有且僅有一條積分曲線...

經過擴張相空間的每一點有且僅有一條積分曲線 證明:假若經過擴張相空間的某個點有兩條不同的積分曲線&#xff0c;則意味着經過相空間中的某點有兩條相曲線&#xff0c;這與常微分方程(阿諾爾德) 1.1節 問題3 經過相空間的每一點有且僅有一條相曲線 矛盾.转载于:https://www.c…

c++实现双向链表操作

#include struct node { node* pre{}; node* next{}; int data; }; //双链表创建结点 void create_node(node** header,int value) { node* current header; if (current nullptr){ node ptr new node; ptr->data value; header ptr; }else { while (current ! nullpt…

matlab学习:人脸识别之LBP (Local Binary Pattern)

1.算法简介 LBP是一种简单&#xff0c;有效的纹理分类的特征提取算法。LBP算子是由Ojala等人于1996年提出的&#xff0c;主要的论文是"Multiresolution gray-scale and rotation invariant texture classification with local binary patterns", pami, vol 24, no.7,…

利用栈解决深度搜索问题

#include #include struct Pos { int _row{}; int _col{}; Pos(int row, int col) :_row(row) , _col(col) { } Pos() { } }; std::stacks; bool CheckIsAccess(int* a, int row_size, int col_size, Pos cur) { //行坐标不合法 if (cur._row <0 || cur._row > row_siz…

组策略应用规则示例

组策略应用规则示例一、在AD中创建两个OU&#xff0c;上层为OU1&#xff0c;下层为OU2&#xff0c;在OU2中有一个用户USERA。1&#xff0c;在OU1中做组策略设置为从桌面删除回收站&#xff0c;OU2为禁止访问控制面板。因为策略没有冲突&#xff0c;这时USERA为OU1和OU2的策略累…

boost库下的deadline_timer和steady_timer 区别

1:steady_timer的expires_from_now函数参数必须使用std::chrono 2:deadline_timer的expires_from_now函数参数必须使用boost::posix_time 声明以下处之别人的整理中 3:boost::asio::deadline_timer使用的计量时间是系统时间&#xff0c;因此修改系统时间会影响deadline_time…

羅素悖論和正則公理

假如我们在ZF集合论里加入这么一条公理&#xff1a; 概括公理:设对于每一个对象$x$,我们都有一个依赖于$x$的性质$P(x)$,则存在一个集合$\{x|P(x)\mbox{成立}\}$.使得$$y\in\{x|P(x)\mbox{成立}\}\Leftrightarrow P(y)\mbox{成立}$$. 这看上去是一条很好的公理,在高中教科书中事…

openssl ssl_write 写错误

使用beast库中&#xff0c;调用async_read 异步写函数&#xff0c;会发生ssl_write错误&#xff0c;原因是openssl 限制了包大小&#xff0c;最大支持16KB的包&#xff0c;如果大于16KB的包&#xff0c;将会分成N个包.比如总包字节数为n,因此会被分成n/16 就等于发送对端的次数…

关于make_work_guard猜想

猜想&#xff1a;使用make_work_guard有可能是让io一直保活&#xff0c;不被当没有任务的时候&#xff0c;一直轮询&#xff0c;除非自己调用stop函数。欢迎大家留言&#xff0c;这只是我的猜想. #include<iostream> #include<boost/asio.hpp> #include <boost…

大流量 网站

引用&#xff1a;http://www.admin10000.com/document/948.html 动态应用&#xff0c;是相对于网站静态内容而言&#xff0c; 是指以c/c、php、Java、perl、.net等 服务器端语言开发的网络应用软件&#xff0c;比如论坛、网络相册、交友、BLOG等常见应用。动态应用系统通 常与数…