A deque doubleended queue is a data structure that allows yo

A “deque (double-ended queue)” is a data structure that allows you to push and pop from one end (tail) like a stack, and dequeue from the other end (front) like a queue. Create a LinkedListed-based unbounded Deque class that provide the following methods: push, pop, dequeue, and size(). You do not need to enlarge in the implementation. A main function should be created to test each of those methods.

Here is the Linked List Node Class that the deque can be based on:

Solution


package listclasses;
public class singlelinkedlist {
   int data;
   singlelinkedlist next;
   singlelinkedlist(int data){
       this.data=data;
      
   }
   singlelinkedlist(int data,singlelinkedlist next){
       this.data=data;
       this.next=next;
   }
   public String tostring(){
       return data+\"\";
   }
}


mainclass:

package listclasses;
import java.util.*;
public class singlelinkedlistmain {
static singlelinkedlist head;
static int size;
static int rear;
static {
   head=null;
   size=0;
   rear=5;
}
   public static void main(String[] args) {
     
       enque(10);
       enque(20);
       enque(30);
       enque(40);
       enque(50);

  
   display();
   System.out.println(\"\ height:\"+size);
   dequee();
     
   display();
  
   pop();
   display();
   }
public static void enque(int data){
       if(size<rear){
       singlelinkedlist curnt=head;
       if(head==null){
           head=new singlelinkedlist(data,null);
           size++;
          
       }else{
           while(curnt.next!=null){
               curnt=curnt.next;
           }
           singlelinkedlist newlist=new singlelinkedlist(data,null);
           curnt.next=newlist;
           size++;
       }
       }else{
           System.out.println(\"queeu is full:\");
       }
      
   }

public static void dequee(){
       if(head==null){
           System.out.println(\"queue is empty:\");
           return;
       }
       if(head.next!=null){
           head=head.next;
           size--;
       }
      
   }
public static void push(int data){
       if(size<rear){
       if(head==null){
           head=new singlelinkedlist(data,null);
          
       }else{
           singlelinkedlist curent=head;
           while(curent.next!=null){
               curent=curent.next;
           }
           singlelinkedlist newlist=new singlelinkedlist(data,null);
           curent.next=newlist;
       }
          
       size++;}else
           System.out.println(\"\ stack overflow\");
   }
   public static void pop(){
       if(head==null){
           System.out.println(\"\ stack undflow\");
           return;
       }
       if(head.next==null){
           head=null;
           size--;
           return;
       }
             
       singlelinkedlist curent=head;
       while(curent.next.next!=null){
           curent=curent.next;
       }
       curent.next=null;
       size--;
         
   }
public static void display(){
       singlelinkedlist curent=head;
       while(curent!=null){
           System.out.print(curent.data+\" \");
           curent=curent.next;
       }
       //System.out.println(\"\ \"+size);
      
   }

}

A “deque (double-ended queue)” is a data structure that allows you to push and pop from one end (tail) like a stack, and dequeue from the other end (front) like
A “deque (double-ended queue)” is a data structure that allows you to push and pop from one end (tail) like a stack, and dequeue from the other end (front) like
A “deque (double-ended queue)” is a data structure that allows you to push and pop from one end (tail) like a stack, and dequeue from the other end (front) like

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site