In: Computer Science
Change this cods according to instructions please:
public class Stack {
int stackPtr;
int data[];
public Stack() //constructor
{
stackPtr=0;
data=new int[10];
}
public Stack(int sz) //constructor
{
stackPtr=0;
data=new int[sz];
}
public void push(int val)
{
data[stackPtr]=val;
stackPtr++;
}
public int pop()
{
stackPtr--;
//int retVal;
return(data[stackPtr]);
}
public boolean isEmpty()
{
return(stackPtr==0);
}
public boolean isFull()
{
return(stackPtr==data.length);
}
}
Hey mate, before implementing stack as an array there are some few concepts need to be known-
1) Stack is a linear data structure which follows a particular order in which the operations are performed. The order is Last in First out (LIFO), that means the last element inserted will be the first element to be deleted or pop out.
2) The stackPtr or top of stack will always points to -1 value which represents the stack is empty. And it doesn't points to 0 for empty condition because in array concept 0th index stores the first value and therefore, stack implemented as an array will have first element at 0th position.
3) The insertion (push) of elements in stack as an array will start from 0th index and deletion (pop) will take place from last index of array (i.e. (size of array -1) position).
CLASS IMPLEMENTATION -
public class Stack {
int stackPtr=-1;
int data[ ];
public Stack() /* no-argument
constructor*/
{
data=new int[10];
}
public Stack(int sz) /* parameterized
constructor*/
{
data=new int[sz];
}
public void push(int val)
{
if(stackPtr>=(data.length-1))
{
System.out.println("Stack is full, cannot push more
elements");
}
else
{
data[++stackPtr]=val;
}
}
public int pop()
{
if(stackPtr==-1)
{
System.out.println("Stack is empty, cannot pop any element");
}
else
{
return(data[stackPtr--]); /* returning deleted
value*/
}
return 0;
}
public boolean isEmpty()
{
return (stackPtr==-1); /* return true if stack isEmpty
else return false*/
}
public boolean isFull()
{
return (stackPtr==(data.length-1)); /* return true if
stack isFull else return false*/
}
}
TESTING CODE -
class Main /* driver code to test our
implementation*/
{
public static void main(String[] args)
{
Stack s=new Stack(); /* this will call no-argument
constructor as no parameter is passed*/
System.out.println("Stack is full :"+ s.isFull());
/* checking stack status in the beginning*/
System.out.println("Stack is empty :"+ s.isEmpty());
s.push(1); /* adding elements in stack*/
s.push(10);
s.push(4);
s.push(6);
s.push(14);
s.push(7);
s.push(23);
s.push(11);
s.push(98);
s.push(16);
s.push(132); /* this value will not be added as the
size of stack is 10 and stack is full
and the message present in if condition inside push() will be
displayed*/
System.out.println("Stack is full :"+ s.isFull());
/* stack status after adding elements*/
System.out.println("Popped element :"+ s.pop());
System.out.println("Popped element :"+ s.pop());
System.out.println("Stack is full :"+ s.isFull()); /*
stack status after popping out some elements*/
System.out.println("Stack is empty :"+ s.isEmpty());
}
}
OUTPUT -
Stack is full :false
Stack is empty :true
Stack is full, cannot push more elements
Stack is full :true
Popped element :16
Popped element :98
Stack is full :false
Stack is empty :false