X Tutup
The Wayback Machine - https://web.archive.org/web/20200805201600/https://github.com/TheAlgorithms/Java/pull/1379/files
Skip to content
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

Add clearAll and index based insertion in DoublyLinkedList and fixed bugs #1379

Open
wants to merge 2 commits into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -112,6 +112,22 @@ public Link deleteTail() {

return temp;
}
/**
* Delete all the elements in the list
*
*
*
*/
public void clearList(){
Link temp = head;
while(temp != null){
Link next = temp.next;// Keep the next element as a reference
temp.previous = temp.next = null;// Delete the the next and previous pointers
temp = next;// Move to the next node
}
head = tail = temp = null;// Update the head and the tail with the new values

}

/**
* Delete the element from somewhere in the list
@@ -166,6 +182,38 @@ else if (current == null)
current.previous = newLink; // 1 <--> newLink <--> 2(current) <--> 3
}
}
/**
* Add the element int the position "index" (0 based)
*
* @param index the position of the element to be added
* @param data the value to the element to be added
*/

public void addIn(int index,int data) { // index is 0 based
if(index < 0){
System.out.println("non valid index!");
return;
}
if(index == 0){
insertHead(data);
return;
}

Link temp = head;
for(int i = 0 ; i < index - 1 ; i++ ){
temp = (temp != null) ? temp.next : tail;
}
if(temp == tail ) {
insertTail(data);
}else{
Link newLink = new Link(data);
newLink.next = temp.next;
newLink.previous = temp;
temp.next.previous = newLink;
temp.next = newLink;
}

}

/**
* Deletes the passed node from the current list
@@ -178,22 +226,22 @@ public void deleteNode(Link z) {
} else if(z == head){
deleteHead();
} else{ //before <-- 1 <--> 2(z) <--> 3 -->
z.previous.next = z.next // 1 --> 3
z.next.previous = z.previous // 1 <--> 3
z.previous.next = z.next; // 1 --> 3
z.next.previous = z.previous; // 1 <--> 3
}
}

public static void removeDuplicates(DoublyLinkedList l ) {
Link linkOne = l.head ;
while(linkOne.next != null) { // list is present
Link linkTwo = linkOne.next; // second link for comparison
while(linkTwo.next!= null) {
if(linkOne.value == linkTwo.value) // if there are duplicates values then
l.delete(linkTwo.value); // delete the link
linkTwo = linkTwo.next ; // go to next link
}
linkOne = linkOne.next; // go to link link to iterate the whole list again
}
Link linkOne = l.head ;
while(linkOne.next != null) { // list is present
Link linkTwo = linkOne.next; // second link for comparison
while(linkTwo.next!= null) {
if(linkOne.value == linkTwo.value) // if there are duplicates values then
l.delete(linkTwo.value); // delete the link
linkTwo = linkTwo.next ; // go to next link
}
linkOne = linkOne.next; // go to link link to iterate the whole list again
}
}

/**
@@ -210,12 +258,15 @@ public boolean isEmpty() {
*/
public void display() { // Prints contents of the list
Link current = head;
if(isEmpty()) System.out.println("Empty list!");// Check if list is empty
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println();
}


}

/**
@@ -279,5 +330,8 @@ public static void main(String args[]) {
myList.insertOrdered(67);
myList.insertOrdered(3);
myList.display(); // <-- 3(head) <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
myList.addIn(9,18);
myList.display();// <-- 3(head) <--> 10 <--> 13 <--> 23 <--> 67 <--> 18 (tail) -->
}

}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.
X Tutup