Sakshi 19csu269
Queue Assignment
Ques-1)
Ans) import java.util.*;
public class ReverseArray2{
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;
void Queue() {
front = -1;
rear = -1;
}
// check if the queue is full
boolean isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}
// check if the queue is empty
boolean isEmpty() {
if (front == -1)
return true;
else
return false;
}
// insert elements to the queue
void enQueue(int element) {
// if queue is full
if (isFull()) {
System.out.println("Queue is full");
}
else {
if (front == -1) {
// mark front denote first element of queue
front = 0;
}
rear++;
// insert element at the rear
items[rear] = element;
System.out.println("Insert " + element);
}
}
// delete element from the queue
int deQueue() {
int element;
// if queue is empty
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
}
else {
// remove element from the front of queue
element = items[front];
// if the queue has only one element
if (front >= rear) {
front = -1;
rear = -1;
}
else {
// mark next element as the front
front++;
}
System.out.println( element + " Deleted");
return (element);
}
}
// display element of the queue
void display() {
int i;
if (isEmpty()) {
System.out.println("Empty Queue");
}
else {
System.out.println("\nFront-> " + front);
System.out.println("Items -> ");
for (i = front; i <= rear; i++)
System.out.print(items[i] + " ");
System.out.println("\nRear index-> " + rear);
}
}
void reversequeue()
{
int temp=0;
int s=SIZE/2;
for(int i=front,j=rear;i<s&&j>s;i++,j--)
{
temp=items[i];
items[i]=items[j];
items[j]=temp;
}
System.out.println("Queue after Reverse=");
for (int i = front; i < rear+1; i++) {
System.out.println(items[i]);
}
}
public static void main(String[] args) {
ReverseArray2 q = new ReverseArray2();
q .Queue();
q. deQueue();
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.enQueue(4);
q.enQueue(5);
q.display();
q.reversequeue();
}
}
Screenshot
Ques-2)
Ans) class Node
{
int data; // integer data
Node next; // pointer to the next node
public Node(int data)
{
// set the data in allocated node and return the node
this.data = data;
this.next = null;
}
}
class Queue
{
private static Node rear = null, front = null;
// Utility function to remove front element from the queue
public static int dequeue() // delete at the beginning
{
if (front == null) {
System.out.print("\nQueue Underflow");
System.exit(1);
}
Node temp = front;
System.out.printf("Removing %d\n", temp.data);
// advance front to the next node
front = front.next;
// if list becomes empty
if (front == null) {
rear = null;
}
// deallocate the memory of removed node and
// optionally return the removed item
int item = temp.data;
return item;
}
// function to add an item in the queue
public static void enqueue(int item)
{
Node node = new Node(item);
System.out.printf("Inserting %d\n", item);
// queue was empty
if (front == null) {
// initialize both front and rear
front = node;
rear = node;
} else {
// update rear
rear.next = node;
rear = node;
}
}
// Utility function to return top element in a queue
public static int display() {
// check for empty queue
if (front != null) {
return front.data;
} else {
System.exit(1);
}
return -1;
}
// Utility function to check if the queue is empty or not
public static boolean isEmpty() {
return rear == null && front == null;
}
}
void reversequeue(int size)
{
Queue temp=front;
Queue ptr1=rear;
//front=rear;
for(int i=0;i<size;i++)
{
while(temp.next!=rear)
{
temp=temp.next;
}
rear.next=temp;
rear=temp;
}
rear.next=null;
front=ptr1;
}
class Main
{
public static void main(String[] args)
{ size=3;
Queue q = new Queue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
System.out.printf("Front element is %d\n", q.display());
q.reversequeue(size);
q.display();
if (q.isEmpty()) {
System.out.print("Queue is empty");
} else {
System.out.print("Queue is not empty");
}
}
}
Screenshot
Ques-3)
Ans) import java.util.*;
class Queue
{
int front=-1;
int rear=-1;
int[] arr=new int[10];
void insert(int data)
{
if(rear==(arr.length-1))
{
System.out.println("Stack overflow");
return;
}
if(front==-1 && rear==-1)
{
front=rear=0;
arr[rear]=data;
}
else
{
rear=rear+1;
arr[rear]=data;
}
void remove()
{
if(front==-1||front>rear)
{
System.out.println("Stack is empty");
}
else
{
front=front+1;
}
}
void display()
{
for(int i=front;i<=rear;i++)
{
System.out.println(arr[i]);
}
}
void count()
{
int count=0;
for(int temp=front;temp<=rear;temp++)
{
if(arr[temp]%2==0)
{
count++;
}
}
System.out.println(count);
}
}
class Scountqueue
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Queue m=new Queue();
int choice;
char ch='y';
while(ch=='y')
{
System.out.println("1.add an element");
System.out.println("2.delete an element");
System.out.println("3.display the queue");
System.out.println("Enter the choice");
choice=sc.nextInt();
switch(choice)
{
case 1:System.out.println("enter data to insert");
int data;
data=sc.nextInt();
m.insert(data);
break;
case 2:m.remove();
break;
case 3: m.display();
break;
}
System.out.println("want to continue?");
ch=sc.next().charAt(0);
}
m.count();
}
}
Screenshot
Ques-4)
Ans) // Write a program to count the odd numbers in a queue using linked list
import java.util.*;
class Queue
{
static Queue front;
static Queue rear;
Queue next;
int data;
public Queue()
{
this.next=null;
}
public Queue(int data)
{
this.data=data;
this.next=null;
}
public Queue(int data,Queue next)
{
this.data=data;
this.next=next;
}
public void enqueue(int data) // function to add elements
{
Queue temp;
temp=new Queue(data); // creting a new node temp
if(rear==null)
{
rear=front=temp; // inserting the first element
}
else
{
rear.next=temp;
temp.next=null;
rear=temp; // updating the last node after insertion
}
}
public void dequeue() // function to delete elements
{
if(front==null) // if the queue is empty
{
System.out.println("Queue is empty");
}
else
{
front=front.next; // updating the front after deletion
}
}
public void display() // function to print the queue
{
Queue temp;
if(front==null)
{
System.out.println("Queue is empty");
}
else
{
temp=front; // initializing temp
while(temp!=rear)
{
System.out.print(temp.data+" ");
temp=temp.next; // prints data till 2nd last node
}
System.out.println(temp.data); // printing the last node
}
}
public int count_odd()
{
int count=0;
Queue temp=front;
for(;temp!=rear;temp=temp.next)
{
if(temp.data%2!=0)
count++;
}
if(rear.data%2!=0)
count++;
return count;
}
}
public class odd extends Queue
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Queue qu=new Queue();
int i,size,data,del_size;
System.out.println("Enter the size of the queue");
size=sc.nextInt();
System.out.println("------------------");
if(size!=0)
{
System.out.println("Enter the elements");
}
for(i=1;i<=size;i++)
{
data=sc.nextInt();
qu.enqueue(data);
}
if(front!=null)
{
System.out.println("Elements of the queue are");
}
qu.display();
System.out.println("No. of odd elements in the queue is:"+qu.count_odd());
}
}
Screenshot
Ques-5)
Ans) // Write a program to reverse the first m elements from the queue, where m < n, n is
the total number of elements in queue.
import java.util.Scanner;
public class Queuer {
public static int front, rear, max; //front use as deleting end and rear use as inserting end
and max is mx size of queue
public static int queue[]; //array of type int
Queuer(int m) //constructor of class queue and parameter use is the max. size of queue
{
front = rear = -1;
max = m;
queue = new int[max];
}
public static void Enqueue(int data)
{ //function enqueue to insert value in queue
if (rear== max-1) //condtion to check whether the queue is full or not
{
System.out.println("Queue is full");
return;
}
if(front == -1 & rear ==-1)
{
front=rear=0;
queue[rear]=data;
}
else {rear++; //if queue is not reach to max. size of it then value added in queue
queue[rear] = data;
}
return;
} //end of function
public static void rev(int s)
{
int temp=0;
for(int i=0,j=s-1;i<s/2&&j>s/2;i++,j--)
{
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}System.out.println("queue after reverse");
queueDisplay();
}
public static void queueDisplay()//function display value to display each value of queue
{
int i;
if (front == -1 & rear ==-1) { //condtion to check whether queue is empty or not
System.out.println("Queue is Empty");
return;
}
for (i = front; i < rear+1; i++) { //if queue is not empty then display all value present in
queue
System.out.println(queue[i]);
}
return;
} //end of function
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in); //creating a object of scanner class
Queuer q = new Queuer(5); //creating a object of queue class and calling constructor
of queue class
int data;
System.out.println("Enter the number of element u want to add in queue");
int size=sc.nextInt();
for(int i=0;i<size;i++)
{
System.out.println("Enter a value u want to add: ");
data=sc.nextInt();
Enqueue(data); //calling a function enqueue
}
System.out.println("queue before reverse");
queueDisplay();
int m;
System.out.println("enter the from which u want to reverse:");
m=sc.nextInt();
rev(m);
Screenshot