欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java实现数据结构之双链表(查找,修改,插入,删除)结点

程序员文章站 2022-03-22 19:17:03
...

双链表的实现原理与单链表的基本一致,不同之处只是在于双链表的结点增加了一个前驱指针域,用来指向它的前驱结点,可以实现链表的双向遍历,在插入和删除结点是也更方便

可以参考单链表来看https://blog.csdn.net/qq_34517044/article/details/102817627

package Datastruct;
class DoubleLinkList<Item>{
	private class Node{
		Item item;
		Node prenext;
		Node latnext;
	}
    private Node first;
    private Node last;
    private int N;
    public boolean isEmpty() {
    	return first==null;
    }
    public int size() {
    	return N;
    }
    public void put(Item item) {
    	Node oldlast=last;
    	last=new Node();
    	last.item=item;
    	if(isEmpty()) first=last;
    	else {
    		oldlast.latnext=last;
    		last.prenext=oldlast;
    	}
    	N++;
    }
    public void search(int i) {
    	if(isEmpty()) {
    		System.out.println("List is Empty,Search fail");
    		return; 
    	}
    	if(i<1||i>N) {
    		System.out.println("Search Exceed");
    		return;
    	}
    	else {
    		Node x=first;
    		int count=0;
    		for(;x!=null;x=x.latnext) {
    			count++;
    			if(i==count) break;
    		}
    		System.out.println(x.item);
    	}
    }
    public void change(int i,Item item) {
    	if(isEmpty()) {
    		System.out.println("List is Empty,Change fail");
    		return;
    	}
    	if(i<1||i>N) {
    		System.out.println("Change Exceed");
    		return;
    	}
    	else {
    		Node x=first;
    		int count=0;
    		for(;x!=null;x=x.latnext) {
    			count++;
    			if(count==i) break;
    		}
    		x.item=item;
    	}
    }
    public void insert(Item item,int i) {
    	if(i==1) {
    		Node oldfirst=first;
    		first=new Node();
    		first.item=item;
    		first.latnext=oldfirst;
    		oldfirst.prenext=first;
    	}
    	else {
    		Node x=first;
    		int count=0;
    		for(;x!=null;x=x.latnext) {
    			count++;
    			if(i==count) break;
    		}
    		Node n=new Node();
    		n.item=item;
    		n.prenext=x.prenext;
    		n.latnext=x;
    		x.prenext.latnext=n;
    		x.prenext=n;
    	}
    	N++;
    }
    public void delete(int i) {
    	if(isEmpty()) {
    		System.out.println("List is Empty,delete fail");
    		return;
    	}
    	if(i==1) {
    		first=first.latnext;
    		first.prenext=null;
    	}
    	else if(i==N) {
    		Node x=first;
    		int count=0;
    		for(;x!=null;x=x.latnext) {
    			count++;
    			if(count==i) break;
    		}
    		x.prenext.latnext=null;
    		last=x.prenext;
    	}
    	else {
    		Node x=first;
    		int count=0;
    		for(;x!=null;x=x.latnext) {
    			count++;
    			if(i==count) break;
    		}
    	    x.prenext.latnext=x.latnext;
    	    x.latnext.prenext=x.prenext;
    	}
    	N--;
    }
    public void show() {
    	if(isEmpty()) {
    		System.out.println("链表为空");
    		return;
    	}
    	else {
    		Node x=first;
    		for(;x!=null;x=x.latnext) {
    			System.out.print(x.item+" ");
    		}
    		System.out.println();
    	}
    }
}
public class DoubleLinkListTest {
	public static void main(String[] args) {
		DoubleLinkList<Integer> l=new DoubleLinkList<Integer>();
		l.put(1);
		l.put(3);
		l.put(2);
		l.put(5);
		l.change(3,7);
		l.show();
		System.out.println("链表大小:"+l.size());
		l.insert(8,2);
		l.show();
		l.delete(2);
		l.show();
	}
}

因为用的是泛型,测试时可以任意指定基本对象类型 

测试结果是对的,可以自行测试