(数据结构)线性表_单链表反转 _模仿
2014-01-05 19:37
274 查看
方法1:
方法2:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef int ElemType; typedef struct Node *List; typedef struct Node *Position; typedef struct Node Node; struct Node { ElemType Element; List next; }; int length_List(List L) //L带头结点 { int len; Position temp; temp=L; while(temp->next!=NULL) { temp=temp->next; len++; } return len; } List ReverseList(List L) //反转单链表函数,L带头结点 { if(L->next->next==NULL||L->next==NULL) //空链表或者只有一个结点 return L; List Result ,temp; //使用这两个结构体变量,起保护L作用 int len ,i; Result=(List)malloc(sizeof(Node)); Result->next=NULL; len=length_List(L); for(i=0;i<len;i++) { temp=(List)malloc(sizeof(Node)); temp->Element=L->next->Element; //临时链表结点,用来存放L链表中的元素 temp->next=Result->next; Result->next=temp; L=L->next; } return Result; }
方法2:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef int ElemType; typedef struct Node *List; typedef struct Node *Position; typedef struct Node Node; struct Node { ElemType Element; List next; }; List ReverseList(List L1) //反转单链表函数,L1带头结点 { if(L1->next==NULL||L1->next->next==NULL) return L1; Position PreviousPos ,CurrentPos ,NextPos; List L; L=L1->next; //L无头结点,为处理方便 PreviousPos=NULL; CurrentPos=L; NextPos=L->next; while(NextPos!=NULL) { CurrentPos->next=PreviousPos; PreviousPos=CurrentPos; CurrentPos=NextPos; NextPos=NextPos->next; } CurrentPos->next=PreviousPos; return CurrentPos; }
相关文章推荐
- 【算法和数据结构】_6_线性结构_双链表
- 02-线性结构1 两个有序链表序列的合并——中国大学MOOC-陈越、何钦铭-数据结构-2017秋
- 数据结构---线性表之双链表(C语言)
- 数据结构之线性链表的算法
- 数据结构专题——线性表之双链表及其Java实现
- 数据结构【线性表(二)链表】项目一--建立单链表
- PHP数据结构之三 线性表中的单链表的PHP实现
- 数据结构【线性表(二)链表】项目之循环双链表应用
- 数据结构学习笔记 --- 线性表 (双向链表、循环链表)
- 线性表 之 链表反转
- Java数据结构-线性表之单链表应用-单链表合并
- 数据结构-链表创建,删除,插入,反转,连接
- 数据结构练习(17)反转链表
- 基本数据结构之线性表-链表
- 数据结构类型定义及基本操作汇总(一)--线性表,单链表,栈和队列
- 数据结构【线性表(二)链表】项目之单链表:逆置
- PAT线性结构_一元多项式求导、按给定步长反转链表、出栈序列存在性判断
- 数据结构复习1——链表反转
- 数据结构:线性表之静态链表
- 数据结构(二):线性表的使用原则以及链表的应用-稀疏矩阵的三元组表示