当前位置: 首页 > news >正文

7-6 带头节点的双向循环链表操作

本题目要求读入一系列整数,依次插入到双向循环链表的头部和尾部,然后顺序和逆序输出链表。

链表节点类型可以定义为

typedef int DataType;
typedef struct LinkedNode{DataType data;struct LinkedNode *prev;struct LinkedNode *next;
}LinkedNode;

链表类型可以定义为

typedef struct LinkedList{int length; /* 链表的长度 */LinkedNode head; /* 双向循环链表的头节点 */
}LinkedList;

初始化链表的函数可声明为

void init_list(LinkedList *list);

分配节点的函数可声明为

LinkedNode *alloc_node(DataType data);

头部插入的函数可声明为

void push_front(LinkedList *list, DataType data);

尾部插入的函数可声明为

void push_back(LinkedList *list, DataType data);

顺序遍历的函数可声明为

void traverse(LinkedList *list);

逆序遍历的函数可声明为

void traverse_back(LinkedList *list);

输入格式:

输入一行整数(空格分隔),以-1结束。

输出格式:

第一行输出链表顺序遍历的结果,第二行输出逆序遍历的结果。

输入样例:

在这里给出一组输入。例如:

1 2 3 4 5 6 -1

输出样例:

5 3 1 2 4 6
6 4 2 1 3 5

 链表式代码

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first 
#define yy second 
#define endl '\n'
#define O_O ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
struct node
{int data;node *next;node *pre;
};
int main()
{node *head, *tail, *tt, *p;head = new node;tail = new node;tt = new node;head -> next = NULL;head -> pre = NULL;tail = head;tt = head;int x, cnt = 0, n = 0;while(cin >> x && x != -1){n ++;p = new node;p -> next = NULL;p -> pre = NULL;p -> data = x;cnt ++;if(cnt & 1){tail -> pre = p;p -> next = tail;tail = p;}else{tt -> next = p;p -> pre = tt;tt = p;}}int m = n;while(tail && m){if(tail == head){tail = tail -> next;continue;}m --;if(m)cout << tail -> data << " ";else cout << tail -> data << endl;tail = tail -> next;}m = n;while(tt && m){if(tt == head){tt = tt -> pre;continue;}m --;if(m)cout << tt -> data << " ";else cout << tt -> data << endl;tt = tt -> pre;}return 0;
}

http://www.lryc.cn/news/7902.html

相关文章:

  • npm publish 、 npm adduser 提示 403 的问题
  • Java 8的函数式接口使用示例
  • 2023年企业如何改善员工体验?为什么员工体验很重要?
  • 设计模式:桥接模式让抽象和实现解耦,各自独立变化
  • C++学习记录——십 STL初级认识、标准库string类
  • 【redis】redis缓存与数据库的一致性
  • XCP实战系列介绍12-基于Vector_Davinci工具的XCP配置介绍(一)
  • Unity Material详解
  • 碰撞检测算法分类
  • 代码随想录第十二天(
  • 电源模块 DC-DC直流升压正负高压输出12v24v转±110V±150V±220V±250V±300V±600V
  • 【动画图解】这个值取对了,ViewPager2才能纵享丝滑
  • CSDN每日一练:小豚鼠搬家
  • Dockerfile命令及实践构建一个网站
  • [VMware]Ubuntu18.04 网络图标消失
  • 国产C2000,P2P替代TMS320F280049C,独立双核32位CPU,主频高达400MHz
  • 二十五、Gtk4-多线程分析
  • JVM基础学习
  • ASML逆袭史:人、资金、技术,缺一不可
  • MongoDB 覆盖索引查询
  • Flink Checkpoint 中的Aligned Checkpoint 和 Unaligned Checkpoint
  • C++快速入门
  • ubuntu18.04 network有线网络图标缺失解决记录
  • java对象克隆和面向对象的设计原则
  • 传透式血氧仪设计方案
  • 让逆向工程师们头疼的代码混淆,就像永远也走不出的“浪浪山”
  • 【拓展】基于机器学习的心脏病预测方法(14)——心脏病数据集补充
  • 深度解读Webpack中的loader原理
  • 2023年全国最新二级建造师精选真题及答案
  • 为什么现代企业发展离不开CRM系统的助力