【数据结构复习】链表的倒置(头插法倒置)
2023-09-14 09:03:44 时间
#include <bits/stdc++.h> using namespace std; typedef int ElemType; struct LNode{ ElemType data; LNode *next; }; LNode *head,*tail; void init(){ head = (LNode*)malloc(sizeof(LNode)); head->next = NULL; tail = head; } void input_data(){ int x; cin >> x; while (x!=-1){ LNode *temp = (LNode*)malloc(sizeof(LNode)); temp->data = x; temp->next = NULL; tail->next = temp; tail = temp; cin >> x; } } //关键函数 void _reverse(){ LNode *p = head->next; head->next = NULL; while (p){ LNode *temp = p->next;//记录下当前遍历到的这个节点的下一个 p->next = head->next;//这个几点的下一个节点接在头结点后面的那个节点 head->next = p;//头结点的后一个节点指向该节点,从而完成插入过程 p = temp; } } void print(){ LNode *temp = head->next; while (temp){ cout<<temp->data<<" "; temp = temp->next; } } int main(){ init(); //freopen("D://rush.txt","r",stdin); input_data(); _reverse(); print(); fclose(stdin); return 0; }
相关文章
- 链表数据结构(C/C++语言实现)
- 链表实现快排和归并
- 循环链表结构
- Java实现 LeetCode 83 删除排序链表中的重复元素
- 数据结构与算法--链表
- linux内核数据结构之链表
- 【链表】LeetCode 148. 排序链表【中等】
- (剑指Offer)面试题13:在O(1)时间内删除链表结点
- 单向链表翻转
- 【数据结构笔记09】数据结构之稀疏矩阵的三元组和十字链表法具体代码实现(C语言)
- 数据结构之链表创建一元多项式,求一元多项式之和
- 19. 删除链表的倒数第 N 个结点-c语言 遍历一次链表求解
- linux内核里的字符串转换 ,链表操作常用函数(转)
- [LeetCode] 19. 删除链表的倒数第N个节点 ☆☆☆
- 【剑指Offer学习】【面试题56:链表中环的入口结点】
- 【剑指Offer面试题】 九度OJ1518:反转链表
- 【Leetcode刷题Python】25.K 个一组翻转链表
- 3.顺序表和链表的优缺点
- 【数据结构与算法】什么是双向循环链表?以及实现过程
- 【数据结构与算法】什么是链表?并用代码手动实现一个单向链表
- 数据结构和算法 二、数组和链表
- 【数据结构】顺序表和链表的优缺点 | 浅析CPU高速缓存 | 理解 “命中率“ 的概念