Update DoublyLinkedList.java
This commit is contained in:
parent
ece940b655
commit
14974872fd
@ -2,22 +2,25 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
@ -30,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
|
||||
*/
|
||||
@ -66,8 +70,7 @@ class DoublyLinkedList{
|
||||
if (isEmpty()) { // Check if there are no elements in list then it adds first element
|
||||
tail = newLink;
|
||||
head = tail;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
tail.next = newLink; // currentTail(tail) --> newLink -->
|
||||
newLink.previous = tail; // currentTail(tail) <--> newLink -->
|
||||
tail = newLink; // oldTail <--> newLink(tail) -->
|
||||
@ -97,8 +100,7 @@ class DoublyLinkedList{
|
||||
Link temp = tail;
|
||||
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
|
||||
tail.next = null; // 2ndLast(tail) --> null
|
||||
if(tail==null)
|
||||
{
|
||||
if (tail == null) {
|
||||
head = null;
|
||||
}
|
||||
return temp;
|
||||
@ -180,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;
|
||||
|
||||
/**
|
||||
@ -213,7 +220,6 @@ class Link{
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
DoublyLinkedList myList = new DoublyLinkedList();
|
||||
|
||||
myList.insertHead(13);
|
||||
myList.insertHead(7);
|
||||
myList.insertHead(10);
|
||||
|
Loading…
Reference in New Issue
Block a user