Update DoublyLinkedList.java

This commit is contained in:
Libin Yang
2019-03-23 15:43:38 +08:00
committed by GitHub
parent ece940b655
commit 14974872fd

View File

@ -2,22 +2,25 @@
/** /**
* This class implements a DoublyLinkedList. This is done using the classes * This class implements a DoublyLinkedList. This is done using the classes
* LinkedList and Link. * LinkedList and Link.
* * <p>
* A linked list is simplar to an array, it holds values. However, * A linked list is similar to an array, it holds values. However,
* links in a linked list do not have indees. With a linked list * links in a linked list do not have indexes. With a linked list
* you do not need to predetermine it's size as it grows and shrinks * you do not need to predetermine it's size as it grows and shrinks
* as it is edited. This is an example of a double ended, doubly * as it is edited. This is an example of a double ended, doubly
* linked list. Each link references the next link and the previous * linked list. Each link references the next link and the previous
* one. * one.
* *
* @author Unknown * @author Unknown
*
*/ */
class DoublyLinkedList { class DoublyLinkedList {
/** Head refers to the front of the list */ /**
* Head refers to the front of the list
*/
private Link head; private Link head;
/** Tail refers to the back of the list */ /**
* Tail refers to the back of the list
*/
private Link tail; private Link tail;
/** /**
@ -30,6 +33,7 @@ class DoublyLinkedList{
/** /**
* Constructs a list containing the elements of the array * Constructs a list containing the elements of the array
*
* @param array the array whose elements are to be placed into this list * @param array the array whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null * @throws NullPointerException if the specified collection is null
*/ */
@ -66,8 +70,7 @@ class DoublyLinkedList{
if (isEmpty()) { // Check if there are no elements in list then it adds first element if (isEmpty()) { // Check if there are no elements in list then it adds first element
tail = newLink; tail = newLink;
head = tail; head = tail;
} } else {
else {
tail.next = newLink; // currentTail(tail) --> newLink --> tail.next = newLink; // currentTail(tail) --> newLink -->
newLink.previous = tail; // currentTail(tail) <--> newLink --> newLink.previous = tail; // currentTail(tail) <--> newLink -->
tail = newLink; // oldTail <--> newLink(tail) --> tail = newLink; // oldTail <--> newLink(tail) -->
@ -97,8 +100,7 @@ class DoublyLinkedList{
Link temp = tail; Link temp = tail;
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
tail.next = null; // 2ndLast(tail) --> null tail.next = null; // 2ndLast(tail) --> null
if(tail==null) if (tail == null) {
{
head = null; head = null;
} }
return temp; return temp;
@ -180,14 +182,19 @@ class DoublyLinkedList{
* linked list. * linked list.
* *
* @author Unknown * @author Unknown
*
*/ */
class Link { class Link {
/** Value of node */ /**
* Value of node
*/
public int value; public int value;
/** This points to the link in front of the new link */ /**
* This points to the link in front of the new link
*/
public Link next; public Link next;
/** This points to the link behind the new link */ /**
* This points to the link behind the new link
*/
public Link previous; public Link previous;
/** /**
@ -213,7 +220,6 @@ class Link{
*/ */
public static void main(String args[]) { public static void main(String args[]) {
DoublyLinkedList myList = new DoublyLinkedList(); DoublyLinkedList myList = new DoublyLinkedList();
myList.insertHead(13); myList.insertHead(13);
myList.insertHead(7); myList.insertHead(7);
myList.insertHead(10); myList.insertHead(10);