如何实现将一个链表逆序?
2016-04-16 15:41
501 查看
如何实现将一个链表逆序?
2008-07-04 22:19:14| 分类: 程序相关|举报|字号 订阅下载LOFTER我的照片书 |
基本思想:
增加一个辅助指针,以P结点为例 p—>q—>r,我们只需r, q调换下位置就可以了
p=head;
q=p->next; //
while(q!=NULL)
{
temp=q->next; //r
q->next=p; //q—>r 变成了q—>p
p=q; //指针后移
q=temp;
}
这样增加个辅助的指针就行乐
ok 通过编译的代码:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
typedef struct List{
int data;
struct List *next;
}List;
List *list_create(void)
{
struct List *head,*tail,*p;
int e;
head=(List *)malloc(sizeof(List));
tail=head;
printf("\nList Create,input numbers(end of 0):");
scanf("%d",&e);
while(e){
p=(List *)malloc(sizeof(List));
p->data=e;
tail->next=p;
tail=p;
scanf("%d",&e);}
tail->next=NULL;
return head;
}
List *list_reverse(List *head)
{
List *p,*q,*r;
p=head;
q=p->next;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=NULL;
head=p;
return head;
}
void main(void)
{
struct List *head,*p;
int d;
head=list_create();
printf("\n");
for(p=head->next;p;p=p->next)
printf("--%d--",p->data);
head=list_reverse(head);
printf("\n");
for(p=head;p->next;p=p->next)
printf("--%d--",p->data);
}
进阶:
题目出处:http://blog.rainbud.net/article/entry20050928-014721
给定一单链表的表头指针和指向其中一个节点的指针,要求以该指针为头将原链表逆序排列,例如:
1. N1->N2->N3->N4->N5->NULL pHEAD = N1,pSTART = N3,返回N3->N2->N1->N5->N4->NULL
2. N1->N2->N3->N4->N5->NULL pHEAD = N1,pSTART = N5,返回N5->N4->N3->N2->N1->NULL
3. N1->N2->N3->N4->N5->NULL pHEAD = N1,pSTART = N1,返回N1->N5->N4->N3->N2->NULL
不允许额外分配存储空间,不允许递归,可以使用临时变量。
思路:先将整个链表逆序,然后再从pSTART后面断开,将那部分接到pHead上。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef struct Node
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node* next;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}Node, *PNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PNode pHead = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PNode ListReverse(PNode pHead, PNode pStart)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PNode pNode = NULL,
pCur = pHead, pNext = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(pCur != NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pNext = pCur->next;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pCur->next = pNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pNode = pCur;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pCur = pNext;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pCur = pNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(pCur == pStart)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return pNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(pCur->next != pStart)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pCur = pCur->next;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pCur->next = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pHead->next = pNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return pStart;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- sscanf用法简析
- 使用PowerDesigner进行代码生成
- 网络流二十四题之二 —— 太空飞行计划问题(SHUT)
- c++搜索树判断
- Android进阶——Handler的实际应用与多线程和界面的四种交互方式
- Jersey(1.19.1) - Building Responses
- android中的异步任务-----------AsyncTask的使用
- struts2开发5--从客户端批量提交数据到服务器端
- linux之学习live555视频笔记
- POJ 1041 John's trip(欧拉回路)
- 理解 Linux 的硬链接与软链接
- Linux 顶层目录结构
- 柔性数组
- leetcode 67. Add Binary
- NFS
- OK6410开发板资源介绍---嵌入式回归第一篇
- LeetCode——027
- JAVA技术开发规范(3)——JASMP平台技术架构
- ngui中scrollview的使用
- hdu 1823 Luck and Love 【二维线段树】