#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++)的更多相关文章

  1. 递归迭代vector三种方法实现二路归并排序

    https://mp.csdn.net/mdeditor/84933084# 附链接

  2. js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式

    js replace 全局替换   js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...

  3. Django中三种方式写form表单

    除了在html中自己手写form表单外,django还可以通过 继承django.forms.Form 或django.forms.ModelForm两个类来自动生成form表单,下面依次利用三种方式 ...

  4. python 获取表单的三种方式

    条件:urls.py文件中配置好url的访问路径.models.py文件中有Business表. 在views.py文件中实现的三种方式: from app01 improt models def b ...

  5. 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 ...

  6. Kubernetes 对象管理的三种方式

    Kubernetes 中文文档 1. Kubernetes 对象管理的三种方式对比 Kubernetes 中的对象管理方式,根据对象配置信息的位置不同可以分为两大类: 命令式:对象的参数通过命令指定 ...

  7. 架构设计哲学【三种方式:支持DevOps的原则】

    三种方式:支持DevOps的原则 2012年8月22日作者Gene Kim 45条评论 这篇文章是杨波老师分享的一篇文章:这几年对他架构影响最深的一篇文章.主要描述是关于DevOps的,但对系统架构同 ...

  8. 使用javascript实现在页面打印的效果的三种方式

    <div id="console"></div> <script type="text/javascript"> var c ...

  9. 前端js,css文件合并三种方式,bat命令

    前端js,css文件合并三种方式,bat命令 前端js文件该如何合并三个方式如下:1. 一个大文件,所有js合并成一个大文件,所有页面都引用它.2. 各个页面大文件,各自页面合并生成自己所需js的大文 ...

随机推荐

  1. glassfish中新建数据源(创建数据库连接池)

    1.浏览器输入:http://localhost:4848 登录glassfish域管理控制台,默认的用户名和密码是amin和adminadmin.(也可以通过NetBeans的服务选项卡--服务器- ...

  2. Jmeter常用的逻辑控制器

    一.ForEach控制器 作用:ForEach Controlle一般和用户自定义变量(User Defined Variables)一起使用,其在用户自定义变量中读取一系列相关的变量.每一个线程下执 ...

  3. vue组件---插槽

    (1)插槽内容 Vue 实现了一套内容分发的 API,这套 API 的设计灵感源自 Web Components 规范草案,将 <slot> 元素作为承载分发内容的出口. 在父级组件里可以 ...

  4. jquery onclick 问题

    var str = ''; for(var i = 0;i<data.list.length;i++){ str += "<tr><td>" + (i ...

  5. JAVA基础——集合浅析

    Java  集合      数组是一种很常见的数据结构,开始接触编程的时候多数程序都和数组相关.刚开始接触Java时也是一直使用数组写一些程序,后来越来越觉得数组这东西没法满足需求了,这时一位“前辈” ...

  6. Java:冒泡排序 | 二分查找

    2018-10-29 20:16:46 冒泡排序 例子(对数字排序): 假设有这样一组数字:32, 8, 128, 2, 64 现在对其进行冒泡排序(*表示下次比较的开始数字): 32>8? t ...

  7. linux hexdump-显示文件十六进制格式

    博主推荐:获取更多 linux文件内容查看命令 收藏:linux命令大全 hexdump命令一般用来查看“二进制”文件的十六进制编码,但实际上它能查看任何文件,而不只限于二进制文件. 语法 hexdu ...

  8. C语言结构体用法

    结构体的定义: 方法一: struct student { char name[10]; int age; int number; }; struct student stu1; 方法二: struc ...

  9. spring 学习(二)

    public interface BeanPostProcessor { @Nullable default Object postProcessBeforeInitialization(Object ...

  10. Java MyBatis 插入数据库返回主键--insertSelective这样就不用每次到数据库里面查询了

    insertSelective---Java MyBatis 插入数据库返回主键--insertSelective这样就不用每次到数据库里面查询了 https://www.cnblogs.com/xi ...