Using Java, in the most simple algorithm please
import java.util.*;
import java.lang.*;
class Driven {
static class stack {
//Define a type of a data.
char datas[];
//This variable is use to keep track of a stack.
int top = -1;
//Default constructor.
public stack() {
datas = new char[10];
}
//Constructor for user-defined size.
public stack(int size) {
datas = new char[size];
}
public void push(char data)
{
//Check the condition if stack is full or not.
//If top = length-1 then it means all place is filled.
if(top == datas.length - 1)
{
System.out.println("Stack is full");
}
//If stack is not full than add that value.
else
{
top++;
datas[top] = data;
}
}
public void pop()
{
//For popping you have to check is stack is empty.
//if stack is empty than you cannot pop item.
if(top == -1)
{
System.out.println("Stack is empty.");
}
else
{
System.out.println("Popped item is : " + datas[top]);
top--;
}
}
//If Top = -1 it means stack is empty.
public Boolean isEmpty(){
if(top == -1)
{
return true;
}
else {
return false;
}
}
//If top=length-1 it means stack is full.
public Boolean isFull(){
if(top == datas.length - 1)
{
return true;
}
else {
return false;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
stack obj = new stack(2);
obj.push('1');
obj.push('2');
obj.push('3');
System.out.println(obj.isFull());
}
}
OUTPUT :-
Get Answers For Free
Most questions answered within 1 hours.