用C++实现单链表的创建、逆置和输出 的两种方法

http://blog.csdn.net/lfeng_coding/article/details/47300563

题目描述:在已知单链表头节点的情况下,设计算法逆置单链表并输出

方法一:采用首先将头节点指向空,让其变为尾节点,然后利用中间节点 p、q 将其后的节点一个接一个改为指向前面的节点



/****************************

*作者:刘峰

* 时间:2015\8\5

* 环境:VS2013

* 功能:实现创建一个节点可控的单链,并逆置输出

****************************/


[cpp] view plain copy
  1. #include "stdafx.h"  
  2.   
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. struct List  
  7. {  
  8.     int num;  
  9.     List *next;  
  10. };  
  11.   
  12. List *createList(int n)       //创建含有n个节点的单链表  
  13. {  
  14.     List *head, *p, *q;  
  15.     q=head = NULL;   //初始化表头和中间指针  
  16.     int i;  
  17.     for (i = n; i > 0; --i)  
  18.     {  
  19.         p = new List;     //申请空间,创建第一个节点  
  20.         cin >> p->num;      //往节点中存入数据信息  
  21.         if (head == NULL)  
  22.         {  
  23.             head = p;  
  24.         }  
  25.         else  
  26.         {  
  27.             q->next = p;  
  28.         }  
  29.         q = p;  
  30.     }  
  31.     q->next = NULL;  
  32.     return head;  
  33. }  
  34.   
  35. List *ReverseList(List *head)          //逆置单链表  
  36. {  
  37.     List *p, *r;       //定义两个中间节点,用于顺移逆置链表节点  
  38.     if (head->next == NULL)  
  39.         return head;  
  40.     p = head;          //获取头节点地址  
  41.     r = p->next;       //获取链表第二个节点地址  
  42.     p->next = NULL;    //头节点变为尾节点,原链表表头指向空  
  43.     while (r)  
  44.     {  
  45.         p = r;  
  46.         r = r->next;  
  47.         p ->next = head;   //使第二个节点指向原先的头节点  
  48.         head = p;          //使第二个节点变为头节点,用于循环逆置  
  49.     }  
  50.     return head;  
  51. }  
  52.   
  53. void print(List *head)        //输出逆置后的单链表  
  54. {  
  55.     List *p;  
  56.     p = head;  
  57.     while (p)  
  58.     {  
  59.         cout<<p->num;  
  60.         p = p->next;  
  61.         cout << " ";  
  62.     }  
  63.     cout << endl;  
  64. }  
  65. int _tmain(int argc, _TCHAR* argv[])  
  66. {  
  67.     List *p, *q;  
  68.     cout << "请输入单链表的节点个数:";  
  69.     int n;  
  70.     cin >> n;  
  71.     cout << endl;  
  72.     cout << "创建一个节点为" << n << "的单链表" << endl;  
  73.     p = createList(n);  
  74.     cout << endl;  
  75.     cout << "这步为程序逆置单链表" << endl;  
  76.     q = ReverseList(p);  
  77.     cout << endl;  
  78.     cout << "打印逆置后的单链表" << endl;  
  79.     print(q);  
  80.     cout << endl;  
  81.     return 0;  
  82. }  



方法二:用p,q指向单链表中相邻的两节点,将r指向q的下一个结点,然后同步后移。当q=NULL时,表示指向原单链表的尾结点,将p赋值为头节点 head 即可。


逆置函数代码如下(其他部分不变):

List *ReverseList(List *head)
{
List *p, *q, *r;
p = head;
if (p->next == NULL)
return head;
q = p->next;
while (q != NULL)     //q为空,说明p为最后一个节点,所以结束while后将q赋值给head,作为逆置后的表头
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = NULL;   //将原head变为逆置后链表的表尾
head = p;            //逆置后新的表头
return head;
}



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

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

相关文章

两个栈实现一个队列

