Queue Assignment
Queue Assignment
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;
}
// 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];
}
System.out.println("Queue after Reverse=");
for (int i = front; i < rear+1; i++) {
System.out.println(items[i]);
}
}
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
return -1;
}
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 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");
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;
}
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 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];
}
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;
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