Merge pull request #718 from PrathamGupta/patch-1

Updated DoublyLinkedList.java
This commit is contained in:
Libin Yang
2019-03-23 15:44:04 +08:00
committed by GitHub

View File

@ -1,22 +1,26 @@
/**
* This class implements a DoublyLinkedList. This is done using the classes
* LinkedList and Link.
*
* A linked list is simplar to an array, it holds values. However,
* links in a linked list do not have indees. With a linked list
* <p>
* A linked list is similar to an array, it holds values. However,
* 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
* as it is edited. This is an example of a double ended, doubly
* linked list. Each link references the next link and the previous
* one.
*
* @author Unknown
*
*/
class DoublyLinkedList {
/** Head refers to the front of the list */
/**
* Head refers to the front of the list
*/
private Link head;
/** Tail refers to the back of the list */
/**
* Tail refers to the back of the list
*/
private Link tail;
/**
@ -29,6 +33,7 @@ class DoublyLinkedList{
/**
* Constructs a list containing the elements of the array
*
* @param array the array whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
@ -62,22 +67,28 @@ class DoublyLinkedList{
public void insertTail(int x) {
Link newLink = new Link(x);
newLink.next = null; // currentTail(tail) newlink -->
if (isEmpty()) { // Check if there are no elements in list then it adds first element
tail = newLink;
head = tail;
} else {
tail.next = newLink; // currentTail(tail) --> newLink -->
newLink.previous = tail; // currentTail(tail) <--> newLink -->
tail = newLink; // oldTail <--> newLink(tail) -->
}
}
/**
* Delete the element at the head
*
* @return The new head
*/
public void deleteHead(){
public Link deleteHead() {
Link temp = head;
head = head.next; // oldHead <--> 2ndElement(head)
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
if (head == null)
tail = null;
return temp;
}
/**
@ -85,11 +96,14 @@ class DoublyLinkedList{
*
* @return The new tail
*/
public void deleteTail(){
public Link deleteTail() {
Link temp = tail;
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
tail.next = null; // 2ndLast(tail) --> null
if (tail == null) {
head = null;
}
return temp;
}
/**
@ -168,14 +182,19 @@ class DoublyLinkedList{
* linked list.
*
* @author Unknown
*
*/
class Link {
/** Value of node */
/**
* Value of node
*/
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;
/** This points to the link behind the new link */
/**
* This points to the link behind the new link
*/
public Link previous;
/**
@ -201,7 +220,6 @@ class Link{
*/
public static void main(String args[]) {
DoublyLinkedList myList = new DoublyLinkedList();
myList.insertHead(13);
myList.insertHead(7);
myList.insertHead(10);