双向链表
一、介绍
基于前面所讲的单向链表实现, 双向链表也叫双向表,是链表的一种,它由多个结点组成,每个结点都由一个数据域和两个指针域组成,数据域用来存储数据,其中一个还真域迎来指向其后继结点,另外一个指针域用来指向前驱结点链表的头结点的数据不存储数据,指向前驱结点的指针阈值为null,指向后继结点的指针域指向第一个真正存储数据的结点。
二、代码实现
package 数据结构;
import java.util.Iterator;
/**
* @author chao
* @create -05-2022-05-25-13:22
*/
public class TwoWayLinkList<T> implements Iterable<T> {
private Node head;
private Node last;
private int N;
private class Node{
public Node(T item,Node pre,Node next){
this.item=item;
this.pre=pre;
this.next=next;
}
public T item;
public Node pre;
public Node next;
}
public TwoWayLinkList() {
//初始化头结点 和为节点
this.head=new Node(null,null,null);
this.last=null;
this.N=0;
}
//清空链表
public void clear(){
this.head.next=null;
this.last=null;
this.head.pre=null;
this.head.item=null;
this.N=0;
}
//获取链表的长度
public int lLenght(){
return N;
}
//判断链表是否为空
public boolean isEmpty(){
return N==0;
}
//获取第一个元素
public T getFirst(){
if(isEmpty()){
return null;
}
return head.next.item;
}
//获取最后一个元素
public T getLast(){
if(isEmpty()){
return null;
}
return last.item;
}
//插入元素t
public void insert(T t){
if(isEmpty()){
//如果链表为空
//创建新的节点
//新节点称为为节点,头节点指向为节点
Node newnode1 = new Node(t, head, null);
last=newnode1;
head.next=last;
}else {
//如果链表不为空
Node oldlast=last;
Node newnode2 = new Node(t, oldlast, null);
//让当前节点指向新节点
oldlast.next=newnode2;
//让新节点成为尾结点
last=newnode2;
//闯进新的节点
}
N++;
}
//向指定位置i插入元素t
public void insert(int i,T t){
//找到i位置的前一个节点
Node pre=head;
for (int j = 0; j <i ; j++) {
pre=pre.next;
}
//找到当前节点
Node curr = pre.next;
//创建新节点
Node newnode = new Node(t, pre, curr);
//前一个结点指向当前节点
pre.next=newnode;
//新节点指向当前节点
curr.pre=newnode;
//元素加一
N++;
}
//获取指定位置i出元素
public T gets(int i){
Node curr=head.next;
for (int j = 0; j <i ; j++) {
curr=curr.next;
}
return curr.item;
}
public int indexOf(T t){
Node n=head;
for (int i = 0; n.next!=null ; i++) {
n=n.next;
if(n.next.equals(t)){
return i;
}
}
return -1;
}
//删除位置i出元素,并返回该元素
public T remove(int i){
//找到i位置的前一个节点
Node pre=head;
for (int j = 0; j <i ; j++) {
pre=pre.next;
}
//找到i位置节点
Node curr = pre.next;
//找打i位置下一个节点
Node nextnode = curr.next;
//i位置前一个节点变为当前节点
pre.next=nextnode;
//i位置下一个节点变为当前节点
nextnode.pre=pre;
//元素个数减一
N--;
return curr.item;
}
@Override
public Iterator<T> iterator() {
return new TItrtayor();
}
private class TItrtayor implements Iterator{
private Node n;
public TItrtayor(){
this.n=head;
}
@Override
public boolean hasNext() {
return n.next!=null;
}
@Override
public Object next() {
n=n.next;
return n.item;
}
}
}
三、测试代码
package 数据结构.Test;
import 数据结构.TwoWayLinkList;
/**
* @author chao
* @create -06-2022-06-02-16:20
*/
public class TtoWayLinklistTest {
public static void main(String[] args) {
TwoWayLinkList<String>s1=new TwoWayLinkList<>();
s1.insert("姚明");
s1.insert("科比");
s1.insert("麦迪");
s1.insert(1,"詹姆斯");
for(Object s:s1){
System.out.println(s);
}
System.out.println("------------------------------");
//测试获取
String getResult=s1.gets(1);
System.out.println("获取的索引1的结果为:"+getResult);
//测试删除
String removeResult=s1.remove(0);
System.out.println("删除元素是:"+removeResult);
System.out.println("清空后的元素个数为"+s1.lLenght());
System.out.println("111111111111111111");
System.out.println("第一个元素是"+s1.getFirst());
System.out.println("最后个元素是"+s1.getLast());
//测试清空
s1.clear();
}
}
四、结果