SlideShare a Scribd company logo
**JAVA** A double-ended queue is a list that allows the addition and removal of items from
either end. One end is arbitrarily called the front and other the rear, but the two ends behave
identically. Specify, design and implement a class for a double-ended queue. Include operations
to check if it is empty and to return the number of items in the list. For each end, include
operations for adding and deleting items. Use doubly linked list for your implementation. Call
your class Deque (pronounced "deck").
Solution
//package com.java2novice.ds.queue;
public class DequeDblLinkedListImpl {
private LinkedNode front;
private LinkedNode rear;
public void insertFront(T item){
//add element at the beginning of the queue
System.out.println("adding at front: "+item);
LinkedNode nd = new LinkedNode();
nd.setValue(item);
nd.setNext(front);
if(front != null) front.setPrev(nd);
if(front == null) rear = nd;
front = nd;
}
public void insertRear(T item){
//add element at the end of the queue
System.out.println("adding at rear: "+item);
LinkedNode nd = new LinkedNode();
nd.setValue(item);
nd.setPrev(rear);
if(rear != null) rear.setNext(nd);
if(rear == null) front = nd;
rear = nd;
}
public void removeFront(){
if(front == null){
System.out.println("Deque underflow!! unable to remove.");
return;
}
//remove an item from the beginning of the queue
LinkedNode tmpFront = front.getNext();
if(tmpFront != null) tmpFront.setPrev(null);
if(tmpFront == null) rear = null;
System.out.println("removed from front: "+front.getValue());
front = tmpFront;
}
public int size(){
if(front == null && rear ==null){
return 0;
}
//remove an item from the beginning of the queue
LinkedNode tmpFront = front;
LinkedNode tmpRear = rear;
int i=0;
while(tmpFront != rear)
{
i++;
tmpFront=tmpFront.getNext();
}
i++;
return i;
}
public void removeRear(){
if(rear == null){
System.out.println("Deque underflow!! unable to remove.");
return;
}
//remove an item from the beginning of the queue
LinkedNode tmpRear = rear.getPrev();
if(tmpRear != null) tmpRear.setNext(null);
if(tmpRear == null) front = null;
System.out.println("removed from rear: "+rear.getValue());
rear = tmpRear;
}
public static void main(String a[]){
DequeDblLinkedListImpl deque = new DequeDblLinkedListImpl();
deque.insertFront(34);
deque.insertFront(67);
System.out.println("Size of the queue : "+deque.size());
deque.insertFront(29);
deque.insertFront(765);
deque.removeFront();
deque.removeFront();
deque.removeFront();
deque.insertRear(43);
deque.insertRear(83);
deque.insertRear(84);
deque.insertRear(546);
deque.insertRear(356);
deque.removeRear();
deque.removeRear();
deque.removeRear();
deque.removeRear();
deque.removeFront();
deque.removeFront();
deque.removeFront();
}
}
class LinkedNode{
private LinkedNode prev;
private LinkedNode next;
private T value;
public LinkedNode getPrev() {
return prev;
}
public void setPrev(LinkedNode prev) {
this.prev = prev;
}
public LinkedNode getNext() {
return next;
}
public void setNext(LinkedNode next) {
this.next = next;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}

More Related Content

PDF
In java , I want you to implement a Data Structure known as a Doubly.pdf
PDF
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
PPT
Lec-07 Queues.ppt queues introduction to queue
PDF
I need help implementing a Stack with this java programming assignme.pdf
PDF
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
PDF
Hi, Please find my code.I have correted all of your classes.Plea.pdf
PDF
Sorted number list implementation with linked listsStep 1 Inspec.pdf
PDF
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Lec-07 Queues.ppt queues introduction to queue
I need help implementing a Stack with this java programming assignme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf

Similar to JAVA A double-ended queue is a list that allows the addition and.pdf (20)

PPTX
Stack and Queue
PPTX
The presention is about the queue data structure
PPTX
linkedlist.pptx
PDF
A deque (pronounced deck) is an ordered set of items from which item.pdf
PDF
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
PDF
Introduction and BackgroundIn recent lectures we discussed usi.pdf
DOCX
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
PDF
1- The design of a singly-linked list below is a picture of the functi (1).pdf
PDF
Help please!!(Include your modified DList.java source code file in.pdf
DOCX
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
PDF
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
PPT
Chapter 5: linked list data structure
PDF
I need help in writing the test cases of the below methods i.pdf
PPTX
Data Structures and Agorithm: DS 09 Queue.pptx
PDF
Exception to indicate that Singly LinkedList is empty. .pdf
PDF
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
PDF
DSU C&C++ Practical File Diploma
PDF
DATA STRUCTURE USING C & C++
Stack and Queue
The presention is about the queue data structure
linkedlist.pptx
A deque (pronounced deck) is an ordered set of items from which item.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
1- The design of a singly-linked list below is a picture of the functi (1).pdf
Help please!!(Include your modified DList.java source code file in.pdf
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Chapter 5: linked list data structure
I need help in writing the test cases of the below methods i.pdf
Data Structures and Agorithm: DS 09 Queue.pptx
Exception to indicate that Singly LinkedList is empty. .pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
DSU C&C++ Practical File Diploma
DATA STRUCTURE USING C & C++
Ad

More from amrishinda (20)

PDF
Let f(z) be an entire function such that fn+1(z) equivalent 0, Prove.pdf
PDF
Implement threads and a GUI interface using advanced Java Swing clas.pdf
PDF
If a cells contents are greatly hypo-osmotic to the surrounding env.pdf
PDF
How are the forelimbs of humans, the wings of birds, the wings of ba.pdf
PDF
Fom 1120, Sched Mark for follow up Question 15 of 105. A corporat.pdf
PDF
Germ cells are set aside during animal development and give rise to .pdf
PDF
Determine the missing amounts. (Hint For example, to solve for (a), .pdf
PDF
Discuss the various methods of intellectual property protection avai.pdf
PDF
devise a hypothesis that explains the geographical distribution of m.pdf
PDF
Describe a diode array detector and CCD camera. Differentiate betwee.pdf
PDF
Connect a motor, two push buttons and two LEDS to the Arduino as sho.pdf
PDF
Consider managers (supervisors) that you have worked for in the past.pdf
PDF
Caterpillar is one of the Americas major exporters. The company sell.pdf
PDF
what numbers go with these descriptions.1. Uses ATP hydrolysis2..pdf
PDF
What are the three forms of Business Organizations Answer the follow.pdf
PDF
Anatomy Question Use your own words.A girl with a defective heart.pdf
PDF
Why parkinsons disease is special among the different neurological.pdf
PDF
Which statement is not true promoters are transcribed the Pribnow .pdf
PDF
When extracting DNA of an fruit how does the environemnt (how hot or.pdf
PDF
Which ANN do you prefer amidst of the variety of ANNs Justify the r.pdf
Let f(z) be an entire function such that fn+1(z) equivalent 0, Prove.pdf
Implement threads and a GUI interface using advanced Java Swing clas.pdf
If a cells contents are greatly hypo-osmotic to the surrounding env.pdf
How are the forelimbs of humans, the wings of birds, the wings of ba.pdf
Fom 1120, Sched Mark for follow up Question 15 of 105. A corporat.pdf
Germ cells are set aside during animal development and give rise to .pdf
Determine the missing amounts. (Hint For example, to solve for (a), .pdf
Discuss the various methods of intellectual property protection avai.pdf
devise a hypothesis that explains the geographical distribution of m.pdf
Describe a diode array detector and CCD camera. Differentiate betwee.pdf
Connect a motor, two push buttons and two LEDS to the Arduino as sho.pdf
Consider managers (supervisors) that you have worked for in the past.pdf
Caterpillar is one of the Americas major exporters. The company sell.pdf
what numbers go with these descriptions.1. Uses ATP hydrolysis2..pdf
What are the three forms of Business Organizations Answer the follow.pdf
Anatomy Question Use your own words.A girl with a defective heart.pdf
Why parkinsons disease is special among the different neurological.pdf
Which statement is not true promoters are transcribed the Pribnow .pdf
When extracting DNA of an fruit how does the environemnt (how hot or.pdf
Which ANN do you prefer amidst of the variety of ANNs Justify the r.pdf
Ad

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Presentation on HIE in infants and its manifestations
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Lesson notes of climatology university.
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Classroom Observation Tools for Teachers
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Cell Structure & Organelles in detailed.
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Institutional Correction lecture only . . .
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
GDM (1) (1).pptx small presentation for students
Presentation on HIE in infants and its manifestations
VCE English Exam - Section C Student Revision Booklet
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Lesson notes of climatology university.
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Classroom Observation Tools for Teachers
Chinmaya Tiranga quiz Grand Finale.pdf
Complications of Minimal Access Surgery at WLH
Cell Structure & Organelles in detailed.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Institutional Correction lecture only . . .
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Cell Types and Its function , kingdom of life
Supply Chain Operations Speaking Notes -ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Microbial diseases, their pathogenesis and prophylaxis
FourierSeries-QuestionsWithAnswers(Part-A).pdf

JAVA A double-ended queue is a list that allows the addition and.pdf

  • 1. **JAVA** A double-ended queue is a list that allows the addition and removal of items from either end. One end is arbitrarily called the front and other the rear, but the two ends behave identically. Specify, design and implement a class for a double-ended queue. Include operations to check if it is empty and to return the number of items in the list. For each end, include operations for adding and deleting items. Use doubly linked list for your implementation. Call your class Deque (pronounced "deck"). Solution //package com.java2novice.ds.queue; public class DequeDblLinkedListImpl { private LinkedNode front; private LinkedNode rear; public void insertFront(T item){ //add element at the beginning of the queue System.out.println("adding at front: "+item); LinkedNode nd = new LinkedNode(); nd.setValue(item); nd.setNext(front); if(front != null) front.setPrev(nd); if(front == null) rear = nd; front = nd; } public void insertRear(T item){ //add element at the end of the queue System.out.println("adding at rear: "+item); LinkedNode nd = new LinkedNode(); nd.setValue(item); nd.setPrev(rear); if(rear != null) rear.setNext(nd); if(rear == null) front = nd; rear = nd; } public void removeFront(){ if(front == null){
  • 2. System.out.println("Deque underflow!! unable to remove."); return; } //remove an item from the beginning of the queue LinkedNode tmpFront = front.getNext(); if(tmpFront != null) tmpFront.setPrev(null); if(tmpFront == null) rear = null; System.out.println("removed from front: "+front.getValue()); front = tmpFront; } public int size(){ if(front == null && rear ==null){ return 0; } //remove an item from the beginning of the queue LinkedNode tmpFront = front; LinkedNode tmpRear = rear; int i=0; while(tmpFront != rear) { i++; tmpFront=tmpFront.getNext(); } i++; return i; } public void removeRear(){ if(rear == null){ System.out.println("Deque underflow!! unable to remove."); return; } //remove an item from the beginning of the queue LinkedNode tmpRear = rear.getPrev(); if(tmpRear != null) tmpRear.setNext(null);
  • 3. if(tmpRear == null) front = null; System.out.println("removed from rear: "+rear.getValue()); rear = tmpRear; } public static void main(String a[]){ DequeDblLinkedListImpl deque = new DequeDblLinkedListImpl(); deque.insertFront(34); deque.insertFront(67); System.out.println("Size of the queue : "+deque.size()); deque.insertFront(29); deque.insertFront(765); deque.removeFront(); deque.removeFront(); deque.removeFront(); deque.insertRear(43); deque.insertRear(83); deque.insertRear(84); deque.insertRear(546); deque.insertRear(356); deque.removeRear(); deque.removeRear(); deque.removeRear(); deque.removeRear(); deque.removeFront(); deque.removeFront(); deque.removeFront(); } } class LinkedNode{ private LinkedNode prev; private LinkedNode next; private T value; public LinkedNode getPrev() { return prev; } public void setPrev(LinkedNode prev) {
  • 4. this.prev = prev; } public LinkedNode getNext() { return next; } public void setNext(LinkedNode next) { this.next = next; } public T getValue() { return value; } public void setValue(T value) { this.value = value; } }