链表的建立、输出、非递归反转、递归反转
来源:互联网 发布:我们来了网络更新时间 编辑:程序博客网 时间:2024/05/16 19:53
链表的建立、输出、非递归反转、递归反转
代码如下:
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <string.h> #include <fstream> #include <map> #include <iomanip> #include <cmath>#include <set>#include <stdio.h>#include <queue>using namespace std; const int MAX = 0x7FFFFFFF; const int MIN = 0x80000000; typedef struct node{int value;struct node *next;}Node;//功能:创建链表//输入:头结点//返回值:-1代表创建失败,1代表创建成功int createList(Node** head){int a, count = 0;Node *cur, *pre;//0为终止符while(cin >> a && a != 0){if(count == 0){*head = (Node*)malloc(sizeof(Node));if(*head == NULL)return -1;(*head)->value = a;(*head)->next = NULL;cur = pre = *head;}else{cur = (Node*)malloc(sizeof(Node));if(cur == NULL)return -1;cur->value = a;cur->next = NULL;pre->next = cur;pre = cur;}count++;}return 1;}//功能:打印链表//输入:链表头结点//返回值: -1代表链表为空,1代表打印成功int printList(Node* head){if(head == NULL)return -1;while(head != NULL){cout << head->value << " ";head = head->next;}cout << endl;return 1;}//非递归反转链表//输入:二级指针的头结点//返回值:-1表示链表为空,1代表反转成功//参数中的头结点为二级指针,函数完成之后,head指向反转后的结点int reverseListNotRecursion(Node** head){Node* cur, *pre = *head;if(*head == NULL)return -1;while(pre->next != NULL){cur = pre->next;pre->next = cur->next;cur->next = *head;*head = cur;}return 1;}//递归反转链表//输入:头结点,待反转结点//返回值:当前反转的结点,已插入尾部//返回为参数中的引用,即反转后的头结点Node* reverseListRecursion(Node** head, Node* p){Node* remain;if(p == NULL || p->next == NULL){*head = p;return p;//返回最后一个结点}remain = reverseListRecursion(head, p->next);remain->next = p;p->next = NULL;return p;}int main(){Node *head = NULL;//建立链表//假设创建链表中数依次为:1,2,3,4,5createList(&head);//输出创建之后的链表//输出1 2 3 4 5 printList(head);//非递归反转链表reverseListNotRecursion(&head);//输出5 4 3 2 1printList(head);//递归反转链表reverseListRecursion(&head, head);//输出1 2 3 4 5 printList(head);system("pause");return 0;}
- 链表的建立、输出、非递归反转、递归反转
- 链表的建立与反转(递归反转和非递归反转)
- 链表反转的实现(递归反转链表,非递归反转链表)
- 反转链表[递归,非递归]
- 反转链表(递归与非递归)
- 链表反转--递归与非递归
- 链表反转的递归和非递归算法
- C++递归与非递归实现链表的反转
- 链表反转的递归和非递归实现方式
- 链表反转的递归和非递归实现方式
- 反转链表(非递归)
- 链表的递归反转
- 递归 反转链表
- 递归反转链表
- 使用递归和非递归方式反转单向链表
- 反转链表(递归和非递归实现)
- 递归和非递归实现链表反转
- 边看边写(链表反转(递归和非递归))
- vb.net写的断点续传下载类
- RAID
- ocp-047-18 create view
- CSS3字体
- 一个简单的Web服务器(Web服务器如何工作)
- 链表的建立、输出、非递归反转、递归反转
- 《UNIX环境高级编程》笔记--读和写流
- git 相关
- 黑马程序员--java异常学习笔记
- MFC界面编程: 为 Arp协议获得本地局域网内在线主机MAC地址的程序 编写 界面
- poj 2528 坐标离散化+线段树
- TCP和UDP和Socket
- lightoj 1028 - Trailing Zeroes (I) 欧拉函数
- ORACLE 分区表