java中的双链列表

时间:2020-02-23 14:34:47  来源:igfitidea点击:

在本教程中,我们将看到Java中的双链接列表实现。
我们已经看到了单独的列表的实施。

我们可以将其视为单独链接列表的扩展名。
与单独的列表相比,实施它非常复杂。
在双链表中,节点具有到下一个节点和上一个节点的数据和指针。
第一个节点之前的NULL和上一个节点的下一个点的指向NULL,因此我们可以使用以下下一个指向指针迭代链接列表。
双链接列表的一个例子:

双链接列表的节点可以如下所示:

class Node {
	public int data;
	public Node next;
	public Node prev;
 
	public void displayNodeData() {
		System.out.println("{ " + data + " } ");
	}
}

如我们所见,在双链接列表的情况下,我们有一个更另外的参考(节点上面)。
假设,在双链接列表的情况下,我们要做的第一个操作,它看起来如下:

Java示例中的双链表列表

让我们在Java中实现双倍链接的列表。

创建一个名为doublylinkedlist.java的Java文件。

package org.arpit.theitroad.algo;
 
class Node {
	public int data;
	public Node next;
	public Node prev;
 
	public void displayNodeData() {
		System.out.println("{ " + data + " } ");
	}
}
 
public class DoublyLinkedList {
 
	private Node head;
	private Node tail;
	int size;
 
	public boolean isEmpty() {
		return (head == null);
	}
 
	//used to insert a node at the start of linked list
	public void insertFirst(int data) {
		Node newNode = new Node();
		newNode.data = data;
		newNode.next = head;
		newNode.prev=null;
		if(head!=null)
			head.prev=newNode;
		head = newNode;
		if(tail==null)
			tail=newNode;
		size++;
	}
 
	//used to insert a node at the start of linked list
	public void insertLast(int data) {
		Node newNode = new Node();
		newNode.data = data;
		newNode.next = null;
		newNode.prev=tail;
		if(tail!=null)
			tail.next=newNode;
		tail = newNode;
		if(head==null)
			head=newNode;
		size++;
	}
	//used to delete node from start of Doubly linked list
	public Node deleteFirst() {
 
		if (size == 0) 
			throw new RuntimeException("Doubly linked list is already empty");
		Node temp = head;
		head = head.next;
		head.prev = null;
		size--;
		return temp;
	}
 
	//used to delete node from last of Doubly linked list
	public Node deleteLast() {
 
		Node temp = tail;
		tail = tail.prev;
		tail.next=null;
		size--;
		return temp;
	}
 
 
	//Use to delete node after particular node
	public void deleteAfter(Node after) {
		Node temp = head;
		while (temp.next != null && temp.data != after.data) {
			temp = temp.next;
		}
		if (temp.next != null)
			temp.next.next.prev=temp;
		temp.next = temp.next.next;
 
	}
 
	//For printing Doubly Linked List forward
	public void printLinkedListForward() {
		System.out.println("Printing Doubly LinkedList (head --> tail) ");
		Node current = head;
		while (current != null) {
			current.displayNodeData();
			current = current.next;
		}
		System.out.println();
	}
 
	//For printing Doubly Linked List forward
	public void printLinkedListBackward() {
		System.out.println("Printing Doubly LinkedList (tail --> head) ");
		Node current = tail;
		while (current != null) {
			current.displayNodeData();
			current = current.prev;
		}
		System.out.println();
	}
}

创建另一个名为doublelinkedlistmain的类:

package org.arpit.theitroad.algo;
 
public class DoubleLinkedListMain {
 
 public static void main(String args[])
 {
   DoublyLinkedList myLinkedlist = new DoublyLinkedList();
   myLinkedlist.insertFirst(5);
   myLinkedlist.insertFirst(6);
   myLinkedlist.insertFirst(7);
   myLinkedlist.insertFirst(1);
   myLinkedlist.insertLast(2);
   myLinkedlist.printLinkedListForward();
   myLinkedlist.printLinkedListBackward();
   
   System.out.println("================");
   //Doubly Linked list will be 
   //1 ->  7 -> 6 -> 5 -> 2
   
   Node node=new Node();
   node.data=1;
   myLinkedlist.deleteAfter(node);
   myLinkedlist.printLinkedListForward();
   myLinkedlist.printLinkedListBackward();
   //After deleting node after 1,doubly Linked list will be 
   //2 -> 1 -> 6 -> 5
   System.out.println("================");
   myLinkedlist.deleteFirst();
   myLinkedlist.deleteLast();
   
   //After performing above operation, doubly Linked list will be 
   // 6 -> 5
   myLinkedlist.printLinkedListForward();
   myLinkedlist.printLinkedListBackward();
 }
}

运行上面的程序时,我们将得到以下输出:

Printing Doubly LinkedList (head -> tail)
{ 1 }
{ 7 }
{ 6 }
{ 5 }
{ 2 }Printing Doubly LinkedList (tail -> head)
{ 2 }
{ 5 }
{ 6 }
{ 7 }
{ 1 }================
Printing Doubly LinkedList (head -> tail)
{ 1 }
{ 6 }
{ 5 }
{ 2 }