Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same.
using java
StackArray.java:
public class StackArray {
private final int size = 20; //Size of the stack
array
private int[] elements; //Array that gets created as a
stack
private int topIndex = -1; //index of the top
element
public StackArray() {
elements = new int[size];
}
public StackArray( int maxSize ) {
elements = new int[maxSize];
}
public void push( int element ) {
if( isFull( ))
System.out.println("Stack is full and element cannot be
pushed");
//throw new Exception("Stack
Full");
else {
topIndex++;
elements[topIndex ] = element;
}
}
public void pop() {
if( isEmpty() )
System.out.println("Stack is empty and element cannot be
popped");
//throw new Exception("Stack Empty");
else {
topIndex--;
}
}
public int top() {
if( isEmpty() ) {
System.out.println("Stack is empty and no Top exists");
System.exit(1);
return 0;
}
else
return
elements[topIndex];
}
public boolean isEmpty() {
return topIndex == -1;
}
public boolean isFull() {
return ( topIndex ==
elements.length - 1 );
}
public void printStack() {
if( isEmpty())
System.out.println("Stack is empty");
else
for( int i = 0;
i <= topIndex; i++)
System.out.print(elements[i] + " ");
System.out.println();
}
}
Theory:
Stack:
-The stack is a linear data structure which obeys the LIFO(Last In First Out) sequence.
Operations on Stack:
There are mainly 2 operations perform on stack:
1.Push- we can add elements in stack using push() method. Its add element to the top of stack.
2.Pop- We can delete elements in stack using pop() method. Its delete top element of stack.
Solution:
Algorithm to pop 2 element at a time from stack:
1. Create a stack.
2. Enter/push some values in stack.
Note: the stack values must be differents.
3. for deleting elements first check the stack is empty or not.
4. If the stack is not empty then we check the first element i.e 0th location of stack is a top element or not.
5. If it is top element then we can delete one element and display stack is empty.
6. If it is not a top element then we can delete two element and continue process.
7.display the stack.
Code:
public class StacktwoPopArr {
private final int size = 20; //Size of the stack array
private int[] elements; //Array that gets created as a stack
private int topIndex = -1; //index of the top element
public StacktwoPopArr() {
elements = new int[size];
}
public StacktwoPopArr( int maxSize ) {
elements = new int[maxSize];
}
public void push( int element ) {
if( isFull( ))
System.out.println("Stack is full and element cannot be
pushed");
//throw new Exception("Stack Full");
else {
topIndex++;
elements[topIndex ] = element;
}
}
public void pop() {
if( isEmpty() )
System.out.println("Stack is empty and element cannot be
popped");
//throw new Exception("Stack Empty");
else {
if (elements[0]==elements[topIndex])
{
topIndex--;
System.out.println("Stack is empty");
}
else
{
topIndex--;
topIndex--;
}
}
}
public int top() {
if( isEmpty() ) {
System.out.println("Stack is empty and no Top exists");
System.exit(1);
return 0;
}
else
return elements[topIndex];
}
public boolean isEmpty() {
return topIndex == -1;
}
public boolean isFull() {
return ( topIndex == elements.length - 1 );
}
public void printStack() {
if( isEmpty())
System.out.println("Stack is empty");
else
for( int i = 0; i <= topIndex; i++)
System.out.print(elements[i] + " ");
System.out.println();
}
public static void main(String[] args) {
StacktwoPopArr s=new StacktwoPopArr();
s.push(10); //call push() method for adding element
s.printStack();// the output: 10
s.push(20);
s.printStack();// the output: 10 20
s.push(30);
s.printStack();// the output: 10 20 30
s.pop();//In this line we delete top 2 elements i.e 30 and 20
s.printStack();// the output: 10
s.pop();//In this line we delete remaining one element i.e 10 and
print stack is empty.
}
}
Output:
Get Answers For Free
Most questions answered within 1 hours.