Java实现双向循环链表
作者:I like study. 发布时间:2023-11-08 04:14:40
标签:java,链表
双向循环链表定义
相比于单链表,有两个指针,next指针指向下一个结点,prior指针指向上一个结点,最后一个结点的next指针指向头结点,头结点的prior指针指向最后一个结点
代码实现:
我们对单链表的实现加以修改
package algorithm.datastructure.doublelinkedlist;
import java.util.NoSuchElementException;
/**
* 双向循环链表
* 两个指针,next指针指向下一个结点,prior指针指向上一个结点,最后一个结点的next指针指向头结点,
* 头结点的prior指针指向最后一个结点
* */
public class LinkedList {
private Node head;//头节点
private int size;//链表长度
static private class Node{
private int data;//数据
private Node next;//下一个结点
private Node prior;//上一个结点
public Node(){
}
public Node(int data){
this.data=data;
}
private Node(int data,Node next){
this.data=data;
this.next=next;
}
public Node(Node prior,int data,Node next){
this.prior=prior;
this.data=data;
this.next=next;
}
}
//初始化空链表
public LinkedList(){
//head=null;
}
//添加元素
public Boolean add(int element){
linkLast(element);
return true;
}
//在某个位置之前添加元素
public Boolean add(int index,Integer element){
checkPositionIndex(index);
if (index==size){
linkLast(element);
} else {
linkBefore(element,node(index));
}
return true;
}
//根据下标获取元素
public int get(int index){
checkElementIndex(index);
return node(index).data;
}
//获取第一个元素
public Integer getFirst(){
Node f=head;
if (f==null){
throw new NoSuchElementException();
} else {
return f.data;
}
}
//获取最后一个元素
public Integer getLast(){
if (size==0){
throw new NoSuchElementException();
}
return head.prior.data;
}
//删除第一个元素
public Integer removeFirst(){
Node f=head;
if (f==null){
throw new NoSuchElementException();
} else {
return unlink(head);
}
}
//删除最后一个元素
public Integer removeLast(){
if (size==0){
throw new NoSuchElementException();
}
int index=size-1;
return unlink(node(index));
}
//根据索引删除元素
public Integer remove(int index){
checkElementIndex(index);
return unlink(node(index));
}
//销毁链表
public void destroyList(){
clearList();
}
//将链表置为空表
public void clearList() {
for (Node p=head;p!=null;){
Node next=p.next;//记录下一个结点
p=null;//删除当前结点
p=next;//指向下一个结点
}
size=0;
head=null;
}
//遍历链表
public void traverseList(Boolean isReverseVisited){
if (!isEmpty()){
if (!isReverseVisited){
for (Node p=head;p!=head.prior;p=p.next){
System.out.println(p.data);
}
System.out.println(head.prior.data);
} else {
for (Node p=head.prior;p!=head;p=p.prior){
System.out.println(p.data);
}
System.out.println(head.data);
}
}
}
//返回链表元素个数
public int size(){
return size;
}
public Boolean isEmpty(){
return size==0;
}
/**双向链表插入一个结点,要改变的指针如下:
*
*(1)前一个结点的next指针
*(2)后一个结点的prior指针
*(3)新创建的结点的prior指针和next指针
* */
//尾部添加结点
private void linkLast(int element){
if (size==0){//没有结点时
head=new Node(element);
head.next=head;
head.prior=head;
size++;
} else {
//得到最后一个结点
Node oldTail=head.prior;
//new新的尾结点 newTail
//newTail的前一个结点设置为旧的尾结点,
//newTail的后一个结点指向head
Node newTail=new Node(oldTail,element,head);
//head的下一个结点指向newTail
head.prior=newTail;
//旧的尾结点的下一个结点指向新的尾结点
oldTail.next=newTail;
size++;
}
}
//在某结点之前插入结点
private void linkBefore(int element,Node node){
if (node==null){
linkLast(element);
} else {
Node p=head;
if (node.data==p.data){
Node q=p.prior;
Node newNode=new Node(q,element,p);
q.next=newNode;
p.prior=newNode;
size++;
} else {
for (p=p.next;p!=head;){
if (node.data==p.data){
Node q=p.prior;
Node newNode=new Node(q,element,p);
q.next=newNode;
p.prior=newNode;
size++;
}
p=p.next;
}
}
}
}
/*
* 双向链表删除一个结点:
* (1)找到该结点
* (2)找到该结点的前一结点(prior)p和下一结点(next)q
* (3)p的next指针指向q,q的prior指针指向p
* (4)如果是删除的是头结点,指明当前头结点
* */
//删除结点
private Integer unlink(Node node){
Integer deleteNodeData=node.data;
Node p=null,q=null;
if (deleteNodeData==head.data){//该结点为头结点
Node cur=head;
p=head.prior;
q=head.next;
p.next=q;
q.prior=p;
head=q;
cur=null;
size--;
} else {
Node m;
for (m=head.next;m!=head;){
if (m.data==deleteNodeData){
p=m.prior;
q=m.next;
p.next=q;
q.prior=p;
m=null;
size--;
break;
}
m=m.next;
}
}
return deleteNodeData;
}
//数组下标是否越界
private Boolean isElementIndex(int index){
return index>=0&&index<size;
}
//插入位置是否越界
public Boolean isPositionIndex(int index){
return index>=0&&index<=size;
}
//检验下标是否越界,抛出异常
private void checkElementIndex(int index){
if(!isElementIndex(index)){
try {
throw new IndexOutOfBoundsException("下标越界");
} catch (Exception e) {
e.printStackTrace();
}
}
}
//检验插入下标是否越界,抛出异常
private void checkPositionIndex(int index){
if(!isPositionIndex(index)){
try {
throw new IndexOutOfBoundsException("下标越界");
} catch (Exception e) {
e.printStackTrace();
}
}
}
//返回指定位置的元素
private Node node(int index){
int nowIndex = 0;
if(size>0){
for (Node p=head;p!=head.prior;){
if (nowIndex==index){
return p;
}
p=p.next;
nowIndex++;
}
if (nowIndex==index){
return head.prior;
}
}
return null;
}
public static void main(String[] args) {
java.util.LinkedList linkedList0=new java.util.LinkedList();
linkedList0.add(6);
linkedList0.remove(0);
linkedList0.size();
linkedList0.peek();
//linkedList0.getFirst();
linkedList0.clear();
//测试
LinkedList linkedList=new LinkedList();
linkedList.add(2);
linkedList.add(3);
linkedList.add(5);
linkedList.add(7);
linkedList.add(1);
linkedList.add(33);
linkedList.add(4,0);
linkedList.add(3,1);
linkedList.add(7,6);
linkedList.add(0,10);
linkedList.add(10,11);
linkedList.remove(0);
linkedList.remove(0);
linkedList.remove(0);
linkedList.remove(1);
linkedList.remove(4);
linkedList.remove(5);
linkedList.remove(0);
// linkedList.remove(0);
// linkedList.remove(0);
// linkedList.remove(0);
// linkedList.remove(0);
System.out.println(linkedList.get(0));
System.out.println(linkedList.get(1));
System.out.println(linkedList.get(2));
System.out.println(linkedList.get(3));
System.out.println(linkedList.getFirst());
System.out.println(linkedList.getLast());
linkedList.removeFirst();
linkedList.removeLast();
System.out.println("遍历链表");
linkedList.traverseList(false);
// System.out.println("逆向遍历链表");
// linkedList.traverseList(true);
System.out.println("链表长度");
System.out.println(linkedList.size());
linkedList.clearList();
}
}
来源:https://blog.csdn.net/rj2017211811/article/details/109316864


