#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. 最容易理解的HMM文章

    wiki上一个比较好的HMM例子 分类 隐马尔科夫模型 HMM(隐马尔科夫模型)是自然语言处理中的一个基本模型,用途比较广泛,如汉语分词.词性标注及语音识别等,在NLP中占有很重要的地位.网上关于HM ...

  2. HDU_6017_Girls love 233_(dp)(记忆化搜索)

    Girls Love 233  Accepts: 30  Submissions: 218  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: ...

  3. Handling unhandled exceptions and signals

    there are two ways to catch otherwise uncaught conditions that will lead to a crash: Use the functio ...

  4. vi 命令学习(二)

    [选中文本(可视模式)] v 可视模式 从光标位置开始按正常模式选择文本 V 可视行模式 选中光标经过的完整行 ctrl + v 可视块模式 垂直方向选中文本 [ 撤销和恢复撤销] u undo 撤销 ...

  5. java虚拟机(九)--常用jvm参数

    1.-Xms20M: 表示设置JVM启动内存的最小值为20M,必须以M为单位 2.-Xmx20M: 表示设置JVM启动内存的最大值为20M,必须以M为单位.将-Xmx和-Xms设置为一样可以避免JVM ...

  6. Openstack manila的一些命令

    (本文是测试环境进行的操作:) 1.查看一些信息: [root@openstackcontroller ~]# manila type-list [root@openstackcontroller ~ ...

  7. 登录deepin 15.9后不显示任务栏,无法操作

    一直觉得在Linux下编程很酷,所以决定装个Deepin试试,安装很顺利,然后搭建了开发环境,写了一个简单程序,觉得挺不错的. 哪知第二天一开机,登录后找不到任务栏了,做不了啥操作,走接傻眼了,直觉以 ...

  8. linux命令 info

    info命令是Linux下info格式的帮助指令. 就内容来说,info页面比man page编写得要更好.更容易理解,也更友好,但man page使用起来确实要更容易得多.一个man page只有一 ...

  9. IE 浏览器证书错误常见问题解答

    有时,你将看到下表的错误消息,比如,此网站的安全证书已经被吊销等等,说明网站的安全证书有问题. 站点的证书允许 Internet Explorer 与站点建立安全连接. SSL证书出现错误有时是证书本 ...

  10. POJ 3270 置换群问题

    题目大意是: 每头牛都有一个对应的值a[i],现在给定一个初始的牛的序列,希望通过两两交换,能够使这些牛按值升序排列,每次交换都会耗费一个 a[i]+a[j] 希望耗费最小,求出这个最小耗费 个人觉得 ...