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

Queue

Queue data structure

Uploaded by

jones.codyxxx
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Queue

Queue data structure

Uploaded by

jones.codyxxx
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT 5: 6/5/24

A Queue is a linear data structure which enables the user to add integers from rear end and remove
integers from the front end, using the concept of FIFO (Fist In First Out) principle. Define a class
Queue with the following details:
Class Name: Queue
Data members:
q[]: array to hold elements.
n: to store the size of the array.
f: to point the index of front end.
r: to point the index of rear end.
Member methods:
Queue(int n1): parameterized constructor.
void addQueue(int x): to push or insert argument x at the rear location, if possible, otherwise display
"Queue Overflow".
int deleteQueue(): to pop integer from the front location of the queue, if possible, return the
integer, otherwise display "Queue Underflow".
void display(): to print the elements present in the queue, if possible, otherwise display "Queue
Underflow".

import java.util.*;
class Queue_Diganta Biswas
{ //start of class
int q[]; //declaration of variables
int f,r,n;
Queue_ Diganta Biswas (int n1)
{
q=new int[n];
n=n1; //initializing variables
f=r=0;
}

void addQueue(int x)
{
if (r < n - 1)
{
q[r] = x;
r++;
}
else
{
System.out.println("Queue Overflow");
}
}

int deleteQueue() //deleting an element from the queue


{
if(f!=r)
{
f++;
return q[f];
}
else
{
System.out.println("Queue Underflow");
return -9999;
}
}

void display()
{
if (f == r)
{
System.out.println("Queue Underflow");
return;
}
for (int i = f; i < r; i++)
{
System.out.print(q[i] + " ");
}
System.out.println();
}

public static void main()


{ //start of main
Scanner sc= new Scanner(System.in);
int ch, no; //declaration of variables
Queue_ Diganta Biswas ob=new Queue_ Diganta Biswas (5);
while(true)
{ //start of while
System.out.println("Enter 1 - insert , 2 - delete, 3 - display , 4 - exit");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter number to insert ");
no=sc.nextInt();
ob.addQueue(no);
break;
case 2:
no=ob.deleteQueue();
System.out.println("Number deleted "+no);
break;
case 3:
ob.display();
break;
case 4:
System.out.println("Exit");
System.exit(0);
default:
System.out.println("Wrong Choice");
}//end of switch case
}//end of while
}//end of main
}//end of class

Variable description:-
Name Datatype Purpose
q[] int array to hold elements.
N int to store the size of the array.
f int to point the index of front end.
r int to point the index of rear end.
n1 int formal parameter
x int formal parameter
ch int switch case variable
no int variable to pass to methods

You might also like