Java stack class example
Problem
Write a Java class to implement a stack data structure using an array for storage. The stack must support pop and push operations.
Java stack implementation
A stack is a LIFO (last in first out) data structure. A push operation adds an element to the top of the stack. On the other hand a pop operation deletes an element from the top of the stack. We can use a regular array to store the stack elements. Whenever we push to the stack we fill the next available position in the array. Whenever we pop from the stack we mark the last filled position as free. We need to check if the stack is full before we push a new element. We also need to make sure the stack is not empty before a pop operation.
Java stack source code
Here is a suggested solution in Java. The following is the source code of the Java Class followed by the driver main program. If you want to copy the source code please put each one into a separate source code file. The first code snippet should be saved into JavaStack.java and the second code snippet should be saved into TestStack.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
public class JavaStack { //Array A[] is used to store the stack elements private int A[]; //Index points to the position of the next element private int index = 0; //Array size (java stack size) private int size; //Constructor takes stack size as input public JavaStack(int s) { //Create an array of size s A = new int[s]; //Save size because we need it later size = s; } //java stack methods: //Java stack push public void Push(int x) { //Push only if the stack is not full if (index >= 0 && index < size) { //Push element to stack A[index] = x; //Do not forget to update the stack pointer index++; } } //Java stack pop public int Pop() { //Pop only if the stack is not empty if (index > 0 && index <= size) { //Do not forget to update the stack pointer //to remove the top element index--; //Return the result of the pop operation return A[index]; } else { //For simplicity return some negative number //to indicate an empty stack. We are assuming //the stack will not have an element of value //-9999 when it is not empty. return -9999; } } public void PrintStack() { System.out.print("Stack Values: "); for (int i = 0; i < index; i++) { System.out.print(A[i] + " "); } System.out.println(""); } } //Here is the driver program public class TestStack { public static void main(String[] args) { //Create a stack of size 5 JavaStack js = new JavaStack(5); //Push 5 elements to the stack js.Push(1); js.Push(2); js.Push(3); js.Push(4); js.Push(5); //Print the stack it should print "Stack Values: 1 2 3 4 5" js.PrintStack(); //Pop 3 elements int x = js.Pop(); int y = js.Pop(); int z = js.Pop(); //This should print //x = 5 //y = 4 //z = 3 System.out.println ("x = " + x); System.out.println ("y = " + y); System.out.println ("z = " + z); //This should print "Stack Values: 1 2" js.PrintStack(); //Pop 3 elements but we have only 2 so the //stack will be empty the third Pop must not cause problems js.Pop(); js.Pop(); js.Pop(); //This will print "Stack Values: " js.PrintStack(); //Push one element js.Push(1); //This will print "Stack Values: 1" js.PrintStack(); } } |
I hope this was a useful tutorial, if you have questions or comments please use the comments section below. Thanks for reading.