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
* 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 */
class DoublyLinkedList {
/**
* 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;
/**
* Default Constructor
*/
public DoublyLinkedList(){
public DoublyLinkedList() {
head = 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){
public DoublyLinkedList(int[] array) {
if (array == null) throw new NullPointerException();
for (int i:array) {
for (int i : array) {
insertTail(i);
}
}
@ -45,9 +49,9 @@ class DoublyLinkedList{
*
* @param x Element to be inserted
*/
public void insertHead(int x){
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
public void insertHead(int x) {
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
tail = newLink;
else
head.previous = newLink; // newLink <-- currenthead(head)
@ -60,14 +64,13 @@ class DoublyLinkedList{
*
* @param x Element to be inserted
*/
public void insertTail(int x){
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 {
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) -->
@ -79,11 +82,11 @@ class DoublyLinkedList{
*
* @return The new head
*/
public Link 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)
if (head == null)
tail = null;
return temp;
}
@ -93,13 +96,12 @@ class DoublyLinkedList{
*
* @return The new tail
*/
public Link 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;
if (tail == null) {
head = null;
}
return temp;
}
@ -110,19 +112,19 @@ class DoublyLinkedList{
* @param x element to be deleted
* @return Link deleted
*/
public void delete(int x){
public void delete(int x) {
Link current = head;
while(current.value != x) //Find the position to delete
while (current.value != x) // Find the position to delete
current = current.next;
if(current == head)
if (current == head)
deleteHead();
else if(current == tail)
else if (current == tail)
deleteTail();
else{ //Before: 1 <--> 2(current) <--> 3
else { // Before: 1 <--> 2(current) <--> 3
current.previous.next = current.next; // 1 --> 3
current.next.previous = current.previous; // 1 <--> 3
}
@ -133,19 +135,19 @@ class DoublyLinkedList{
*
* @param x Element to be added
*/
public void insertOrdered(int x){
public void insertOrdered(int x) {
Link newLink = new Link(x);
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;
if(current == head)
if (current == head)
insertHead(x);
else if(current == null)
else if (current == null)
insertTail(x);
else{ //Before: 1 <--> 2(current) <--> 3
else { // Before: 1 <--> 2(current) <--> 3
newLink.previous = current.previous; // 1 <-- newLink
current.previous.next = newLink; // 1 <--> newLink
newLink.next = current; // 1 <--> newLink --> 2(current) <--> 3
@ -158,16 +160,16 @@ class DoublyLinkedList{
*
* @return true if list is empty
*/
public boolean isEmpty(){
return(head == null);
public boolean isEmpty() {
return (head == null);
}
/**
* Prints contents of the list
*/
public void display(){ //Prints contents of the list
public void display() { // Prints contents of the list
Link current = head;
while(current!=null){
while (current != null) {
current.displayLink();
current = current.next;
}
@ -180,14 +182,19 @@ class DoublyLinkedList{
* linked list.
*
* @author Unknown
*
*/
class Link{
/** Value of node */
class Link {
/**
* 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;
/**
@ -195,15 +202,15 @@ class Link{
*
* @param value Value of node
*/
public Link(int value){
public Link(int value) {
this.value = value;
}
/**
* Displays the node
*/
public void displayLink(){
System.out.print(value+" ");
public void displayLink() {
System.out.print(value + " ");
}
/**
@ -211,9 +218,8 @@ class Link{
*
* @param args Command line arguments
*/
public static void main(String args[]){
public static void main(String args[]) {
DoublyLinkedList myList = new DoublyLinkedList();
myList.insertHead(13);
myList.insertHead(7);
myList.insertHead(10);