0% found this document useful (0 votes)
54 views

Class Private Int Private Int Private Int Private Int Public Int Public Int

The document defines a Dequeue (double-ended queue) class with methods to insert and remove items from either end of the queue. The class includes insertLeft(), insertRight(), removeLeft(), removeRight(), isEmpty(), and isFull() methods. It uses a circular array to support wrapping around at the end of the array, similar to a standard queue implementation.

Uploaded by

juthaman
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Class Private Int Private Int Private Int Private Int Public Int Public Int

The document defines a Dequeue (double-ended queue) class with methods to insert and remove items from either end of the queue. The class includes insertLeft(), insertRight(), removeLeft(), removeRight(), isEmpty(), and isFull() methods. It uses a circular array to support wrapping around at the end of the array, similar to a standard queue implementation.

Uploaded by

juthaman
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

//Create a Dequeue class based on the discussion of deques.

It should include insertLet(),


//insertRight(),removeLeft(),removeRight(),isEmpty(),and isFull() methods.It will
//need to support wrap around at the end of the array,as queues do

class dqueue
{
private int maxSize;
private int[] dqueArray;
private int front;
private int rear;
public int nItems;

public dqueue(int s)
{
maxSize = s;
dqueArray = new int[maxSize];
front = 0;
rear = -1;
nItems =0;
}

public void insertRight(int j)


{
if (rear == maxSize-1)
rear = -1;
dqueArray[++rear] = j;
nItems++;
}

public void insertLeft(int j)


{
if (front == 0)
front = maxSize;
dqueArray[--front] = j;
nItems++;
}

public int removeLeft()


{
int temp;
temp = dqueArray[front++];
nItems--;
if (front == maxSize)
front = 0;
return temp;
}

public int removeRight()


{
int temp;
temp = dqueArray[rear--];
nItems--;
if (rear == -1)
rear = maxSize-1;
return temp;
}

1
public boolean isEmpty()
{
return (nItems==0);
}

public boolean isFull()


{
return (nItems==maxSize);
}

class dequeueApp
{
public static void main(String[] args)

{
dqueue thequeue = new dqueue(100);
int[] inputArray = {10,20,33,44,55,66,88};

for (int x:inputArray)


thequeue.insertRight(x); //insert to the queue
try
{
for(;thequeue.nItems>0;)
{
//delete from the queue
System.out.printf("%s" ,thequeue.removeLeft());
System.out.println();
}
}
catch(Exception ex)
{
System.out.println("no more elements to delete");
}

for (int x:inputArray)


thequeue.insertLeft(x); //insert to the queue

try
{
for(;thequeue.nItems>0;)
{
//delete from the queue
System.out.printf("%s" ,thequeue.removeRight());
System.out.println();
}
}
catch(Exception ex)
{
System.out.println("no more elements to delete");
}
}
}

2
3

You might also like