文章目录

  • ==模板== --双链表
  • ==模板==--循环双链表
  • 1.双链表的基本运算
  • 2:整数双链表的基本运算-3
  • 3:整数双链表的基本运算-4
  • 4:循环双链表的基本运算

模板 --双链表

struct node{int data;struct node *next;struct node *prior; //多一个前指针prior
};void initList(node *&head){head=new node();head->next=NULL;head->prior=NULL;
}void destoryList(node *&head){ //销毁链表node *pre=head;node *p=head->next; //从头结点开始删除while(p!=NULL){delete pre; //c语言:free(pre);pre=p;p=p->next;}delete pre;
}int getLength(node *head){ //求长度int count=0;node *p=head->next; //工作指针while(p!=NULL){p=p->next;count++;}return count;
}void insElem(node *&head,int value,int i){ //在第i个元素后插入元素,值为valuenode *p=head;int count=1;while (p!=NULL&&count!=i) //找到第i-1个结点count++,p=p->next;node *q=new node ();q->data=value;q->next=p->next;//1if(p->next!=NULL)//判断p是不是尾结点p->next->prior=q;//2q->prior=p;//3p->next=q;//4
}void output(node *head)
{node *p=head->next;while (p!=NULL){cout<<p->data<<" ";p=p->next;}cout<<endl;
}
void deleElem(node *&head,int i){ //删除第i个元素int count=0;node *p=head;while(p!=NULL&&count!=i) //直接找第i个,改变它的指针域count++,p=p->next;node *pre=new node ();pre=p->prior;//保存前驱节点 if(p->next!=NULL)p->next->prior=pre;//1 绕过自己pre->next=p->next;//2delete p;
}
void createList_Tail(node *&head,int a[],int len){node *tc=head;for(int i=0;i<len;i++){node *s=new node ();s->data=a[i];tc->next=s;s->prior=tc;tc=s;}tc->next=NULL;
}

模板–循环双链表

#include <iostream>
#define Element char
using namespace std;struct node{Element data;node *next;node *prior;
};void initList(node *&head){ //初始化双链表head=new node();head->next=head;head->prior=head;
}void create_Tail(node *&head,Element a[],int len){  //尾插法建表node *tc=head;for(int i=0;i<len;i++){node *s=new node();s->data=a[i];tc->next=s;s->prior=tc;tc=s;}tc->next=head; //最后一个结点的prior应该指向head
}void output(node *head){  //输出node *p=head->next;while (p!=head)cout<<p->data<<" ",p=p->next;cout<<endl;
}int getLength(node *head){ //求长度int count=0;node *p=head->next;while (p!=head)count++,p=p->next;return count;
}bool isEmpty(node *head){ //判断是否为空return head->next==head; //1为空
}Element findTh(node *head,int th){ //找到第th个结点node *p=head->next;int count=1;while (p!=head&&count!=th)count++,p=p->next;return p->data;
}int findValue(node *head,Element value){ //找到值为value的结点int count=1;node *p=head->next;while (p!=head&&p->data!=value)count++,p=p->next;return count;
}void insElem(node *&head,Element e,int th){ //在第th个结点后插入值为e的结点node *p=head->next;int count=1;while (p!=head&&count<th)count++,p=p->next;node *s=new node();s->data=e;s->next=p->next;p->next->prior=s;s->prior=p;p->next=s;
}void delElem(node *&head,int th){ //删除第th个结点node *p=head->next;int count=1;while (p!=head&&count!=th)count++,p=p->next;p->next->prior=p->prior;p->prior->next=p->next;delete p;
}void destoryList(node *&head){ //释放双链表node *pre=head,*p=pre->next;while (p!=head){delete pre;pre=p;p=p->next;}delete pre;
}int main()
{node *head;initList(head);int n;char a[1000];cin>>n;for(int i=0;i<n;i++)cin>>a[i];create_Tail(head,a,n);output(head);cout<<getLength(head)<<endl;if(isEmpty(head))cout<<"yes"<<endl;else cout<<"no"<<endl;cout<<findTh(head,3)<<endl;cout<<findValue(head,'a')<<endl;insElem(head,'f',3);output(head);delElem(head,5);output(head);destoryList(head);return 0;
}

