Skip to content
Open
Changes from all commits
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
148 changes: 111 additions & 37 deletions DoublyLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,17 @@ public int getLength(){
public void insertAtHead(int value){
DllNode node = new DllNode(value,null,null);

//if our list is null
if(length == 0){
head.setNext(node);
node.setPrev(head);

tail.setPrev(node);
node.setNext(tail);
length += 1;
length++;
return;
}
//if we have more than one element in list.
else{
node.setNext(head.getNext());
node.setPrev(head);
Expand All @@ -97,54 +99,42 @@ public void insertAtHead(int value){
}
public void insertAt(int data , int position){


if(position < 0){
position = 0;
}
if(position > length){
position = length;
}


DllNode node , temp = head;

if(head == null){
head = new DllNode(data);
tail.setPrev(head);
head.setNext(tail);
}

//when want to insert at head
if(position == 0){

insertAtHead(data);

return;
}
else if(position == length){

insertAtTail(data);

}
else{

for(int i = 1 ; i <= position ; i++){
//when we want to insert at tail
if(position == length){
insertAtTail(data);
return;
}
DllNode node , temp = head.getNext();
for(int i = 1 ; i < position ; i++){

temp = temp.getNext();

}



System.out.println("temp data : " + temp.getData());
//we have reached a position before now.

node = new DllNode(data,null,null);

node.setNext(temp.getNext());
node.setPrev(temp);

temp.getNext().setPrev(node);
temp.setNext(node);


length += 1;
}




Expand All @@ -165,18 +155,97 @@ public void insertAtTail(int data){
}

}
// removing the first element
public int removeFirst(){
//if no element in the list
if(length == 0){
return Integer.MIN_VALUE;
}
DllNode temp = head.getNext();
head.setNext(temp.getNext());
temp.getNext().setPrev(head);
length -= 1;
return temp.getData();

}
//removing last element
public int removeLast(){
//if no element in list
if(length == 0){
return Integer.MIN_VALUE;
}
DllNode temp = tail.getPrev();
tail.setPrev(temp.getPrev());
temp.getPrev().setNext(tail);
length -= 1;
return temp.getData();

}

public int removeFirst(){return 0;}
public int removeLast(){return 0;}
public synchronized void removeMatched(DllNode node){}

public synchronized void removeAtPosition(int position){
if(position < 0){
position = 0;
}
//if position greater than the no of elements starting from 0
if(position > length - 1){
position = length - 1;
}
// to remove first element
if(position == 0){
removeFirst();
return;
}
//to remove last element
if(position == (length - 1)){
removeLast();
return;
}
//else to remove in between element
DllNode temp = head.getNext();
for(int i = 1 ; i < position ; i++){
temp = temp.getNext();
}



temp.setNext(temp.getNext().getNext()); // since we reach one element before actual position , we need to set the next of current node to
//next of actual position node , so we skip the actual position node and reach its next node.
temp.getNext().setPrev(temp); // now setting prev of node after position node to point to node before position node.
length -= 1;
return;

}


public synchronized void removeMatched(DllNode node){

if(length == 0){
return;
}
DllNode temp = head.getNext();
int pos = 0;
while(pos < length){
if(node.equals(temp)){
temp.getPrev().setNext(temp.getNext());
temp.getNext().setPrev(temp.getPrev());
length -= 1;

}
pos += 1;
}


}
public String toString(){

String result = "[";
DllNode node = head.getNext();

for(int i = 0 ; i < length ; i++){
System.out.println("Node data at " + i +" is " + node.getData());
result += node.getData();
if( i != (length - 1)){
if(i < length - 1){
result += ",";
}
node = node.getNext();
Expand All @@ -192,26 +261,31 @@ public static void main(String[] args){
DoublyLinkedList dll = new DoublyLinkedList();
dll.insertAtHead(5);
System.out.println(dll.toString());
System.out.println("list Length " + dll.getLength());

dll.insertAtHead(15);
System.out.println(dll.toString());
System.out.println("list Length " + dll.getLength());

dll.insertAtTail(25);
System.out.println(dll.toString());

dll.insertAt(35,1);

dll.removeAtPosition(2);
System.out.println(dll.toString());


dll.insertAt(45,dll.getLength());
System.out.println(dll.toString());


// dll.removeFirst();
// System.out.println(dll.toString());


// dll.removeLast();
// System.out.println(dll.toString());


// dll.insertAt(35,1);
// System.out.println(dll.toString());

dll.insertAt(65,dll.getLength() - 1);
System.out.println(dll.toString());
}

}