利用两个栈实现一个队列思路是这样的. 首先这个队列包含两个栈, 然后一个栈用来入队列, 一个栈用来出队列 typedef struct QueBy2Stack {SeqStack input;SeqStack output; }QueBy2Stack; 1. 初始化 void QueBy2StackInit(QueBy2Stack* stack) {if(stack NULL){return;//非法…

HDU 5934:Boom——强连通分量+缩点

【题目描述】 There are N bombs needing exploding.Each bomb has three attributes: exploding radius ri, position (xi,yi) and lighting-cost ci which means you need to pay ci cost making it explode.If a un-lighting bomb is in or on the border the exploding ar…

Linux--线程死锁

http://blog.csdn.net/gebushuaidanhenhuai/article/details/73799824 线程为什会死锁&#xff1f;&#xff1f;“锁”又是什么东西&#xff1f;我们这篇博客主要讲一下为什么要给线程加锁&#xff0c;为什么会出现线程死锁&#xff0c;线程死锁怎么解决。 互斥锁 在我的上篇博…

两个队列实现一个栈

用两个队列实现一个栈的原理是这样的. 规定两个队列, 必须有一个队列是非空, 一个队列是空.每次入栈时必须往非空队列中入, 而每次出栈时, 必须将非空队列里的元素装到空队列中, 直到非空队列中只有一个元素时, 此时就将剩下的这个元素出栈即可. 而取栈顶元素时, 和出栈一样, 先…

POJ-1144 Network——Trajan+割点

【题目描述】 A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect together tw…

Linux--生产者与消费者

http://blog.csdn.net/gebushuaidanhenhuai/article/details/74011636 基本概念 提到生产者和消费者&#xff0c;我们最有可能想到的是商店卖东西&#xff0c;顾客在货架上(缓冲区&#xff09;买东西。 生产者消费者问题&#xff0c;其实是一个多线程同步问题的经典案例。该问…

进程的挂起以及可重入函数

相关接口     pause 函数用于将进程挂起. 如果信号的处理动作是终止进程, 则进程终止, pause 函数没有返回值; 如果信号的处理动作是忽略, 则进程被挂起, pause函数不返回, 如果信号的处理动作是捕捉, 则调用信号处理动作之后pause 返回 -1.来看一段代码 #include<s…

POJ1236Network of Schools——强连通分量缩点建图

【题目描述】 A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distri…

C——通过调用函数分配内存

http://blog.csdn.net/u012627502/article/details/3579724 1&#xff09;以返回值方式返回&#xff1a;把动态分配的存储位置地址&#xff0c;赋值给指针类型返回值&#xff08;不同于被调用函数的自动变量地址&#xff09; 2&#xff09;以形参形式返回&#xff1a;二级指针类…

gdb调试多进程程序

1.gdb下调试多进程程序只需要以下几条命令即可              除此之外还可以查看正在调试的进程 info inferiors, 同时也可以将当前正在调试的进程切换到另外一个进程中让其取运行     2.代码调试演示 #include<stdio.h> #include<stdlib.h> #…

BZOJ1123-BLO——强连通分量求割点+计数

【题目描述】 Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通。Input 输入n<100000 m<500000及m条边Output 输出n个数&#xff0c;代表如果把第i个点去掉&#xff0c;将有多少对点不能互通。Sample Input 5…

关于memcpy和memmove两函数的区别

http://blog.csdn.net/caowei840701/article/details/8491836 [cpp] view plaincopy <p> 关于memcpy和memmove两个c标准库函数&#xff0c;其功能都是将一块内存区域中的指定大小内容复制到目标内存中&#xff0c;在翻阅c标准库实现的源代码我们发现他们是有区别的。&…

判断字符串出栈合法性

先来看说一下思路 接下来就是写代码了 int StackOrder(SeqStack* stack, char* input, char* output, int size_input, int size_output) {if(stack NULL || input NULL || output NULL){return 0;}int i_input 0;int j_output 0;SeqStackType value;for(; j_output <…

CodeForces - 1200C——小模拟

【题目描述】 Amugae is in a very large round corridor. The corridor consists of two areas. The inner area is equally divided by n sectors, and the outer area is equally divided by m sectors. A wall exists between each pair of sectors of same area (inner o…

1 单例模式

达内 闵大神 //饿汉单例模式 #include <iostream> using namespace std;class Singleton { public:static Singleton& getInstance(){return s_instance;} private:Singleton(){}Singleton(const Singleton& that){}static Singleton s_instance;//静态成员变量 …

共享栈

1.定义 所谓共享栈就是利用一个数组实现两个栈. 先来看一下共享栈的数据结构 typedef struct SharedStack {int top1;int top2;SeqStackType* data; }SharedStack; 2. 初始化 void SharedStackInit(SharedStack* stack) {if(stack NULL){return;//非法输入}stack -> top…

BZOJ1018 | SHOI2008-堵塞的交通traffic——线段树维护区间连通性+细节

【题目描述】 BZOJ1018 | SHOI2008-堵塞的交通traffic 有一天&#xff0c;由于某种穿越现象作用&#xff0c;你来到了传说中的小人国。小人国的布局非常奇特&#xff0c;整个国家的交通系统可 以被看成是一个2行C列的矩形网格&#xff0c;网格上的每个点代表一个城市&#xff0…

C++ 函数隐藏

C该函数隐藏 只有基类成员函数的定义已声明virtualkeyword&#xff0c;当在派生类中的时间&#xff0c;以支付功能实现&#xff0c;virtualkeyword可以从时间被添加以增加。它不影响多状态。 easy混淆视听&#xff0c;掩盖&#xff1a; &#xff0c;规则例如以下&#xff1a; …

迷宫求解(递归)

首先来看一下迷宫简易图                                  我们用 0 来表示该位置是墙, 用 1 来表示该位置是路. 所以, 我们在处理迷宫问题的时候可以将其看成一个二维数组即可, 而对应的每一条路我们可以用坐标的形式将其表示, 所以还需要…

CodeForces - 786C——二分+模拟?

【题目描述】 Rick and Morty want to find MR. PBH and they cant do it alone. So they need of Mr. Meeseeks. They Have generated n Mr. Meeseeks, standing in a line numbered from 1 to n. Each of them has his own color. i-th Mr. Meeseeks color is ai.Rick and M…