What is Stack?
It is basically pile of anything kept in such way that things are accessed in LIFO (Last In First Out) order. A very good example is when we stack the plates. And we pick up the last plate first, and when we put more plates, we stack them up.
Stack in computer language is an array of any data type elements / class objects where the data is pushed or stacked one on top of another and always the last element is popped or retrieved first. Here is the implementation of Stack class.
In the following example, two classes are created - Stack and StackDemo.
Class Stack is a simple representation of a stack. Here stack is in the form of a character array. There is a constructor for the class Stack, which creates a character array for the given size and set pointer to start of the array. All the data members of the class Stack are kept private to avoid direct access from other classes.
There are two methods push and pop - push method checks if the stack is full, if not puts the element into last available index and increases index value by 1. pop method checks if the stack is empty, if not pops the last index element from the stack and then decreases the index value by 1.
Class StackDemo simply shows how to use the push and pop method from the class Stack.
Class Stack (Stack.java)
Push to stack:
j a v a
Pop from stack:
a v a j Stack is empty
Push to stack:
s t a c k
Pop from stack:
k c a t s Stack is empty
In the following example, two classes are created - Stack and StackDemo.
Class Stack is a simple representation of a stack. Here stack is in the form of a character array. There is a constructor for the class Stack, which creates a character array for the given size and set pointer to start of the array. All the data members of the class Stack are kept private to avoid direct access from other classes.
There are two methods push and pop - push method checks if the stack is full, if not puts the element into last available index and increases index value by 1. pop method checks if the stack is empty, if not pops the last index element from the stack and then decreases the index value by 1.
Class StackDemo simply shows how to use the push and pop method from the class Stack.
Class Stack (Stack.java)
public class Stack { private char ch[]; private int size, lastIndex; Stack(int size) { this.size = size; ch = new char[size]; lastIndex = 0; } void push(char c) { if(lastIndex == size) { System.out.println("Stack is full"); return; } ch[lastIndex] = c; System.out.print(c + " "); lastIndex++; } void pop() { if(lastIndex == 0) { System.out.println("Stack is empty"); return; } System.out.print(ch[lastIndex-1] + " "); lastIndex--; } }Class StackDemo (StackDemo.java)
public class StackDemo { public static void main(String args[]) { Stack s1 = new Stack(10); System.out.println("Push to stack:"); s1.push('j'); s1.push('a'); s1.push('v'); s1.push('a'); System.out.println(""); System.out.println("Pop from stack:"); s1.pop(); s1.pop(); s1.pop(); s1.pop(); s1.pop(); System.out.println(""); System.out.println("Push to stack:"); s1.push('s'); s1.push('t'); s1.push('a'); s1.push('c'); s1.push('k'); System.out.println(""); System.out.println("Pop from stack:"); s1.pop(); s1.pop(); s1.pop(); s1.pop(); s1.pop(); s1.pop(); } }Output
Push to stack:
j a v a
Pop from stack:
a v a j Stack is empty
Push to stack:
s t a c k
Pop from stack:
k c a t s Stack is empty
No comments:
Post a Comment