• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Java

Implementing Stack using Array in Java

  • Categories Java, Data Structures

To understand more about Stack watch our video on Implementing Stack using Array in Java – click here

Q. What is a stack? 
Ans.
Stack is a linear data structure implemented in Last in and First out or LIFO manner. Like for example a stack of plates, where insertion and deletion happens only at one end.

Q. What is overflow and underflow situation? Or What is difference between them?
Ans.

Overflowunderflow
Overflow refers to when one tries to push an item in stack that is fullUnderflow refers to when one tries to POP/delete an item from an empty stack
This situation occurs when the size of the stack is fixed and cannot grow further or there is no memory left to accommodate new itemThis situation is when the stack is empty and still one tries to pop/delete an item

Q.  What are the two major stack operations?
Ans. The two operations performed on the stack are:

    1. Push: Adding (inserting) new element on to the stack.
    2. Pop: Removing (deleting) an element from the stack.

Q. list the different operations perform on the stack?
Ans.

    1. Push i.e., Insertion of element in the stack
    2. Pop i.e., Deletion of an element from the stack
    3. Peek i.e., viewing topmost element without removing it
    4. isEmpty() i.e., checks if the stack is empty.
    5. Display or view all the elements in the stack.

stack operation program .

import java.util.Scanner;
public class Stack
{
   String [] name;
   int cap;
   int top;
   boolean parenthesisChecker(String s)
   {
      for (int i = 0; i < s.length();i++) { char c = s.charAt(i); switch (c) { case '{': case '[': case '(': push(Character.toString(c)); break; case ')': String b = pop(); if (b.charAt(0) != '(') { System.out.println(b + " UnMatched ) "); return false; } break; case ']': b = pop(); if (b.charAt(0) != '[') { System.out.println(b + " UnMatched ] "); return false; } break; case '}': b = pop(); if (b.charAt(0) != '{') { System.out.println(b + " UnMatched } "); return false; } break; default: System.out.print("Ignore "+ c); } } return true; } Stack(int size) { cap = size; name = new String[cap]; top = -1; } Stack() { top = -1; cap = 100; name = new String[cap]; } void push (String s) { if (top+1 >= cap) 
      {
         System.out.println("Stack Overflow");
      }
      else 
      {
         top++;
         name[top] = s;
         System.out.println("push successful");
      }
   }
   String pop()
   {
      if (top < 0) 
      {
         System.out.println("Stack Underflow");
         return null;
      }
      else 
      {
         String s = name[top];
         top--;
         System.out.println("pop successful");
         return s;
      }
   }
   boolean isEmpty() 
   {
      return (top < 0);
   }
   void display() 
   {
      for (int i = 0; i <= top; i++) 
      {
         System.out.println(name[i]);
      }
   }
   String peek() 
   {
      if (top < 0) 
      {
         System.out.println("Stack Underflow");
         return null;
      }
      else 
      {
         String s = name[top];
         return s;
      }
   }
   public static void main(String[] args) 
   {
      Scanner input = new Scanner(System.in);
      int opt = 0;
      Node start = null;
      Stack nameStk = new Stack();
      do {
        System.out.println("Enter the option: \n1 Push \n2 Pop \n3 Display \n4 Peek \n5 isEmpty \n6 Quit");
        opt = input.nextInt();
        switch (opt ) {
          case 1:
             System.out.print("Enter name to add in stack");
             String data = input.next(); 
             nameStk.push(data);
             break;
          case 2:
             String s = nameStk.pop();
             System.out.println(s);
             break;
          case 3:
             nameStk.display();
             break;
          case 4:
             s = nameStk.peek();
             System.out.println(s);
             break;
          case 5:
             if(nameStk.isEmpty() == true)
                System.out.println("Stack Empty");
             else
                System.out.println("Stack Not Empty");
             break;
           case 7:
             System.out.print("Enter string to check");
             data = input.nextLine(); 
             if(nameStk.parenthesisChecker(data) == true)
                System.out.print("Matched");
             else
                System.out.print("UnMatched");
             break;
          }
       } while (opt != 6);
   }
}
  • Share:
author avatar
Simply Coding

Previous post

CBSE 10 Computer Applications Marking Scheme (2020-21)
August 28, 2021

Next post

Single Linked List Programs in Java
August 28, 2021

You may also like

Single Linked List Programs in Java
Single Linked List Programs in Java
28 August, 2021
Constructor Programs
Constructor Programs
3 July, 2021
factorialProg
Solve any Factor Program in 10 minutes
3 July, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now