/*** @program:* @Author: JINLEI* @Description:*              无头双向链表* @Date: 2021/3/25* @Time: 15:49**/class Node{public int data;  //数据public Node next;  //后继信息public Node prev;  //前驱信息public Node (int data){this.data = data;}
}
public class MyLinkedList {public Node head; //双向链表的头public Node tail; //双向链表的尾//无头双向链表头插法public void addFirst(int data){Node node = new Node(data);if (this.head == null) {//第一次插入数据this.head = node;this.tail = node;}else {node.next = this.head;this.head.prev = node;this.head = node;}}//无头双向链表尾插法public void addLast(int data){Node node = new Node(data);if (this.head == null){//第一次插入this.head = node;this.tail = node;}else {this.tail.next = node;node.prev = this.tail;this.tail = node;}}//打印无头双向链表public void display(){Node cur = this.head;while (cur != null){System.out.print(cur.data+ " ");cur = cur.next;}System.out.println();}//查找是否包含关键字key是否在双向链表中public boolean contains(int key){Node cur = this.head;while (cur != null ){if (cur.data == key) {return true;}cur = cur.next;}return false;}//得到双向链表的长度public int size(){int count = 0;Node cur = this.head;while (cur != null){count++;cur = cur.next;}return count;}private void checkIndex(int index){if (index < 0 || index > size()){throw new RuntimeException("index不合法");}}private Node searchIndex(int index){Node cur = this.head;while (index != 0){cur = cur.next;index--;}return cur;}//任意位置插入,第一个数据节点为0号下标public void addIndex(int index, int data){checkIndex(index);if (index == 0){addFirst(data);return ;}if (index == size()){addLast(data);return ;}Node cur = searchIndex(index);Node node = new Node(data);node.next = cur;node.prev = cur.prev;cur.prev.next = node;cur.prev = node;}//删除第一次出现的关键字为key的节点public int remove(int key){Node cur = this.head;while (cur != null){if (cur.data == key){int oldData = cur.data;// 删除的是头节点if (cur == this.head){this.head = this.head.next;this.head.prev = null;}else {//不是头节点cur.prev.next = cur.next;//判断是不是尾节点if (cur.next != null){cur.next.prev = cur.prev;}else {this.tail = cur.prev; //删除的尾巴节点  只需要tail 前移}}return oldData;}cur = cur.next;}return -1;}//删除所有值为key的节点public void removeAllKey(int key){Node cur = this.head;while (cur != null){if (cur.data == key){int oldData = cur.data;// 删除的是头节点if (cur == this.head){this.head = this.head.next;if (this.head != null) {this.head.prev = null;}}else {//不是头节点cur.prev.next = cur.next;//判断是不是尾节点if (cur.next != null){cur.next.prev = cur.prev;}else {this.tail = cur.prev; //删除的尾巴节点  只需要tail 前移}}}cur = cur.next;}}/*** 清除双向链表* 一个一个节点进行释放*/public void clear(){while (this.head != null){Node cur = this.head.next;this.head.prev = null;this.head.next = null;this.head = cur;}this.tail = null;}
}

双向链表的常用基本操作相关推荐

  1. linux mysql常用基本操作,Linux下MySQL数据库常用基本操作 一

    Linux下MySQL数据库常用基本操作 一 0.登录数据库 mysql -u root -p 1.显示数据库 show databases; 2.选择数据库 use 数据库名; 3.显示数据库中的表 ...

  2. “双向链表的一些基本操作”

    void CreateLink()//头插法建立双向链表节点(逆序存储) {node *L,*s;L=new node;L->prior=L->next=NULL;for(int i=0; ...

  3. python基础-07-元组/字典的常用基本操作

    题目1 •敏感词判断 li = ["苍井空","波多野结衣","武藤兰","东京热"] •将用户输入的敏感词替换成* • ...

  4. Mathematica中的常用基本操作

    清除变量 Remove["`*"] Clear["Global`*"]; (*Clear all variables*) MMA中 ` 符号是一个很抽象但又挺常 ...

  5. python列表字典操作_Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结...

    创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_star ...

  6. python读取字符串的list dict_转:Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结...

    1 创建列表2 sample_list = ['a',1,('a','b')]3 4 Python 列表操作5 sample_list = ['a','b',0,1,3]6 7 得到列表中的某一个值8 ...

  7. [转载] Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结

    创建列表 sample_list = ['a',1,('a','b')]Python 列表操作 sample_list = ['a','b',0,1,3]得到列表中的某一个值 value_start ...

  8. 06列表的常用基本操作

    基本数据类型列表list(增删改查) •list 专门是用来存储大量的数据的数据类型 •在python中使用[]中括号来表示列表 •列表是可变的数据类型,内部元素没有任何要求 •列表: 能装东西的东西 ...

  9. 05字符串的常用基本操作

    •1,upper()把字符串更改为大写字母,忽略大小写时使用 •2,strip()默认去掉字符串左右两端的空白(\n \t space) •3,replace () 字符串替换 •4,split () ...

最新文章

  1. 自学linux指令分析-cat
  2. 【Java】Java运行cmd命令直接导出.sql文件
  3. 【wordpress基础教程一】:wordpress简介和安装
  4. 快速安装 Moodle 指南
  5. 11-2-进程控制块
  6. “MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。”的解决方案
  7. 【STM32】8.简单呼吸灯的制作教程,附代码、效果视频
  8. 【SSD目标检测】1:图片、视频内的物体检测与定位
  9. 惠普打印机m226dn教程_惠普m226dn说明书
  10. java述职报告ppt_java开发人员述职报告.ppt
  11. 大学四年,工作2年我总结了后端面试的所有知识点(持续更新)
  12. php-redis常用操作
  13. 用计算机管理学生成绩,北京计算机大学组织专家对《学生成绩管理系统》的需求方案进行评审,为使参会人员对会议流程和内容有一个清晰的了解_题来了...
  14. Java —— 自定义JSR303校验
  15. 反射系数和透射系数随入射角的变化曲线
  16. 图片文字怎么转换成文本?分享几个好方法
  17. mysql 查询数据库中所有表的信息
  18. react中的双向绑定
  19. java-php-python-ssm网课系统计算机毕业设计
  20. 如何将文字转换成语音?分享两种文字转换语音小技巧

热门文章

  1. django-视图函数装饰器
  2. spring系统学习:20180607--Spring的 IOC 的XML和注解的整合开发
  3. 使用CrashHandler来获取应用的crash信息
  4. RxJava Agera 从源码简要分析基本调用流程(2)
  5. 干货:结合Scikit-learn介绍几种常用的特征选择方法
  6. MySQL学习足迹记录01--SOURCE,SHOW
  7. global.asax不执行原因
  8. Linux磁盘管理2
  9. 04 16 团队竞技(第二场) 赛后总结
  10. 终于知道以后该咋办了!