Stack Program

Write a program in java to demonstrate the Stack, (LIFO) Last In First Out

 
public class StrStack
{
 String str[];
 int top,size;
 StrStack(int size)
 {
  this.size=size;
  str=new String[size];
  top=-1;
 }
 void push(String value)
 {
  if(top==size-1)
  {
   System.out.println("Stack is Full");
  }
  else
  {
   top++;
   str[top]=value;
  }
 }
 String pop()
 {
  if(top==-1)
  {
   System.out.println("Stack is Empty");
   return "\0";
  }
  else
  {
   String temp=str[top];
   top--;
   return temp;
  }
 }
}

public class StackDemo 
{
 public static void main(String[] args) 
 {   
  StrStack s1=new StrStack(3); 
  s1.push("www.google.com");
  s1.push("https://plus.google.com/104107466825619614125");
  s1.push("Hello");
  s1.push("gtu java program");
  
  System.out.println(s1.pop());
  System.out.println(s1.pop());
  System.out.println(s1.pop());
  System.out.println(s1.pop());
 }
}

Output

0 comments:

Post a Comment