1.双链表的基本运算

实现循环单链表的基本运算:初始化、插入、删除、求表的长度、判空、释放。

(1)初始化循环单链表L,输出(L->next==L)的逻辑值;

(2)依次采用尾插法插入元素:输入分两行数据,第一行是尾插法需要插入的字符数据的个数,第二行是具体插入的字符数据。

(3)输出循环单链表L;

(4)输出循环单链表L的长度;

(5)判断循环单链表L是否为空;

(6)输出循环单链表L的第3个元素;

(7)输出元素a的位置;

(8)在第4个元素位置上插入‘w’元素;

(9)输出循环单链表L;

(10)删除L的第5个元素;

(11)输出循环单链表L;

(12)释放循环单链表L。

#include <iostream>using namespace std;
#define Element charstruct node{Element data;node *next;node *prior;
};void initList(node *&head){ //初始化双链表head=new node();head->next=NULL;head->prior=NULL;
}void create_Tail(node *&head,Element a[],int len){  //尾插法建表node *tc=head;for(int i=0;i<len;i++){node *s=new node();s->data=a[i];tc->next=s;s->prior=tc;tc=s;}tc->next=NULL;
}void output(node *head){  //输出node *p=head->next;while (p!=NULL)cout<<p->data<<" ",p=p->next;cout<<endl;
}int getLength(node *head){ //求长度int count=0;node *p=head->next;while (p!=NULL)count++,p=p->next;return count;
}bool isEmpty(node *head){ //判断是否为空return head->next==NULL; //1为空
}Element findTh(node *head,int th){ //找到第th个结点node *p=head->next;int count=1;while (p!=NULL&&count!=th)count++,p=p->next;return p->data;
}int findValue(node *head,Element value){ //找到值为value的结点int count=1;node *p=head->next;while (p!=NULL&&p->data!=value)count++,p=p->next;return count;
}void insElem(node *&head,Element e,int th){ //在第th个结点后插入值为e的结点node *p=head;int count=0;while (p!=NULL&&count!=th)count++,p=p->next;node *s=new node();s->data=e;s->next=p->next;if(p->next!=NULL)p->next->prior=s;s->prior=p;p->next=s;
}void delElem(node *&head,int th){ //删除第th个结点node *p=head->next;int count=1;while (p!=NULL&&count!=th)count++,p=p->next;p->next->prior=p->prior;p->prior->next=p->next;delete p;
}void destoryList(node *&head){ //释放双链表node *pre=head,*p=pre->next;while (p!=NULL){delete pre;pre=p;p=p->next;}delete pre;
}int main()
{node *head;initList(head);int n;char a[1000];cin>>n;for(int i=0;i<n;i++)cin>>a[i];create_Tail(head,a,n);output(head);cout<<getLength(head)<<endl;if(isEmpty(head))cout<<"yes"<<endl;else cout<<"no"<<endl;cout<<findTh(head,3)<<endl;cout<<findValue(head,'a')<<endl;insElem(head,'f',3);output(head);delElem(head,5);output(head);destoryList(head);return 0;
}

2:整数双链表的基本运算-3

题目链接:传送门

void PDDoutput(node *head){ //反向输出node *p=head->next;while (p->next!=NULL) //找到最后一个结点p=p->next;while (p!=head)cout<<p->data<<" ",p=p->prior; //借助prior往前cout<<endl;
}

3:整数双链表的基本运算-4

借助一个计数数组来记录各数字出现的次数,然后将出现次数恰好为1的数字连接到头结点后

