Stacks in Java

Stack Implementation

Stack class
Arrays
LinkedList

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) {


This content originally appeared on DEV Community and was authored by Swapnil Gupta

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);  
}  

    }
}

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();
    }
}

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


This content originally appeared on DEV Community and was authored by Swapnil Gupta


Print Share Comment Cite Upload Translate Updates
APA

Swapnil Gupta | Sciencx (2022-07-21T12:47:00+00:00) Stacks in Java. Retrieved from https://www.scien.cx/2022/07/21/stacks-in-java/

MLA
" » Stacks in Java." Swapnil Gupta | Sciencx - Thursday July 21, 2022, https://www.scien.cx/2022/07/21/stacks-in-java/
HARVARD
Swapnil Gupta | Sciencx Thursday July 21, 2022 » Stacks in Java., viewed ,<https://www.scien.cx/2022/07/21/stacks-in-java/>
VANCOUVER
Swapnil Gupta | Sciencx - » Stacks in Java. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/21/stacks-in-java/
CHICAGO
" » Stacks in Java." Swapnil Gupta | Sciencx - Accessed . https://www.scien.cx/2022/07/21/stacks-in-java/
IEEE
" » Stacks in Java." Swapnil Gupta | Sciencx [Online]. Available: https://www.scien.cx/2022/07/21/stacks-in-java/. [Accessed: ]
rf:citation
» Stacks in Java | Swapnil Gupta | Sciencx | https://www.scien.cx/2022/07/21/stacks-in-java/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.