Skip to content

Commit

Permalink
added java file for queue data structure
Browse files Browse the repository at this point in the history
Signed-off-by: Shreyansh Khaitan <[email protected]>
  • Loading branch information
shrey141102 committed Oct 3, 2023
1 parent 5e9b016 commit 74ebc0d
Showing 1 changed file with 239 additions and 0 deletions.
239 changes: 239 additions & 0 deletions reverse a queue
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
//using stack
import java.util.*;
class Queue
{
int front, rear, size;
int capacity;
int[] array;

public Queue (int capacity)
{
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
array = new int[this.capacity];
}

// Queue is full when size becomes
// equal to the capacity
boolean isFull (Queue queue)
{
return (queue.size == queue.capacity);
}

// Queue is empty when size is 0
boolean isEmpty (Queue queue)
{
return (queue.size == 0);
}

// Method to add an item to the queue.
// It changes rear and size
void enqueue (int item)
{
if (isFull (this))
return;
this.rear = (this.rear + 1) % this.capacity;
this.array[this.rear] = item;
this.size = this.size + 1;

}
int dequeue ()
{
if (isEmpty (this))
return Integer.MIN_VALUE;

int item = this.array[this.front];
this.front = (this.front + 1) % this.capacity;
this.size = this.size - 1;
return item;
}



public Queue queuereverse (Queue q)
{
Stack < Integer > stk = new Stack <> ();
while (!q.isEmpty (q))
{
stk.add (q.array[front]);
q.dequeue ();
}
while (!stk.isEmpty ())
{
q.enqueue (stk.peek ());
stk.pop ();
}
return q;
}

public void disp () /* function to display the elements of the queue */
{

if (front == -1)
{
System.out.print ("\nQueue is Empty");
}
else
{
for (int i = front; i <= rear; i++)
{
System.out.print (array[i] + " ");
}
}
System.out.println ();
}

}

// Driver class
public class Main
{
public static void main (String[]args)
{
Queue queue = new Queue (1000);

queue.enqueue (10);
queue.enqueue (20);
queue.enqueue (30);
queue.enqueue (40);

System.out.println ("Original queue : ");
queue.disp ();
System.out.println ();

queue = queue.queuereverse (queue);

System.out.println ("Queue after reversing : ");
queue.disp ();

System.out.println ();


}
}

//using recursion
import java.util.*;
class Queue
{
int front, rear, size;
int capacity;
int array[];

public Queue (int capacity)
{
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
array = new int[this.capacity];
}

// Queue is full when size becomes
// equal to the capacity
boolean isFull (Queue queue)
{
return (queue.size == queue.capacity);
}

// Queue is empty when size is 0
boolean isEmpty (Queue queue)
{
return (queue.size == 0);
}

// Method to add an item to the queue.
// It changes rear and size
void enqueue (int item)
{
if (isFull (this))
return;
this.rear = (this.rear + 1) % this.capacity;
this.array[this.rear] = item;
this.size = this.size + 1;

}
int dequeue ()
{
if (isEmpty (this))
return Integer.MIN_VALUE;

int item = this.array[this.front];
this.front = (this.front + 1) % this.capacity;
this.size = this.size - 1;
return item;
}

public Queue reverseQueue (Queue q)
{
// Base case
if (q.isEmpty (q))
return q;

// Dequeue current item
int data = q.array[front];
q.dequeue ();

// Reverse remaining queue
q = reverseQueue (q);

// Enqueue current item
q.enqueue (data);

return q;
}

public void disp () /* function to display the elements of the queue */
{

if (front == -1)
{
System.out.print ("\nQueue is Empty");
}
else
{
for (int i = front; i <= rear; i++)
{
System.out.print (array[i] + " ");
}
}
System.out.println ();
}

}

// Driver class
public class Main
{
public static void main (String[]args)
{
Queue queue = new Queue (1000);

queue.enqueue (10);
queue.enqueue (20);
queue.enqueue (30);
queue.enqueue (40);

System.out.println ("Original queue : ");
queue.disp ();
System.out.println ();

queue.reverseQueue (queue);

System.out.println ("Queue after reversing : ");
queue.disp ();

System.out.println ();


}
}

/*
* Output:
Original queue :
10 20 30 40

Queue after reversing :
40 30 20 10
*/

0 comments on commit 74ebc0d

Please sign in to comment.