ARRAYDEQUE IMPLEMENTATION IN JAVA WITH GENERICS WITHOUT USING INBUILT FUNCTIONS
USING DOUBLY LINKED LIST
class NodeImplementation<E>{
Node<E> head;
Node<E> tail;
static class Node<E>{
E data;
Node<E> next;
Node<E> prev;
Node(E data){
this.data=data;
this.next=null;
this.prev=null;
}
}
public void toAddHead(E data){
Node<E> newNode=new Node(data);
if(head==null && tail==null){
System.out.println("Deque is empty");
head=newNode;
tail=newNode;
}
else{
Node temp=head;
newNode.prev=temp;
temp.next=newNode;
newNode.next=null;
head=newNode;
}
}
public void toAddTail(E data){
Node<E> newNode=new Node(data);
if(head==null && tail==null){
System.out.println("List is Empty.");
head=newNode;
tail=newNode;
}
else{
Node temp=tail;
newNode.next=temp;
temp.prev=newNode;
tail=newNode;
newNode.prev=null;
}
}
public void toRemoveTail(){
if(tail==null){
System.out.println("Lis is empty.");
}
else if(tail==head){
System.out.println("Single element in list");
System.out.println("Removed is: "+head.data);
tail=null;
head=null;
}
else{
Node temp=tail;
System.out.println("Removed is: "+tail.data);
(temp.next).prev=null;
tail=(temp.next);
temp.next=null;
}
}
public void toRemoveLast(){
Node<E> temp=tail;
if(head==null && tail==null){
System.out.println("List is empty.");
}
else if(head==tail){
System.out.println("Only one element in the list.");
System.out.println("Removed is: "+head.data);
head=null;
tail=null;
}
else{
while(temp.next!=head){
temp=temp.next;
}
System.out.println("Removed is: "+head.data);
head.prev=null;
temp.next=null;
head=temp;
}
}
public void retFirst(){
if(head==null && tail==null){
System.out.println("Empty list.");
}
else if(head==tail){
System.out.println("Only one element in list..");
System.out.println("Peek is: "+tail.data);
}
else
{
System.out.println("Peek is: "+tail.data);
}
}
public void retLast(){
if(head==null && tail==null){
System.out.println("Empty list.");
}
else if(head==tail){
System.out.println("Only one element in list..");
System.out.println("Peek is: "+tail.data);
}
else
{
System.out.println("Peek is: "+head.data);
}
}
public void toPrint(){
if(head==null && tail==null){
System.out.println("List is empty. Nothing to print.");
}
else if(head==tail){
System.out.println("Only single element..");
System.out.println(tail.data);
}
else{
Node temp=tail;
while(temp!=null){
System.out.println(temp.data);
temp=temp.next;
}
}
}
}
class ArrayDequeueDemo{
public static void main(String arg[]){
NodeImplementation<Integer> obj1=new NodeImplementation<Integer>();
System.out.println("Adding in the last...");
obj1.toAddHead(1);
obj1.toAddHead(2);
System.out.println("Printing.....");
obj1.toPrint();
System.out.println("Removing from the last...");
obj1.toRemoveLast();
System.out.println("Printing.....");
obj1.toPrint();
System.out.println("Removing from the start...");
obj1.toRemoveTail();
System.out.println("Printing.....");
obj1.toPrint();
System.out.println("Adding in the start...");
obj1.toAddTail(3);
obj1.toAddTail(4);
obj1.toAddTail(5);
obj1.toAddTail(6);
System.out.println("Printing.....");
obj1.toPrint();
System.out.println("Retreival of first element...");
obj1.retFirst();
System.out.println("Retreival of last element...");
obj1.retLast();
}
}
Thanks for Reading.
Please Like, Comment and Share.
"Knowledge grows with sharing not by saving đŸ˜‡."
Comments
Post a Comment