Monday, January 25, 2016

Doubly linked Implementaions In Java

package LinkedList;

public class DLLNode {
private int data;
DLLNode prev;
DLLNode next;

public DLLNode(int data){
this.data = data;
prev  = null;
next = null;
}

public DLLNode(int data,DLLNode prev,DLLNode next){
this.data = data;
this.prev = prev;
this.next = next;
}

public int getData(){
return data;
}

public void setData(int data){
this.data = data;
}

public DLLNode getPrev(){
return prev;
}

public void setPrev(DLLNode where){
prev = where;
}

public DLLNode getNext(){
return next;
}

public void setNext(DLLNode where){
next = where;
}

}


package LinkedList;

public class DoublyLinkedList {
private DLLNode head;
private DLLNode tail;
private int length;

public DoublyLinkedList() {
head = new DLLNode(90, null, null);
tail = new DLLNode(Integer.MAX_VALUE, head, null);
head.setNext(tail);
length = 0;
}

public int getPosition(int data) {
DLLNode temp = head;
int pos = 0;
while (temp != null) {
if (temp.getData() == data) {
return pos;
}
pos += 1;
temp = temp.getNext();
}
return Integer.MIN_VALUE;
}

public int length() {
return length;
}

public void insert(int data) {
DLLNode newNode = new DLLNode(data, head, head.getNext());
newNode.getNext().setPrev(newNode);
head.setNext(newNode);
length += 1;
}

public void insert(int data, int Position) {

if (Position < 0) {
Position = 0;
}
if (Position > length) {
Position = length;
} else if (Position == 0) {
DLLNode temp = new DLLNode(data);
temp.next = head;
head = temp;
}
else {
DLLNode temp = head;
for (int i = 1; i < Position; i++) {
temp = temp.getNext();
}
DLLNode newNode = new DLLNode(data);
newNode.next = temp.next;
newNode.prev = temp;
newNode.next.prev = newNode;
temp.next = newNode;
length += 1;
}
}

public String toString() {
String result = "[]";
if (length == 0) {
return result;
}
result = "[" + head.getNext().getData();
DLLNode temp = head.getNext().getNext();
while (temp != null) {
result += "," + temp.getData();
temp = temp.getNext();
}
return result + "]";
}

public void insertTail(int newValue) {
DLLNode newNode = new DLLNode(newValue, tail.getPrev(), tail);
newNode.getPrev().setNext(newNode);
tail.setPrev(newNode);
length += 1;
}

public void remove(int position) {
DLLNode temp = head;
for (int i = 1; i < position; i++) {
temp = temp.getNext();
}
temp.getNext().setPrev(temp.getPrev());
temp.getPrev().setNext(temp.getNext());
length -= 1;
}
public int removeTail(){
if(length == 0){
return Integer.MAX_VALUE;
}
DLLNode save = tail.getPrev();
tail.setPrev(save.getPrev());
save.getPrev().setNext(tail);
return save.getData();
}

public static void main(String[] args) {
DoublyLinkedList obj = new DoublyLinkedList();
obj.insert(5);
obj.insert(8);
obj.insert(9);
obj.insert(6);
obj.insert(2);
obj.insert(7);
System.out.println("Before doubly linked list:-"+obj);
System.out.println("position:-" + obj.getPosition(5));
System.out.println("remove tail:-"+obj.removeTail());

System.out.println("After remove from tail doubly linked list becomes:-"+obj);
System.out.println("remove tail:-"+obj.removeTail());
System.out.println("After remove from tail doubly linked list becomes:-"+obj);
/*obj.insert(10, 2);
System.out.println("----------");
System.out.println(obj);*/
System.out.println("head data:-"+obj.head.getData());
System.out.println("head next data:-"+obj.head.getNext().getData());
System.out.println("####################################");
System.out.println("position:-"+obj.getPosition(7));
obj.remove(2);
System.out.println(obj);
}
}

No comments:

Post a Comment