猜你喜欢
- 本文实例介绍的是Android的Tab控件,Tab控件可以达到分页的效果,让一个屏幕的内容尽量丰富,当然也会增加开发的复杂程度,在有必要的时
- 本文实例总结了Android开发之Button事件实现与监听方法。分享给大家供大家参考,具体如下:先来介绍Button事件实现的两种方法ma
- CoordinatorLayout 实现了多种Material Design中提到的滚动效果。目前这个框架提供了几种不用写动画代码就能工作的
- 解决问题:我在做移动端accessToken的使用遇到一个问题,就是普通类死活注入不进去spring bean,我和同事雷杰通过各种注解,x
- 官网下载直接打开官网:http://www.oracle.com/technetwork/java/javase/downloads/jdk
- 本文实例讲述了C#操作注册表的方法。分享给大家供大家参考,具体如下:下面我们就来用.NET下托管语言C#注册表操作,主要内容包括:注册表项的
- RabbitMQ作为AMQP的代表性产品,在项目中大量使用。结合现在主流的spring boot,极大简化了开发过程中所涉及到的消息通信问题
- 先看看效果图:1、XML布局引入<com.net168.lib.SortTabLayout android:id=&quo
- 本文实例为大家分享了Java实现置换密码加密解密,供大家参考,具体内容如下思路置换密码只不过是简单的换位而已,这里写的是一个分组长度为7的置
- 1. 查找1) 顺序查找 SeqSearch.java2) 二分查找【二分法,放在算法讲解】2. 顺序查找有一个数列:白眉鹰王、金毛狮王、紫
- 如何解决Mybatis--java.lang.IllegalArgumentException: Result Maps collection already contains value for X这两天因为项目需要整合spring、struts2、mybatis三大框架,但启动的时候总出现这个错误,困扰我好久,在网上找到的答案都不是我
- 本章主要建立在已经安装好Erlang以及RabbitMQ的基础上,接下来,简单介绍一下使用一、Direct直接模式 通过routingKey
- 问题今天在springboot中使用mybatis的时候不能字段不能够进行自动映射,mybatis的版本是3.5.11,数据库是按照下划线进
- 在刚接触后台线程的时候,觉得线程神秘且高深,并且时常有先辈们千叮万嘱:能不用的时候,尽量不要用,千万不要滥用线程,否则会发生预料不到的结果。
- 一、结论先行ArrayList在JDK1.8与JDK1.7底层区别JDK1.7:ArrayList像饿汉式,直接创建一个初始容量为10的数组
- 前言本节我们将学习一下@PostConstruct的用法。概述@PostContruct是spring框架的注解,在方法上加该注解会在项目启
- 最近在开发一个项目,需要写一个后管系统,Bootstrap是美国Twitter公司的设计师Mark Otto和Jacob Thornton合
- 使用AspectJ实现AOP注解方式XML方式AspectJ简介AspectJ是一个基于Java语言的AOP框架Spring2.0以后新增了
- 大家先看下效果图:Android为我们提供了四种应组件,分别为Activity、Service、Broadcast receivers和Co
- 目录synchronized 实现原理适应性自旋(Adaptive Spinning)锁升级Java 对象头偏向锁(Biased Locki