mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
@@ -20,12 +20,24 @@ class DoublyLinkedList{
|
||||
private Link tail;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Default Constructor
|
||||
*/
|
||||
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){
|
||||
if (array == null) throw new NullPointerException();
|
||||
for (int i:array) {
|
||||
insertTail(i);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert an element at the head
|
||||
@@ -60,13 +72,12 @@ class DoublyLinkedList{
|
||||
*
|
||||
* @return The new head
|
||||
*/
|
||||
public Link deleteHead(){
|
||||
public void 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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,11 +85,11 @@ class DoublyLinkedList{
|
||||
*
|
||||
* @return The new tail
|
||||
*/
|
||||
public Link deleteTail(){
|
||||
public void deleteTail(){
|
||||
Link temp = tail;
|
||||
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
|
||||
tail.next = null; // 2ndLast(tail) --> null
|
||||
return temp;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,7 +98,7 @@ class DoublyLinkedList{
|
||||
* @param x element to be deleted
|
||||
* @return Link deleted
|
||||
*/
|
||||
public Link delete(int x){
|
||||
public void delete(int x){
|
||||
Link current = head;
|
||||
|
||||
while(current.value != x) //Find the position to delete
|
||||
@@ -102,8 +113,7 @@ class DoublyLinkedList{
|
||||
else{ //Before: 1 <--> 2(current) <--> 3
|
||||
current.previous.next = current.next; // 1 --> 3
|
||||
current.next.previous = current.previous; // 1 <--> 3
|
||||
}
|
||||
return current;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,4 +221,4 @@ class Link{
|
||||
myList.insertOrdered(3);
|
||||
myList.display(); // <-- 3(head) <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
* This class implements a SinglyLinked List. This is done
|
||||
* using SinglyLinkedList class and a LinkForLinkedList Class.
|
||||
*
|
||||
* A linked list is implar to an array, it hold values.
|
||||
* A linked list is similar to an array, it hold 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 gorws and shrinks as it is edited. This is an example of
|
||||
* it grows and shrinks as it is edited. This is an example of
|
||||
* a singly linked list. Elements can only be added/removed
|
||||
* at the head/front of the list.
|
||||
*
|
||||
@@ -120,7 +120,7 @@ class SinglyLinkedList{
|
||||
|
||||
/**
|
||||
* This class is the nodes of the SinglyLinked List.
|
||||
* They consist of a vlue and a pointer to the node
|
||||
* They consist of a value and a pointer to the node
|
||||
* after them.
|
||||
*
|
||||
* @author Unknown
|
||||
|
||||
Reference in New Issue
Block a user