双端链表、双向链表(java)

本文详细介绍了Java中双端链表和双向链表的概念,并提供了相应的测试代码,包括双端链表的创建、操作及双向链表的实现与测试。
摘要由CSDN通过智能技术生成

1、双端链表
2、测试双端链表
3、双向链表
4、测试双向链表

1、双端链表

package com.cwq.ch05;

import com.cwq.ch04.Node;

/** 
 * 双端链表,将数据串起来
 * @author Carpoor  
 * @date 2019年1月29日 
 */
public class MyFirstLastLinkedList {

	private Node first;
	private Node last;
	
	public MyFirstLastLinkedList() {
		first = null;
	}
	
	/**
	 * 插入数据,向队头插入
	 */
	public void insertFirst(long value) {
		Node node = new Node(value);
		if (isEmpty()) {
			last = node;
		}
		node.next = first;
		first = node;
	}
	
	/**
	 * 插入数据,向队尾插入
	 */
	public void insertLast(long value) {
		Node node = new Node(value);
		if (isEmpty()) {
			first = node;
		} else {
			last.next = node;
		}
		last = node;
	}
	
	/**
	 * 删除数据,删队头
	 */
	public Node deleteFirst() {
		Node tmp = first;
		if (tmp.next == null) {
			last = null;
		}
		first = first.next;
		return tmp;
	}
	
	/**
	 * 展示数据
	 */
	public void display() {
		Node current = first;
		while (current != null) {
			current.display();
			current = current.next;
		}
		System.out.println();
	}
	
	/**
	 * 查找数据
	 */
	public Node find(long value) {
		Node current = first;
		while (current.data != value) {
			if (current.next == null) {
				return null;
			}
			current = current.next;
		}
		return current;
	}
	
	/**
	 * 删除数据
	 */
	public Node delete(long value) {
		Node current = first;
		Node prev = first;
		while (current.data != value) {
			if (current.next == null) {
				return null;
			}
			prev = current;
			current = current.next;
		}
		if (current == first) {
			first = first.next;
		} else {
			prev.next = current.next;
		}
		
		return current;
	}
	
	public boolean isEmpty() {
		return first == null;
	}
}

2、测试双端链表

package com.cwq.ch05;

/** 
 * @author Carpoor  
 * @date 2019年1月30日 
 */
public class TestFirstLastMyLinkedList {

	public static void main(String[] args) {
		MyFirstLastLinkedList list = new MyFirstLastLinkedList();
		list.insertFirst(50);
		list.insertFirst(40);
		list.insertFirst(30);
		list.insertLast(50);
		list.insertLast(40);
		list.insertLast(30);
		list.display();
		
		while(!list.isEmpty()) {
			list.deleteFirst();
			list.display();
		}
		
	}
	
}

3、双向链表

package com.cwq.ch05;

/** 
 * 双端链表,将数据串起来
 * @author Carpoor  
 * @date 2019年1月29日 
 */
public class MyDoubleLinkedList {

	private Node first;
	private Node last;
	
	public MyDoubleLinkedList() {
		first = null;
	}
	
	/**
	 * 插入数据,向队头插入
	 */
	public void insertFirst(long value) {
		Node node = new Node(value);
		if (isEmpty()) {
			last = node;
		}else {
			first.prev = node;
		}
		node.next = first;
		first = node;
	}
	
	/**
	 * 插入数据,向队尾插入
	 */
	public void insertLast(long value) {
		Node node = new Node(value);
		if (isEmpty()) {
			first = node;
		} else {
			last.next = node;
			node.prev = last;
		}
		last = node;
	}
	
	/**
	 * 删除数据,删队头
	 */
	public Node deleteFirst() {
		Node tmp = first;
		if (tmp.next == null) {
			last = null;
		}else {
			first.next.prev = null;
		}
		first = first.next;
		return tmp;
	}
	
	/**
	 * 删除数据,删队尾
	 */
	public Node deleteLast() {
		Node tmp = last;
		if (tmp.prev == null) {
			first = null;
		}else {
			tmp.prev.next = null;
		}
		last = last.prev;
		return tmp;
	}
	
	/**
	 * 展示数据
	 */
	public void display() {
		Node current = first;
		while (current != null) {
			current.display();
			current = current.next;
		}
		System.out.println();
	}
	
	/**
	 * 查找数据
	 */
	public Node find(long value) {
		Node current = first;
		while (current.data != value) {
			if (current.next == null) {
				return null;
			}
			current = current.next;
		}
		return current;
	}
	
	/**
	 * 删除数据
	 */
	public Node delete(long value) {
		Node current = first;
		Node prev = first;
		while (current.data != value) {
			if (current.next == null) {
				return null;
			}
			prev = current;
			current = current.next;
		}
		if (current == first) {
			first = first.next;
		} else {
			prev.next = current.next;
		}
		
		return current;
	}
	
	public boolean isEmpty() {
		return first == null;
	}
}

4、测试双向链表

package com.cwq.ch05;

/** 
 * @author Carpoor  
 * @date 2019年1月31日 
 */
public class TestMyDoubleLinkedList {

	public static void main(String[] args) {
		MyDoubleLinkedList list = new MyDoubleLinkedList();
		list.insertFirst(50);
		list.insertFirst(40);
		list.insertFirst(30);
		list.insertLast(50);
		list.insertLast(40);
		list.insertLast(30);
		list.display();
		
		while(!list.isEmpty()) {
			list.deleteFirst();
			list.display();
		}
		System.out.println("------------------------------------");
		list.insertFirst(50);
		list.insertFirst(40);
		list.insertFirst(30);
		list.insertLast(50);
		list.insertLast(40);
		list.insertLast(30);
		list.display();
		
		while(!list.isEmpty()) {
			list.deleteLast();
			list.display();
		}
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值