Stack

Ads
 

Stack

How to implement a stack using classes in java?

View Answers

August 13, 2012 at 5:38 PM

Here is an example that implements a Stack class based on arrays.

public class Stack {

      private int top;

      private int[] storage;



      Stack(int capacity) {

            if (capacity <= 0)

                  throw new IllegalArgumentException(

                             "Stack's capacity must be positive");

            storage = new int[capacity];

            top = -1;

      }



      void push(int value) {

            if (top == storage.length)

                  throw new StackException("Stack's underlying storage is overflow");

            top++;

            storage[top] = value;

      }



      int peek() {

            if (top == -1)

                  throw new StackException("Stack is empty");

            return storage[top];

      }



      void pop() {

            if (top == -1)

                  throw new StackException("Stack is empty");

            top--;

      }



      boolean isEmpty() {

            return (top == -1);

      }



      public class StackException extends RuntimeException {

            public StackException(String message) {

                  super(message);

            }

      }

}

Ads









Related Tutorials/Questions & Answers:
Stack
Stack  How to implement a stack using classes in java?   Here is an example that implements a Stack class based on arrays. public class Stack { private int top; private int[] storage; Stack(int
stack in java/
stack in java/  java code for stack push pop and display operation? thanks
Advertisements
Array stack
Array stack  Write a stack class ArrayStack.java implements PureStack interface that reads in strings from standard input and prints them in reverse order
Array stack
Array stack  Write a stack class ArrayStack.java implements PureStack interface that reads in strings from standard input and prints them in reverse order
What is a stack overflow error?
What is a stack overflow error?  What is a stack overflow error
Java Stack
Java Stack       The Stack class works on the principle last-in-first-out (LIFO) stack of objects... a vector to be treated as a stack. Methods push() and pop() are used to pick
Stack Overflow - Java Tutorials
Stack Overflow in Java A stack is the part of the memory. The local automatic variable is created on this stack and method arguments are passed. When a process starts, it get a default stack size which is fixed for each process. 
Stack and Queue
Stack and Queue  Consider a class called Configuration. This class √Ę‚?¨Ň?encapsulates√Ę‚?¨¬? the configuration management for any software system... or a Stack. Your App class is given and you should not change this class at all. Your
java technologies stack
java technologies stack  java technologies stack
stack using linked list
stack using linked list  how to implement stack using linked list
ModuleNotFoundError: No module named 'stack'
ModuleNotFoundError: No module named 'stack'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'stack' How to remove the ModuleNotFoundError: No module named 'stack'
heap and stack memory
heap and stack memory   hi i am jane pls explain the difference between heap memory and stack memory with example programs
Java collection Stack example
Java collection Stack example  How to use Stack class in java collection?   The Stack class uses the First In Last Out(FILO) mechanism. Here is an example of Stack class. import java.util.Stack; public class
can Stack accept imageIcon ..??
can Stack accept imageIcon ..??  how can write program like album ..by using Stack as an ideas datastracture and using imageIconb ?? please help me ..thanks alot
remove element from stack
on the internet how to remove names from stack by asking user how many names they want to remove and if the number they enter is bigger than the stack number then show error message and if not then remove the names from stack until the stack is empty
heap and stack memory
heap and stack memory   hi i am jane pls explain the difference between heap memory and stack memory   STACK memory is referred as temporary memory,if you come out of the program the memory
Java Stack Example
Java Stack Example In this section you will learn about Stack class  in java, how to use Stack in java. Before going into details we should know what is stack class, stack  class represent Last In First Out(LIFO) of object
Collection : Stack Example
Collection : Stack Example In this tutorial we will describe implementation of stack with example. Stack : Stack is based on concept of LIFO(Last In First... one access point that is top of stack. There are two main methods of stack
Generic Java Stack
- package jp.co.worksap.intern; /** *The Stack class represents a last-in-first-out(LIFO) stack of objects *And this class can look at the object which has the highest (lowest or so) value. *So every object onto the stack
{create stack with queue}
{create stack with queue}  how can create queue that accept all data type ?? meanse that this array is used accept all type for example(int ,char,String,float).?? please help me ....which output is : 1- to create queue 2-Equeue
ModuleNotFoundError: No module named 'alchemist-stack'
ModuleNotFoundError: No module named 'alchemist-stack'  Hi, My... named 'alchemist-stack' How to remove the ModuleNotFoundError: No module named 'alchemist-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'alchemist-stack'
ModuleNotFoundError: No module named 'alchemist-stack'  Hi, My... named 'alchemist-stack' How to remove the ModuleNotFoundError: No module named 'alchemist-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'alchemist-stack'
ModuleNotFoundError: No module named 'alchemist-stack'  Hi, My... named 'alchemist-stack' How to remove the ModuleNotFoundError: No module named 'alchemist-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'beanstalk-stack'
ModuleNotFoundError: No module named 'beanstalk-stack'  Hi, My... named 'beanstalk-stack' How to remove the ModuleNotFoundError: No module named 'beanstalk-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-stack-it'
ModuleNotFoundError: No module named 'django-stack-it'  Hi, My... named 'django-stack-it' How to remove the ModuleNotFoundError: No module named 'django-stack-it' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'flapjack-stack'
ModuleNotFoundError: No module named 'flapjack-stack'  Hi, My... named 'flapjack-stack' How to remove the ModuleNotFoundError: No module named 'flapjack-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'focus-stack'
ModuleNotFoundError: No module named 'focus-stack'  Hi, My Python... 'focus-stack' How to remove the ModuleNotFoundError: No module named 'focus-stack' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'hmsdbmitc-stack'
ModuleNotFoundError: No module named 'hmsdbmitc-stack'  Hi, My... named 'hmsdbmitc-stack' How to remove the ModuleNotFoundError: No module named 'hmsdbmitc-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'hmsdbmitc-stack'
ModuleNotFoundError: No module named 'hmsdbmitc-stack'  Hi, My... named 'hmsdbmitc-stack' How to remove the ModuleNotFoundError: No module named 'hmsdbmitc-stack' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'py_stack'
ModuleNotFoundError: No module named 'py_stack'  Hi, My Python..._stack' How to remove the ModuleNotFoundError: No module named 'py_stack... to install padas library. You can install py_stack python with following
ModuleNotFoundError: No module named 'python-stack'
ModuleNotFoundError: No module named 'python-stack'  Hi, My Python... 'python-stack' How to remove the ModuleNotFoundError: No module named 'python-stack' error? Thanks   Hi, In your python

Ads