Skip to content

Instantly share code, notes, and snippets.

@Ram-1234
Last active January 1, 2021 19:50
Show Gist options
  • Save Ram-1234/0229ccf49138e838228f589e2a37cca2 to your computer and use it in GitHub Desktop.
Save Ram-1234/0229ccf49138e838228f589e2a37cca2 to your computer and use it in GitHub Desktop.
STACK OPERATION
### stack implementation using java language
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Stack
{
LinkedList stack;
public Stack(){
stack=new LinkedList();
}
public boolean isEmpty(){
return stack.isEmpty();
}
public int size(){
return (int) stack.size();
}
public int push(int k){
stack.add(k);
return k;
// else
// System.out.println("Stack overFlow");
}
public int pop(){
if((int)stack.size()==0)
return 0;
else
return (int)stack.remove(stack.size()-1);
}
public int top(){
return (int)stack.get(stack.size()-1);
}
public static void main (String[] args) throws java.lang.Exception
{
Stack numberStack = new Stack();
System.out.println("Size of Stack:"+numberStack.size());
System.out.println("Stack is empty or not: "+isEmpty());
System.out.println("pushed element is :"+numberStack.push(5));
//numberStack.push(5);
System.out.println("Stack is empty or not: "+isEmpty());
System.out.println("Size of Stack:"+numberStack.size());
System.out.println("top of Stack:"+numberStack.top());
//numberStack.push(8);
System.out.println("Stack is empty or not: "+isEmpty());
System.out.println("pushed element is :"+numberStack.push(8));
System.out.println("top of Stack:"+numberStack.top());
System.out.println("Size of Stack:"+numberStack.size());
System.out.println("pop operation performed: "+numberStack.pop());
System.out.println("pop operation performed: "+numberStack.pop());
System.out.println("pop operation performed: "+numberStack.pop());
System.out.println("Stack is empty or not: "+isEmpty());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment