递归,迭代,堆栈三种方式实现单链表反转(C++)
#author by changingivan
# 2016.04.12
#include <iostream>
#include <stack>
using namespace std; struct Node
{
int val;
Node *next;
}; Node * creat_link()
{
Node *head=NULL;
Node *current=NULL;
Node *tmp=NULL;
int val=0;
int num_node=10;
for(int i=0;i<num_node;i++)
{
val=i+1;
if(i==0)
{
current=(Node*)new(Node);
current->val=val;
current->next=NULL;
head=current;
}
else
{
tmp=(Node*)new(Node);
tmp->val=val;
tmp->next=NULL;
current->next=tmp;
current=current->next; }
}
return head;
} void show_display_link(Node * head)
{
Node * current=head;
while(current!=NULL)
{
cout<<current->val<<endl;
current=current->next; }
cout<<"finish!"<<endl;
} Node * reverse_iteration_version(Node * head)
{
Node * current=NULL;
Node * next_node=NULL;
Node * pre=NULL;
current=head;
pre=head;
next_node=current->next;
current->next=NULL;
while(next_node!=NULL)
{
pre=current;
current=next_node;
next_node=next_node->next;
current->next=pre;
}
head=current;
return head;
} Node * reverse_stack_version(Node * head)
{
stack <Node * > stack_for_link;
Node * current,* tmp,*new_head;
current=head;
while(current!=NULL)
{
stack_for_link.push(current);
current=current->next;
}
current=stack_for_link.top();
stack_for_link.pop();
new_head=current;
while(!stack_for_link.empty())
{
tmp=stack_for_link.top();
stack_for_link.pop();
cout<<tmp->val<<endl;
current->next=tmp;
current=tmp;
}
tmp->next=NULL;
return new_head; } Node * reverse_recursion_version(Node * head)
{
if(head->next==NULL)
{
return head;
}
Node *tmp,*new_head;
tmp=head->next;
new_head=reverse_recursion_version(tmp);
tmp->next=head;
head->next=NULL;
return new_head;
}
int main()
{
Node * head=NULL;
cout << "this is normal order" << endl;
head=creat_link();
show_display_link(head);
cout <<"this is reverse order"<<endl;
//head=reverse_iteration_version(head);
//head=reverse_iteration_version(head);
head=reverse_recursion_version(head);
show_display_link(head);
return 0;
}
递归,迭代,堆栈三种方式实现单链表反转(C++)的更多相关文章
- 递归迭代vector三种方法实现二路归并排序
https://mp.csdn.net/mdeditor/84933084# 附链接
- js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式
js replace 全局替换 js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...
- Django中三种方式写form表单
除了在html中自己手写form表单外,django还可以通过 继承django.forms.Form 或django.forms.ModelForm两个类来自动生成form表单,下面依次利用三种方式 ...
- python 获取表单的三种方式
条件:urls.py文件中配置好url的访问路径.models.py文件中有Business表. 在views.py文件中实现的三种方式: from app01 improt models def b ...
- Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式
Linux就这个范儿 第15章 七种武器 linux 同步IO: sync.fsync与fdatasync Linux中的内存大页面huge page/large page David Cut ...
- Kubernetes 对象管理的三种方式
Kubernetes 中文文档 1. Kubernetes 对象管理的三种方式对比 Kubernetes 中的对象管理方式,根据对象配置信息的位置不同可以分为两大类: 命令式:对象的参数通过命令指定 ...
- 架构设计哲学【三种方式:支持DevOps的原则】
三种方式:支持DevOps的原则 2012年8月22日作者Gene Kim 45条评论 这篇文章是杨波老师分享的一篇文章:这几年对他架构影响最深的一篇文章.主要描述是关于DevOps的,但对系统架构同 ...
- 使用javascript实现在页面打印的效果的三种方式
<div id="console"></div> <script type="text/javascript"> var c ...
- 前端js,css文件合并三种方式,bat命令
前端js,css文件合并三种方式,bat命令 前端js文件该如何合并三个方式如下:1. 一个大文件,所有js合并成一个大文件,所有页面都引用它.2. 各个页面大文件,各自页面合并生成自己所需js的大文 ...
随机推荐
- QS之shell script
1 Invoke Mdoelsim In order to open Modelsim automatically, it is better to use a shell script to inv ...
- SQL关于触发器及存储过程的创建
使用T-SQL语句来创建触发器 基本语句如下﹕ create trigger trigger_name on {table_name | view_name} {for | After | Ins ...
- Flask框架 之第一个Flask程序
from flask import Flask # 创建flask应用对象 # __name__ 代表当前模块名称 # flask以当前目录为总目录,static目录为静态目录,templates为模 ...
- Xamarin绑定ios静态库
以下是官方的步骤介绍,我就不再一步步解释了 https://docs.microsoft.com/zh-cn/xamarin/ios/platform/binding-objective-c/walk ...
- gitlab恢复、迁移
文件说明 安装包:gitlab-ce_8.11.5-ce.0_amd64.deb 备份的数据:533751277_gitlab_backup.tar 系统:Ubuntu 16.04.4 LTS \n ...
- Loadrunner12 安装与卸载
卸载:http://www.51testing.com/html/21/303921-216608.html 安装包下载: loadrunner12安装包下载:链接:https://pan.baidu ...
- 关闭的连接: next
1.最近做了一个项目,扫描读取了第三方数据库的数据,结果本来在公司测试没有问题的程序在客户那边一直报如下错误: java.sql.SQLException: 关闭的连接: next 代码如下: //第 ...
- Xmind的使用
Xmind是用来学习整理思维的工具
- Codeforces 280C - Game on Tree
传送门:280C - Game on Tree 不知道期望是啥的请自行Baidu或Google,(溜了 题目大意,有一棵有根树,每次随机选择一个节点,将这个节点和它的子树删除,问将整棵树删除的期望次数 ...
- Python基础(八)装饰器
今天我们来介绍一下可以提升python代码逼格的东西——装饰器.在学习装饰器之前我们先来复习一下函数的几个小点,方便更好的理解装饰器的含义. 一.知识点复习 1, 在函数中f1和f1()有什么不同,f ...