int num[1000]={}; //计数数组void simplify(node *&head){node *p=head->next;while (p->next!=NULL){ //保证停止的时候p指向最后一个结点num[p->data]++; //记录p->data出现了一次p=p->next;}num[p->data]++; //记录最后一个结点的数据while(p!=head){ //逆向删除,以保证被删掉的是“后面的”数据num[p->data]--; //次数减小if(num[p->data]!=0){ //如果不为0,说明前面还有一样的数,那么删除现在这个结点if(p->next!=NULL) //删除时候注意判断边界p->next->prior=p->prior;if(p->prior!=NULL)p->prior->next=p->next;delete p;}p=p->prior; //逆向查找}
}

4:循环双链表的基本运算

#include <iostream>
#define Element char
using namespace std;struct node{Element data;node *next;node *prior;
};void initList(node *&head){ //初始化双链表head=new node();head->next=head;head->prior=head;
}void create_Tail(node *&head,Element a[],int len){  //尾插法建表node *tc=head;for(int i=0;i<len;i++){node *s=new node();s->data=a[i];tc->next=s;s->prior=tc;tc=s;}tc->next=head; //最后一个结点的prior应该指向head
}void output(node *head){  //输出node *p=head->next;while (p!=head)cout<<p->data<<" ",p=p->next;cout<<endl;
}int getLength(node *head){ //求长度int count=0;node *p=head->next;while (p!=head)count++,p=p->next;return count;
}bool isEmpty(node *head){ //判断是否为空return head->next==head; //1为空
}Element findTh(node *head,int th){ //找到第th个结点node *p=head->next;int count=1;while (p!=head&&count!=th)count++,p=p->next;return p->data;
}int findValue(node *head,Element value){ //找到值为value的结点int count=1;node *p=head->next;while (p!=head&&p->data!=value)count++,p=p->next;return count;
}void insElem(node *&head,Element e,int th){ //在第th个结点后插入值为e的结点node *p=head->next;int count=1;while (p!=head&&count<th)count++,p=p->next;node *s=new node();s->data=e;s->next=p->next;p->next->prior=s;s->prior=p;p->next=s;
}void delElem(node *&head,int th){ //删除第th个结点node *p=head->next;int count=1;while (p!=head&&count!=th)count++,p=p->next;p->next->prior=p->prior;p->prior->next=p->next;delete p;
}void destoryList(node *&head){ //释放双链表node *pre=head,*p=pre->next;while (p!=head){delete pre;pre=p;p=p->next;}delete pre;
}int main()
{node *head;initList(head);int n;char a[1000];cin>>n;for(int i=0;i<n;i++)cin>>a[i];create_Tail(head,a,n);output(head);cout<<getLength(head)<<endl;if(isEmpty(head))cout<<"yes"<<endl;else cout<<"no"<<endl;cout<<findTh(head,3)<<endl;cout<<findValue(head,'a')<<endl;insElem(head,'f',3);output(head);delElem(head,5);output(head);destoryList(head);return 0;
}

