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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,235 +2,241 @@
/** /**
* 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 */ /**
private Link head; * Head refers to the front of the list
/** Tail refers to the back of the list */ */
private Link tail; private Link head;
/**
* Tail refers to the back of the list
*/
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
* @param array the array whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public DoublyLinkedList(int[] array){
if (array == null) throw new NullPointerException();
for (int i:array) {
insertTail(i);
}
}
/** /**
* Insert an element at the head * Constructs a list containing the elements of the array
* *
* @param x Element to be inserted * @param array the array whose elements are to be placed into this list
*/ * @throws NullPointerException if the specified collection is null
public void insertHead(int x){ */
Link newLink = new Link(x); //Create a new link with a value attached to it public DoublyLinkedList(int[] array) {
if(isEmpty()) //Set the first element added to be the tail if (array == null) throw new NullPointerException();
tail = newLink; for (int i : array) {
else insertTail(i);
head.previous = newLink; // newLink <-- currenthead(head) }
newLink.next = head; // newLink <--> currenthead(head) }
head = newLink; // newLink(head) <--> oldhead
}
/** /**
* Insert an element at the tail * Insert an element at the head
* *
* @param x Element to be inserted * @param x Element to be inserted
*/ */
public void insertTail(int x){ public void insertHead(int x) {
Link newLink = new Link(x); Link newLink = new Link(x); // Create a new link with a value attached to it
newLink.next = null; // currentTail(tail) newlink --> if (isEmpty()) // Set the first element added to be the tail
if(isEmpty()) { // Check if there are no elements in list then it adds first element tail = newLink;
tail=newLink; else
head=tail; head.previous = newLink; // newLink <-- currenthead(head)
} newLink.next = head; // newLink <--> currenthead(head)
else { head = newLink; // newLink(head) <--> oldhead
tail.next = newLink; // currentTail(tail) --> newLink --> }
newLink.previous = tail; // currentTail(tail) <--> newLink -->
tail = newLink; // oldTail <--> newLink(tail) -->
}
}
/** /**
* Delete the element at the head * Insert an element at the tail
* *
* @return The new head * @param x Element to be inserted
*/ */
public Link deleteHead(){ public void insertTail(int x) {
Link temp = head; Link newLink = new Link(x);
head = head.next; // oldHead <--> 2ndElement(head) newLink.next = null; // currentTail(tail) newlink -->
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed if (isEmpty()) { // Check if there are no elements in list then it adds first element
if(head == null) tail = newLink;
tail = null; head = tail;
return temp; } else {
} tail.next = newLink; // currentTail(tail) --> newLink -->
newLink.previous = tail; // currentTail(tail) <--> newLink -->
tail = newLink; // oldTail <--> newLink(tail) -->
}
}
/** /**
* Delete the element at the tail * Delete the element at the head
* *
* @return The new tail * @return The new head
*/ */
public Link deleteTail(){ public Link deleteHead() {
Link temp = tail; Link temp = head;
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null head = head.next; // oldHead <--> 2ndElement(head)
tail.next = null; // 2ndLast(tail) --> null head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
if(tail==null) if (head == null)
{ tail = null;
head=null; return temp;
} }
return temp;
}
/** /**
* Delete the element from somewhere in the list * Delete the element at the tail
* *
* @param x element to be deleted * @return The new tail
* @return Link deleted */
*/ public Link deleteTail() {
public void delete(int x){ Link temp = tail;
Link current = head; tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
tail.next = null; // 2ndLast(tail) --> null
if (tail == null) {
head = null;
}
return temp;
}
while(current.value != x) //Find the position to delete /**
current = current.next; * Delete the element from somewhere in the list
*
* @param x element to be deleted
* @return Link deleted
*/
public void delete(int x) {
Link current = head;
if(current == head) while (current.value != x) // Find the position to delete
deleteHead(); current = current.next;
else if(current == tail) if (current == head)
deleteTail(); deleteHead();
else{ //Before: 1 <--> 2(current) <--> 3 else if (current == tail)
current.previous.next = current.next; // 1 --> 3 deleteTail();
current.next.previous = current.previous; // 1 <--> 3
}
}
/** else { // Before: 1 <--> 2(current) <--> 3
* Inserts element and reorders current.previous.next = current.next; // 1 --> 3
* current.next.previous = current.previous; // 1 <--> 3
* @param x Element to be added }
*/ }
public void insertOrdered(int x){
Link newLink = new Link(x);
Link current = head;
while(current != null && x > current.value) //Find the position to insert
current = current.next;
if(current == head) /**
insertHead(x); * Inserts element and reorders
*
* @param x Element to be added
*/
public void insertOrdered(int x) {
Link newLink = new Link(x);
Link current = head;
while (current != null && x > current.value) // Find the position to insert
current = current.next;
else if(current == null) if (current == head)
insertTail(x); insertHead(x);
else{ //Before: 1 <--> 2(current) <--> 3 else if (current == null)
newLink.previous = current.previous; // 1 <-- newLink insertTail(x);
current.previous.next = newLink; // 1 <--> newLink
newLink.next = current; // 1 <--> newLink --> 2(current) <--> 3
current.previous = newLink; // 1 <--> newLink <--> 2(current) <--> 3
}
}
/** else { // Before: 1 <--> 2(current) <--> 3
* Returns true if list is empty newLink.previous = current.previous; // 1 <-- newLink
* current.previous.next = newLink; // 1 <--> newLink
* @return true if list is empty newLink.next = current; // 1 <--> newLink --> 2(current) <--> 3
*/ current.previous = newLink; // 1 <--> newLink <--> 2(current) <--> 3
public boolean isEmpty(){ }
return(head == null); }
}
/** /**
* Prints contents of the list * Returns true if list is empty
*/ *
public void display(){ //Prints contents of the list * @return true if list is empty
Link current = head; */
while(current!=null){ public boolean isEmpty() {
current.displayLink(); return (head == null);
current = current.next; }
}
System.out.println(); /**
} * Prints contents of the list
*/
public void display() { // Prints contents of the list
Link current = head;
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println();
}
} }
/** /**
* This class is used to implement the nodes of the * This class is used to implement the nodes of the
* linked list. * linked list.
*
* @author Unknown
* *
* @author Unknown
*/ */
class Link{ class Link {
/** Value of node */ /**
public int value; * Value of node
/** This points to the link in front of the new link */ */
public Link next; public int value;
/** This points to the link behind the new link */ /**
public Link previous; * This points to the link in front of the new link
*/
public Link next;
/**
* This points to the link behind the new link
*/
public Link previous;
/** /**
* Constructor * Constructor
* *
* @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 + " ");
} }
/** /**
* Main Method * Main Method
* *
* @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(7);
myList.insertHead(10);
myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) -->
myList.insertHead(13); myList.insertTail(11);
myList.insertHead(7); myList.display(); // <-- 10(head) <--> 7 <--> 13 <--> 11(tail) -->
myList.insertHead(10);
myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) -->
myList.insertTail(11); myList.deleteTail();
myList.display(); // <-- 10(head) <--> 7 <--> 13 <--> 11(tail) --> myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) -->
myList.deleteTail(); myList.delete(7);
myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) --> myList.display(); // <-- 10(head) <--> 13(tail) -->
myList.delete(7); myList.insertOrdered(23);
myList.display(); // <-- 10(head) <--> 13(tail) --> myList.insertOrdered(67);
myList.insertOrdered(3);
myList.insertOrdered(23); myList.display(); // <-- 3(head) <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
myList.insertOrdered(67); }
myList.insertOrdered(3);
myList.display(); // <-- 3(head) <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
}
} }