(数据结构)线性表_单链表反转 _模仿
来源:互联网 发布:改图宝软件下载 编辑:程序博客网 时间:2024/05/16 11:35
方法1:
#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;}
0 0
- (数据结构)线性表_单链表反转 _模仿
- (数据结构)线性表_多项式相加1_模仿
- 数据结构-线性表_单链表
- 数据结构-线性表_单链表
- 数据结构_线性表
- 数据结构_线性表
- 数据结构_线性表
- 数据结构_线性表
- 数据结构_线性结构_线性表
- 数据结构_学习之_ 线性表
- 数据结构_线性链表
- 数据结构_线性表实现
- 数据结构02_线性表
- Java 数据结构_线性表
- C++_008_数据结构_线性表_普通线性表
- 数据结构_单链表的建立与反转
- 数据结构_线性表_链式存储_单链表 的基本操作
- 数据结构:线性表的链式存储结构_单链表
- 兼职开发怎样才能变成技术合伙人?
- TP-link 无线路由器WDS设置方法图解_无线桥接设置
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- java语法基础
- poj1860_最短路bellman Ford算法应用
- (数据结构)线性表_单链表反转 _模仿
- OpenCV常用函数札记
- oracle常用sql语句(不断更新中)
- RTSP协议解析
- Android Parcelable接口的使用
- 题目1085:求root(N, k)
- java.net.ConnectException: Connection refused
- Leetcode: First Missing Positive
- 我的2013年大记事