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,40 +2,44 @@
/** /**
* 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;
/** /**
* Default Constructor * Default Constructor
*/ */
public DoublyLinkedList(){ public DoublyLinkedList() {
head = null; head = null;
tail = null; tail = null;
} }
/** /**
* 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
*/ */
public DoublyLinkedList(int[] array){ public DoublyLinkedList(int[] array) {
if (array == null) throw new NullPointerException(); if (array == null) throw new NullPointerException();
for (int i:array) { for (int i : array) {
insertTail(i); insertTail(i);
} }
} }
@ -45,9 +49,9 @@ class DoublyLinkedList{
* *
* @param x Element to be inserted * @param x Element to be inserted
*/ */
public void insertHead(int x){ public void insertHead(int x) {
Link newLink = new Link(x); //Create a new link with a value attached to it Link newLink = new Link(x); // Create a new link with a value attached to it
if(isEmpty()) //Set the first element added to be the tail if (isEmpty()) // Set the first element added to be the tail
tail = newLink; tail = newLink;
else else
head.previous = newLink; // newLink <-- currenthead(head) head.previous = newLink; // newLink <-- currenthead(head)
@ -60,14 +64,13 @@ class DoublyLinkedList{
* *
* @param x Element to be inserted * @param x Element to be inserted
*/ */
public void insertTail(int x){ public void insertTail(int x) {
Link newLink = new Link(x); Link newLink = new Link(x);
newLink.next = null; // currentTail(tail) newlink --> newLink.next = null; // currentTail(tail) newlink -->
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) -->
@ -79,11 +82,11 @@ class DoublyLinkedList{
* *
* @return The new head * @return The new head
*/ */
public Link deleteHead(){ public Link deleteHead() {
Link temp = head; Link temp = head;
head = head.next; // oldHead <--> 2ndElement(head) head = head.next; // oldHead <--> 2ndElement(head)
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
if(head == null) if (head == null)
tail = null; tail = null;
return temp; return temp;
} }
@ -93,13 +96,12 @@ class DoublyLinkedList{
* *
* @return The new tail * @return The new tail
*/ */
public Link deleteTail(){ public Link deleteTail() {
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;
} }
@ -110,19 +112,19 @@ class DoublyLinkedList{
* @param x element to be deleted * @param x element to be deleted
* @return Link deleted * @return Link deleted
*/ */
public void delete(int x){ public void delete(int x) {
Link current = head; Link current = head;
while(current.value != x) //Find the position to delete while (current.value != x) // Find the position to delete
current = current.next; current = current.next;
if(current == head) if (current == head)
deleteHead(); deleteHead();
else if(current == tail) else if (current == tail)
deleteTail(); deleteTail();
else{ //Before: 1 <--> 2(current) <--> 3 else { // Before: 1 <--> 2(current) <--> 3
current.previous.next = current.next; // 1 --> 3 current.previous.next = current.next; // 1 --> 3
current.next.previous = current.previous; // 1 <--> 3 current.next.previous = current.previous; // 1 <--> 3
} }
@ -133,19 +135,19 @@ class DoublyLinkedList{
* *
* @param x Element to be added * @param x Element to be added
*/ */
public void insertOrdered(int x){ public void insertOrdered(int x) {
Link newLink = new Link(x); Link newLink = new Link(x);
Link current = head; Link current = head;
while(current != null && x > current.value) //Find the position to insert while (current != null && x > current.value) // Find the position to insert
current = current.next; current = current.next;
if(current == head) if (current == head)
insertHead(x); insertHead(x);
else if(current == null) else if (current == null)
insertTail(x); insertTail(x);
else{ //Before: 1 <--> 2(current) <--> 3 else { // Before: 1 <--> 2(current) <--> 3
newLink.previous = current.previous; // 1 <-- newLink newLink.previous = current.previous; // 1 <-- newLink
current.previous.next = newLink; // 1 <--> newLink current.previous.next = newLink; // 1 <--> newLink
newLink.next = current; // 1 <--> newLink --> 2(current) <--> 3 newLink.next = current; // 1 <--> newLink --> 2(current) <--> 3
@ -158,16 +160,16 @@ class DoublyLinkedList{
* *
* @return true if list is empty * @return true if list is empty
*/ */
public boolean isEmpty(){ public boolean isEmpty() {
return(head == null); return (head == null);
} }
/** /**
* Prints contents of the list * Prints contents of the list
*/ */
public void display(){ //Prints contents of the list public void display() { // Prints contents of the list
Link current = head; Link current = head;
while(current!=null){ while (current != null) {
current.displayLink(); current.displayLink();
current = current.next; current = current.next;
} }
@ -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;
/** /**
@ -195,15 +202,15 @@ class Link{
* *
* @param value Value of node * @param value Value of node
*/ */
public Link(int value){ public Link(int value) {
this.value = value; this.value = value;
} }
/** /**
* Displays the node * Displays the node
*/ */
public void displayLink(){ public void displayLink() {
System.out.print(value+" "); System.out.print(value + " ");
} }
/** /**
@ -211,9 +218,8 @@ class Link{
* *
* @param args Command line arguments * @param args Command line arguments
*/ */
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);