DEV Community

Swapnil Gupta
Swapnil Gupta

Posted on

Stacks in Java

Stack Implementation

  • Stack class
  • Arrays
  • LinkedList

Stack Methods
Stack Methods
reference:javatpoint

import java.util.Stack;  
import java.util.Iterator;
import java.util.ListIterator;
public class Main
{
    public static void main(String[] args) {
        System.out.println("is stack empty:");
        Stack  stk= new Stack();
        stk.push(78);  
        stk.push(113);
        stk.push(112);
        stk.push("books:");
        stk.push("book1");
        stk.push("book2");
        stk.push("book3");

        int location = stk.search("book1");  
System.out.println("Location of Book1: " + location);  
         //System.out.println(result);
    //  stk.peek();
    System.out.println(stk.size());
    //iterating the stack 
    Iterator iterator = stk.iterator();  

 while(iterator.hasNext())  
{  
Object values = iterator.next();  
System.out.println(values);   
}    

ListIterator ListIterator = stk.listIterator(stk.size());  
System.out.println("Iteration over the Stack from top to bottom:");  
while (ListIterator.hasPrevious())   
{  
Object listi = ListIterator.previous();  
System.out.println(listi);  
}  

    }
}

Enter fullscreen mode Exit fullscreen mode

Stack Implementation using Arrays

// implementing using Arrays 

class Stack
{
    //Max- maximum Size af array
    static final int MAX = 1000;
    // or we can take double the length of Stack itself
    int top;
    int a[] = new int[MAX];

    //  isempdty condition
     boolean isEmpty()
    {
        return (top < 0);
    }

    Stack()
    {
        top = -1;
    }
  //push condition
    boolean push(int x)
    {
        //top should be less than Max, maximum length of Array
        if (top >= (MAX - 1)) {
            System.out.println("Stack Overflow");
            return false;
        }
        else {
            a[++top] = x;
            System.out.println(x + " pushed into stack");
            return true;
        }
    }

    // pop condition
    int pop()
    {
        if (top < 0) {
            System.out.println("Stack Underflow");
            return 0;
        }
        else {
            int x = a[top--];
            return x;
        }
    }


    //peek condition
     int peek()
    {
        if (top < 0) {
            System.out.println("Stack Underflow");
            return 0;
        }
        else {
            int x = a[top];
            return x;
        }
    }
    // printing the stack 
    void print(){
    for(int i = top;i>-1;i--){
      System.out.print(" "+ a[i]);
    }
  }
}
//Main program 
public class Main{
    // choose the length of stack/arrays
    public static void main(String[] args) {
        Stack stk = new Stack();
        stk.push(10);
        stk.push(1);
        stk.push(2);
        System.out.println(stk.pop() + " Popped from stack");
         System.out.print("Elements present in stack :");
        stk.print();
    }
}
Enter fullscreen mode Exit fullscreen mode

Stack Challenge

  • using a stack check whether it is palindrome
  • string may contain punctuation and spaces . they should be ignored
  • case should be ignored
  • Eg:
  • "RaceCar" is a palindrome
  • "hello" is not a palindrome

Top comments (0)