Write a function is_full that returns true or false to indicate whether the arra
ID: 3807374 • Letter: W
Question
Write a function is_full that returns true or false to indicate whether the array that implements a stack is full. Assume that SIZE specifies the size of the array. b) Write a version of push that checks for a full array. If the array is full, the function simply returns false. If the array is not full, the behaviour is the same as the original push, except that the function also returns true. Assume that SIZE specifies the size of the array. c) Write a version of enqueue that checks for a full array. If the array is full, the function simply returns false. If the array is not full, the behaviour is the same as the original enqueue, except that the function also returns true. d) Write a function rear that returns, but do not remove, the most recently added item in a queue.Explanation / Answer
a,b
PROGRAM OF STACK:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Stack
{
private double[] stack=new double[5];
private int size=5;
private int top=-1;
public Stack()
{
}
//Parameterized constructor for initializing array size
public Stack(int size)
{
this.stack = new double[size];
this.top = -1;
this.size = size;
}
//inserting values in to stack
public boolean push(double obj)
{
if (top >= size)
{
return false;
}
stack[++top] = obj;
return true;
}
// //checking whether stack is full
public boolean isFull()
{
if(top==size)
{
return true;
}
return false;
}
//main function
public static void main(String s[]) throws NumberFormatException, IOException
{
//variable type stack
Stack s1= null;
//InputStreamReader & BufferedReader for reading the input
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
int choice=0;
double number=0;
System.out.println("1. user default size of array:");
System.out.println("2. custum size");
choice=Integer.parseInt(br.readLine().toString());
//checking user is using default size of an array or customized size
if(choice==1)
{
s1=new Stack();
}
else if(choice==2)
{
System.out.println(" enter size of array:");
int size=Integer.parseInt(br.readLine());
s1=new Stack(size);
}
loop:do
{
System.out.println("1. push");
System.out.println("2. is full");
System.out.println("3. exit");
System.out.println("Enter the Choice:");
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
//pushing the value to the stack
try
{
System.out.println("Enter the number:");
number=Double.parseDouble(br.readLine());
s1.push(number);
}
catch(IndexOutOfBoundsException e)
{
System.out.println("Array is full");
}
break;
case 2:
//checking the stack whether it is full or not
System.out.println(s1.isFull());
break;
case 3:
break loop;
default:
System.out.println("Enter a valid choice");
}
}while(true);
}
}
c,d
PROGRAM FOR QUEUE:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Queue
{
private double[] queue=new double[5];
private int size=5;
private int front=0;
private int rear=0;
public Queue()
{
}
//Parameterized constructor for initializing array size
public Queue(int size)
{
this.queue = new double[size];
this.front = 0;
this.rear=0;
this.size = size;
}
//inserting values in to queue
public boolean enQueue(double obj)
{
rear = (rear + 1) % queue.length;
if (rear == front)
{ //if full
System.out.println("Queue is fully loaded.");
if (rear == 0)
rear = queue.length - 1;
else
rear = rear - 1;
return false;
}
queue[rear] = obj;
return true;
}
//checking whether queue is empty
public boolean isEmpty()
{
if(this.front==-1 && this.rear==-1)
{
return true;
}
return false;
}
// //checking whether queue is full
public double rear()
{
return queue[rear];
}
//main function
public static void main(String s[]) throws NumberFormatException, IOException
{
//variable type queue
Queue q1= null;
//InputStreamReader & BufferedReader for reading the input
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
int choice=0;
double number=0;
System.out.println("1. user default size of array:");
System.out.println("2. custum size");
choice=Integer.parseInt(br.readLine().toString());
//checking user is using default size of an array or customized size
if(choice==1)
{
q1=new Queue();
}
else if(choice==2)
{
System.out.println(" enter size of array:");
int size=Integer.parseInt(br.readLine());
q1=new Queue(size);
}
loop:do
{
System.out.println("1. Enqueur");
System.out.println("2. is full");
System.out.println("3. exit");
System.out.println("Enter the Choice:");
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
//pushing the value to the queue
try
{
System.out.println("Enter the number:");
number=Double.parseDouble(br.readLine());
q1.enQueue(number);
}
catch(IndexOutOfBoundsException e)
{
System.out.println("Array is full");
}
break;
case 2:
System.out.println(q1.rear());
break;
case 3:
break loop;
default:
System.out.println("Enter a valid choice");
}
}while(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.