- DSA using Java - Discussion
- DSA using Java - Useful Resources
- DSA using Java - Quick Guide
- DSA using Java - Recursion
- DSA using Java - Sorting techniques
- DSA using Java - Search techniques
- DSA using Java - Graph
- DSA using Java - Heap
- DSA using Java - Hash Table
- DSA using Java - Tree
- DSA using Java - Priority Queue
- DSA using Java - Queue
- DSA - Parsing Expressions
- DSA using Java - Stack
- DSA using Java - Circular Linked List
- DSA using Java - Doubly Linked List
- DSA using Java - Linked List
- DSA using Java - Array
- DSA using Java - Data Structures
- DSA using Java - Algorithms
- DSA using Java - Environment Setup
- DSA using Java - Overview
- DSA using Java - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
DSA using Java - Stack
Overview
Stack is kind of data structure which allows operations on data only at one end. It allows access to the last inserted data only. Stack is also called LIFO (Last In First Out) data structure and Push and Pop operations are related in such a way that only last item pushed (added to stack) can be popped (removed from the stack).
Stack Representation
We re going to implement Stack using array in this article.
Basic Operations
Following are two primary operations of a stack which are following.
Push − push an element at the top of the stack.
Pop − pop an element from the top of the stack.
There is few more operations supported by stack which are following.
Peek − get the top element of the stack.
isFull − check if stack is full.
isEmpty − check if stack is empty.
Push Operation
Whenever an element is pushed into stack, stack stores that element at the top of the storage and increments the top index for later use. If storage is full then an error message is usually shown.
// push item on the top of the stack pubpc void push(int data) { if(!isFull()){ // increment top by 1 and insert data intArray[++top] = data; }else{ System.out.println("Cannot add data. Stack is full."); } }
Pop Operation
Whenever an element is to be popped from stack, stack retrives the element from the top of the storage and decrements the top index for later use.
// pop item from the top of the stack pubpc int pop() { // retrieve data and decrement the top by 1 return intArray[top--]; }
Stack Implementation
Stack.java
package com.tutorialspoint.datastructure; pubpc class Stack { private int size; // size of the stack private int[] intArray; // stack storage private int top; // top of the stack // Constructor pubpc Stack(int size){ this.size = size; intArray = new int[size]; //initiapze array top = -1; //stack is initially empty } // Operation : Push // push item on the top of the stack pubpc void push(int data) { if(!isFull()){ // increment top by 1 and insert data intArray[++top] = data; }else{ System.out.println("Cannot add data. Stack is full."); } } // Operation : Pop // pop item from the top of the stack pubpc int pop() { //retrieve data and decrement the top by 1 return intArray[top--]; } // Operation : Peek // view the data at top of the stack pubpc int peek() { //retrieve data from the top return intArray[top]; } // Operation : isFull // return true if stack is full pubpc boolean isFull(){ return (top == size-1); } // Operation : isEmpty // return true if stack is empty pubpc boolean isEmpty(){ return (top == -1); } }
Demo Program
StackDemo.java
package com.tutorialspoint.datastructure; pubpc class StackDemo { pubpc static void main (String[] args){ // make a new stack Stack stack = new Stack(10); // push items on to the stack stack.push(3); stack.push(5); stack.push(9); stack.push(1); stack.push(12); stack.push(15); System.out.println("Element at top of the stack: " + stack.peek()); System.out.println("Elements: "); // print stack data while(!stack.isEmpty()){ int data = stack.pop(); System.out.println(data); } System.out.println("Stack full: " + stack.isFull()); System.out.println("Stack empty: " + stack.isEmpty()); } }
If we compile and run the above program then it would produce following result −
Element at top of the stack: 15 Elements: 15 12 1 9 5 3 Stack full: false Stack empty: trueAdvertisements