数据结构与算法--第二章pro题解相关推荐

  1. 数据结构与算法第二章 线性表、栈、队列、数组、字符串、树、二叉树、哈希表的增删查

    03 增删查:掌握数据处理的基本操作,以不变应万变 通过前面课时的学习,相信你已经建立了利用数据结构去完成时空转移的思想.接下来,你需要在理论思想的指导下灵活使用.其实,要想灵活使用数据结构,你需要先 ...

  2. 自动驾驶决策规划算法第二章——Apollo EM Planner实践篇

    前置学习内容:自动驾驶控制算法 [自动驾驶][零基础]基础自动驾驶控制算法笔记_免费教学录影带的博客-CSDN博客 自动驾驶决策规划第一章 自动驾驶决策规划算法第一章_免费教学录影带的博客-CSDN博 ...

  3. 数据结构与算法 第二次实验报告堆栈队列

          数据结构与算法 第二次实验报告 姓名:许恺 学号:2014011329 班级:计算机14-1 中国石油大学(北京)计算机科学与技术系 前     言 <数据结构>是计算机及相关 ...

  4. Java算法--第二章--查找与排序(2)递归基础--佩波那契最大公约数插入排序汉诺塔

    Java算法–第二章–查找与排序(2)递归基础 一.找重复 1.找到一种划分方法 2.找到递推公式或者等价转换 都是父问题转化为求解子问题 二.找变化的量 变化的量通常要作为参数 三.找出出口 代码: ...

  5. 数据结构 严蔚敏 第二章 线性表

    数据结构 严蔚敏 第二章 线性表 线性表:由n个(n>=0)数据特征相同的元素构成的有限序列. 线性表的类型定义表示和实现 顺序表 存储单元地址连续 随机存取 若每个元素占用 m 个存储单元,以 ...

  6. 数据结构与算法 --- 第一章 绪论

    数据结构与算法 第一章 绪论 1. 作者的话 2. 为什么要学习数据结构与算法 3. 数据结构与算法的作用 4. 数据结构的概念 4.1 名词解读 4.2 什么是数据 4.3 数据结构 4.4 逻辑结 ...

  7. 【恋上数据结构与算法 第二季】【04】图-基础实现_遍历_拓扑排序

    持续学习&持续更新中- 学习态度:脚踏实地 [恋上数据结构与算法 第二季][04]图-基础实现_遍历_拓扑排序 图的实现方案 邻接矩阵 邻接表 图的基础接口 顶点.边的定义 图的基础实现 图的 ...

  8. python可以在多种平台运行、体现了_2020年智慧树数据结构与算法第二单元章节测试答案...

    2020年智慧树数据结构与算法第二单元章节测试答案 更多相关问题 为了寻找和选择通讯的报道对象,当你获知省教育招生考试院发布了<关于调整普通高等教育专科升本科考试录取办法的通知>后,应该熟 ...

  9. PTA数据结构与算法-第一章——褚论

    文章目录 第一章--褚论 第二章--线性表 第三章--栈与队列 第四章--字符串 第五章--树与二叉树 第六章--图 第七章--排序 第八章--检索 判断题 单选题 程序填空题 第一章--褚论 第二章 ...

最新文章

  1. python编程爱心-python画一个玫瑰和一个爱心
  2. 为何jsp 在resin下乱码,但在tomcat下却工作良好的问题
  3. angularJS的controller之间如何正确的通信
  4. python 面向对象 新式类和经典类
  5. (翻译)Tricks of the windows game programming Gurus(Windows游戏大师之路) --- 前言(作者:ANDRE LAMOTHE 1999)...
  6. HDU1466 计算直线的交点数
  7. emacs .emacs_使用Emacs进行社交并跟踪您的待办事项列表
  8. Nutanix 以现代化 IT 基础架构推动医共体建设
  9. iPhone公司为了节约成本,都干过什么事情?
  10. Git项目下载部分文件或文件夹
  11. RecycleView 使用GridView样式列表添加头部
  12. 水晶报表自定义函数进行代码重用 -日期大写
  13. 《大道至简》读书笔记
  14. 关于idea中springboot主启动类没有绿色启动的问题
  15. 2023秋招大厂经典面试题及答案整理归纳(141-160)校招必看
  16. iOS App 转移 图文详解
  17. 【P14】差分输入分立耳机放大器电路V22大改
  18. Window7开机速度有点慢的解决办法
  19. 【AI测试】人工智能测试整体介绍
  20. 数据可视化之关联分析

热门文章

  1. oracle中表数据更新提交后自动被还原的原因查找
  2. js学到什么程度学框架_如何学到什么
  3. 【视频】中国首届微博开发者大会杨卫华演讲 | 新浪微博架构分享
  4. php案例:用Windows命令来运行php程序
  5. Proxy与Reflect详解
  6. 数据据结构实验——顺序表实验
  7. 两小时学会MySQL查询语句(下篇)
  8. 关于一个二维数组问题
  9. 从零开始学习CANoe(十九)—— Diagnostics
  10. 我的linux内核学习之路,Linux再学习(一)-学习路线规划