Java实现双向链表的基本操作

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">双向链表即表中任一结点都是由data数据域,上一个结点引用域lastNode,下一个结点引用域nextNode组成。</span>

/**
 *双向链表结点类
 * @author liangxiamoyi
 * @param <T>
 */
public class DLNode<T> {
	/**
	 * 数据域
	 */
	protected T data;
	/**
	 * 下一个结点
	 */
	protected DLNode<T> nextNode;
	/**
	 * 上一个结点
	 */
	protected DLNode<T> lastNode;
	/**
	 * 构造方法
	 * @param last 上一个结点
	 * @param next 下一个结点
	 */
	public DLNode(DLNode<T> last,DLNode<T> next ){
		this.lastNode=last;
		this.nextNode=next;
	}
	/**
	 * 构造方法
	 * @param item 数据域
	 * @param last 上一个结点
	 * @param next 下一个结点
	 */
	public DLNode(T item,DLNode<T> last,DLNode<T> next){
		this.data=item;
		this.nextNode=next;
		this.lastNode=last;
	}
}

/**
 * 双向链表类
 * @author liangxiamoyi
 *
 * @param <T>
 */
public class DLList<T> {
	/**
	 * 表头
	 */
	private DLNode<T> head;
	/**
	 * 表尾
	 */
	private DLNode<T> tail;
	/**
	 * 当前结点
	 */
	private DLNode<T> currNode;
	/**
	 * 链表大小
	 */
	private int size;
	/**
	 * 构造只有一个哨位结点的空表
	 */
	public DLList(){
		this.head=new DLNode<T>(null,null);
		this.size=0;
	}
	/**
	 * 构建有一个结点的单链表
	 * @param item 数据域
	 */
	public DLList(T item){
		this.head=new DLNode<T>(null, null);
		tail=currNode=new DLNode<T>(item,head,null);
		head.nextNode=tail;
		size=1;
	}
	/**
	 * 判断是否为空
	 * @return
	 */
	public boolean isEmpty(){
		return head.nextNode==null;
	}
	/**
	 * 得到单链表的长度
	 * @return
	 */
	public int getSize(){
		return this.size;
	}
	/**
	 * 存取,得到第k个结点的数据域
	 * @param k 第k个结点
	 * @return 返回第k个结点的数据
	 */
	public T find(int k){
		if(k<0||k>size||size==0){
			throw new RuntimeException("empty list or unreasonable position!");
		}
		currNode=head.nextNode;
		for(int i=2;i<=k;i++){
			currNode=currNode.nextNode;
		}
		return currNode.data;
	}
	/**
	 * 查找,得到第一个数据为item的结点的位置
	 * @param item 数据
	 * @return 位置
	 */
	public int search(T item){
		currNode=head.nextNode;
		for(int i=1;i<=size;i++){
			if(currNode.data==item)return i;
			else{
				currNode=currNode.nextNode;
			}
		}
		return -1;
	}
	/**
	 * 删除当前结点,并将其数据返回
	 * @return 
	 */
	public T delete(){
		if(currNode==head||isEmpty()){
			throw new RuntimeException("head node or empty list");
		}
		DLNode<T> temp=currNode;
		currNode.nextNode.lastNode=currNode.lastNode;
		currNode.lastNode.nextNode=currNode.nextNode;
		size--;
		if(currNode==tail){
			tail=currNode.lastNode;
		}
		currNode=currNode.lastNode;
		return temp.data;
	}
	/**
	 * 删除哨位结点后的第一个真正表结点,并将数据返回
	 * @return
	 */
	public T deleteFromHead(){
		if(isEmpty()){
			throw new RuntimeException("empty list");
		}
		DLNode<T> temp=head.nextNode;
		head.nextNode=head.nextNode.nextNode;
		size--;
		if(temp==tail){
			tail=head;
		}
		else{
			head.nextNode.lastNode=head;
		}
		return temp.data;
	}
	/**
	 * 删除表尾结点,并将数据返回
	 * @return
	 */
	public T deleteFromTail(){
		if(isEmpty()){
			throw new RuntimeException("empty list");
		}
		DLNode<T> p=tail;
		tail=tail.lastNode;
		tail.nextNode=null;
		size--;
		return p.data;
	}
	/**
	 * 删除当前结点的下一个结点
	 * @return
	 */
	public T deleteNext(){
		if(isEmpty()||currNode.nextNode==null){
			throw new RuntimeException("empty list or the next node is null");
		}
		DLNode<T> p=currNode.nextNode;
		if(p==tail){
			tail=currNode;
			currNode.nextNode=null;
		}
		else{
			currNode.nextNode=p.nextNode;
			p.nextNode.lastNode=currNode;
		}
		size--;
		return p.data;
		
	}
	/**
	 * 在当前结点后插入一个数据为item的结点
	 * @param item 数据
	 */
	public void insert(T item){
		if(isEmpty()){
			tail=head.nextNode=new DLNode<T>(item,head,null);
			size++;
			return;
		}
		DLNode<T> p=new DLNode<T>(item,currNode,currNode.nextNode);
		currNode.nextNode.lastNode=p;
		currNode.nextNode=p;
		size++;
		if(tail==currNode){
			tail=p;
		}
	}
	/**
	 * 从表尾插入一个数据为item的结点
	 * @param item
	 */
	public void insertFromTail(T item){
		tail.nextNode=new DLNode<T>(item,tail,null);
		tail=tail.nextNode;
		size++;
	}
	/**
	 * 在哨位结点后插入一个数据为item的结点
	 * @param item
	 */
	public void insertFromHead(T item){
		if(isEmpty()){
			head.nextNode=new DLNode<T>(item,head,null);
			tail=head.nextNode;
			size++;
			return;
		}
		else{
			DLNode<T> p=new DLNode<T>(item, head,head.nextNode);
			head.nextNode.lastNode=p;
			head.nextNode=p;
			size++;
		}
	}
	/**
	 * 打印所有结点的数据
	 */
	public void showNodes(){
		currNode=head.nextNode;
		for(int i=1;i<=size;i++){
			System.out.println(currNode.data);
			currNode=currNode.nextNode;
		}
	}
	//测试
	public static void main(String[] args){
		DLList<Integer> list=new DLList<Integer>(5);
		list.insertFromHead(4);
		list.insertFromTail(3);
		list.insertFromHead(2);
		list.showNodes();
		System.out.println(list.search(4));
		System.out.println(list.find(2));
		list.deleteFromHead();
		list.deleteFromTail();
		list.showNodes();
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值