自定义链表(java)

链表(java)

    • 单链表
    • 双向链表

单链表

package com.lexie.linked;

public class Node {
	// 数据域
	public Object data;
	// 指针域
	public Node next;

	public Node(Object value) {
		this.data = value;
	}

	// 显示
	public void display() {
		System.out.println(data + " ");
	}

}

package com.lexie.linked;

public class SingleLinkedList {
	// 链表结点的个数
	private int size;
	// 头结点
	private Node head;

	public SingleLinkedList() {
		size = 0;
		head = null;
	}

	public Object addHead(Object obj) {
		Node newHead = new Node(obj);
		if (size == 0) {
			head = newHead;
		} else {
			newHead.next = head;
			head = newHead;
		}
		size++;
		return obj;
	}

	public Object deleteHead() {
		Object obj = head.data;
		head = head.next;
		size--;
		return obj;
	}

	public Node find(Object obj) {
		Node current = head;
		int tempSize = size;
		while (tempSize > 0) {
			if (obj.equals(current.data)) {
				return current;
			} else {
				current = current.next;
			}
			tempSize--;
		}
		return null;
	}

	public boolean delete(Object value) {
		if (size == 0) {
			return false;
		}
		Node current = head;
		Node previous = head;
		while (current.data != value) {
			if (current.next == null) {
				return false;
			} else {
				previous = current;
				current = current.next;
			}

		}
		if (current == head) {
			head = current.next;
		} else {
			previous.next = current.next;
			size--;
		}
		return true;
	}

	public boolean isEmpty() {
		return (size == 0);
	}

	public void display() {
		if (size > 0) {
			Node node = head;
			int tempSize = size;
			if (tempSize == 1) {
				System.out.println("[" + node.data + "]");
			}
			while (tempSize > 0) {
				if (node.equals(head)) {
					System.out.print("[" + node.data + "->");
				} else if (node.next == null) {
					System.out.print(node.data + "]");
				} else {
					System.out.print(node.data + "->");
				}
				node = node.next;
				tempSize--;
			}
			System.out.println();
		} else {
			System.out.println("[]");
		}
	}

}

双向链表

package com.lexie.linked;

public class TwoWayLinkedList {
	private class Node{
		private Object data;
		private Node next;
		private Node prev;
		public Node(Object data){
			this.data=data;
		}
		
	}
	private Node head;
	private Node tail;
	private int size;
	public TwoWayLinkedList(){
		size=0;
		head=null;
		tail=null;
	}
	public void addHead(Object value){
		Node newNode=new Node(value);
		if (size==0) {
			head=newNode;
			tail=newNode;
			size++;
		}else{
			head.prev=newNode;
			newNode.next=head;
			head=newNode;
			size++;
		}
	}
	public void addTail(Object value){
		Node newNode=new Node(value);
		if (size==0) {
			newNode.prev=tail;
			tail.next=newNode;
			tail=newNode;
			size++;
		}
	}
	public Node deleteHead(){
		Node temp=head;
		if (size!=0) {
			head=head.next;
			head.prev=null;
			size--;
		}
		return temp;
	}
	public int getSize(){
		return size;
	}
	public boolean isEmpty(){
		return (size==0);
	}
	public void display(){
		 if(size >0){
	            Node node = head;
	            int tempSize = size;
	            if(tempSize == 1){//当前链表只有一个节点
	                System.out.println("["+node.data+"]");
	                return;
	            }
	            while(tempSize>0){
	                if(node.equals(head)){
	                    System.out.print("["+node.data+"->");
	                }else if(node.next == null){
	                    System.out.print(node.data+"]");
	                }else{
	                    System.out.print(node.data+"->");
	                }
	                node = node.next;
	                tempSize--;
	            }
	            System.out.println();
	        }else{//如果链表一个节点都没有,直接打印[]
	            System.out.println("[]");
	        }

	}

}

你可能感兴趣的:(数据结构,链表)