Java Stacks Homework 5. Given below is part of a class declaration and construct
ID: 3883035 • Letter: J
Question
Java Stacks Homework
Explanation / Answer
import java.io.*;
class Stack {
private int[] item;
private int top1,top2;
private int size;
public Stack(int max){
size = max;
item = new int[size];
top1 = -1;
top2 = size;
}
public void push1(int a){
if (!isFull1()){
top1++;
item[top1] = a;
}
else {
System.out.println("Stack1 is full");
}
}
public void push2(int a){
if (!isFull2()){
top2--;
item[top2] = a;
}
else {
System.out.println("Stack2 is full");
}
}
public int pop1(){
int a = item[top1];
top1--;
return a;
}
public int pop2(){
int a = item[top2];
top2++;
return a;
}
public boolean isEmpty1(){
if (top1==-1)
return true;
else
return false;
}
public boolean isEmpty2(){
if (top2==size)
return true;
else
return false;
}
public boolean isFull1(){
if ((top2-top1) == 1)
return true;
else
return false;
}
public boolean isFull2(){
if ((top2-top1) == 1)
return true;
else
return false;
}
public void clear1(){
top1 = -1;
}
public void clear2(){
top2 = size;
}
}
public class DemoStack{
public static void main(String[] args){
Stack st = new Stack(5);
st.push1(1);
st.push2(2);
st.push1(3);
st.push2(4);
System.out.println(st.pop1());
System.out.println(st.pop2());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.