Skip to content

Updated DoublyLinkedList.java #718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Updated DoublyLinkedList.java
Changes made in Insert and Delete functions.
  • Loading branch information
PrathamGupta authored Mar 16, 2019
commit ece940b655a2af1992a8851d3ed2b694879c22ae
24 changes: 18 additions & 6 deletions DataStructures/Lists/DoublyLinkedList.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/**
* This class implements a DoublyLinkedList. This is done using the classes
* LinkedList and Link.
Expand Down Expand Up @@ -62,34 +63,45 @@ public void insertHead(int x){
public void insertTail(int x){
Link newLink = new Link(x);
newLink.next = null; // currentTail(tail) newlink -->
tail.next = newLink; // currentTail(tail) --> newLink -->
newLink.previous = tail; // currentTail(tail) <--> newLink -->
tail = newLink; // oldTail <--> newLink(tail) -->
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) -->
}
}

/**
* Delete the element at the head
*
* @return The new head
*/
public void 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)
tail = null;
return temp;
}

/**
* Delete the element at the tail
*
* @return The new tail
*/
public void 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;
}
return temp;
}

/**
Expand Down