JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java Dynamic Stack Implementation


In this example, the stack will never comes to stack overflow case, beacuse its capacity will keep increases as it reaches to max capacity, so it is very dynamic in capacity.


package com.java2novice.ds.stack;

public class MyDynamicStack {

        private int stackSize;
        private int[] stackArr;
        private int top;

        /**
         * constructor to create stack with size
         * @param size
         */
        public MyDynamicStack(int size) {
                this.stackSize = size;
                this.stackArr = new int[stackSize];
                this.top = -1;
        }

        /**
         * This method adds new entry to the top 
         * of the stack
         * @param entry
         * @throws Exception 
         */
        public void push(int entry){
                if(this.isStackFull()){
                        System.out.println(("Stack is full. Increasing the capacity."));
                        this.increaseStackCapacity();
                }
                System.out.println("Adding: "+entry);
                this.stackArr[++top] = entry;
        }

        /**
         * This method removes an entry from the 
         * top of the stack.
         * @return
         * @throws Exception 
         */
        public int pop() throws Exception {
                if(this.isStackEmpty()){
                        throw new Exception("Stack is empty. Can not remove element.");
                }
                int entry = this.stackArr[top--];
                System.out.println("Removed entry: "+entry);
                return entry;
        }
        
        /**
         * This method returns top of the stack
         * without removing it.
         * @return
         */
        public long peek() {
                return stackArr[top];
        }

        private void increaseStackCapacity(){
                
                int[] newStack = new int[this.stackSize*2];
                for(int i=0;i<stackSize;i++){
                        newStack[i] = this.stackArr[i];
                }
                this.stackArr = newStack;
                this.stackSize = this.stackSize*2;
        }
        
        /**
         * This method returns true if the stack is 
         * empty
         * @return
         */
        public boolean isStackEmpty() {
                return (top == -1);
        }

        /**
         * This method returns true if the stack is full
         * @return
         */
        public boolean isStackFull() {
                return (top == stackSize - 1);
        }

        public static void main(String[] args) {
                MyDynamicStack stack = new MyDynamicStack(2);
                for(int i=1;i<10;i++){
                        stack.push(i);
                }
                for(int i=1;i<4;i++){
                        try {
                                stack.pop();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
        }
}

Output:
Adding: 1
Adding: 2
Stack is full. Increasing the capacity.
Adding: 3
Adding: 4
Stack is full. Increasing the capacity.
Adding: 5
Adding: 6
Adding: 7
Adding: 8
Stack is full. Increasing the capacity.
Adding: 9
Removed entry: 9
Removed entry: 8
Removed entry: 7
<< Previous Program | Next Program >>

List of Stack Data Structure Examples

  1. Stack introduction & implementation
  2. Java Dynamic Stack Implementation
  3. Stack implementation using generics bounded type.
  4. Reverse a word or string using Stack data structure.
  5. Write a program to find out delimiter matching using stack.
  6. Convert a decimal into a binary number using stack.
  7. Towers of Hanoi implementation using stack.
  8. Evaluation of an infix expression that is fully parenthesized using stack in java.
Knowledge Centre
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having 10 elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
Famous Quotations
There is a great difference between worry and concern. A worried person sees a problem, and a concerned person solves a problem.
-- Harold Stephens

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2024 by Nataraja Gootooru. All Rights Reserved.