• Home
  • University of the People
  • Data Structures (proctored course) CS 3303
  • Array-Based Stack Implementation Algorithm

Array-Based Stack Implementation Algorithm

Algorithm for implementing stack using array //array-based stack class Inspection_Station_Stack<E> implements Stack<E> { // declaring the initial size of the stack private static final int initial_Size = 3 // declaring the maximum size of the stack private int maximum_Size; // declares the index for the top item of the stack private int top; // declares the array that holds the item of the stack. private E[] Stack_Array; // defining the constructor Inspection_Station_Stack() { this (initial_Size); } Inspection_Station_Stack (initial_Size) { maximum_Size = size; top = 0; // initialize array for the stack Stack_Array = (E[]) new Object [size]; } //reinitialize the stack by assigning 0 to its first index public void stack_clear () { Top = 0 ; } // push method which pushes the item inspection into the stack public void push (E inspect_item) { assert top != maximum_Size : "Stack is not empty"; // statement to insert each item in the stack array // this is to ensure that the item is not pushed out of the array Stack_Array[top++] = inspect_item; } // the pop method which deletes the top element and prints its value // on the output screen public E pop( ) { // to check if an item exists on the top of the stack assert top != 0 : "stack is not full" ; // writing the top value on the console screen System.out.println("the stack removed the element: " + SArray[ -- top]); } // method that prints the value of the top item public E topValue() { assert top != 0 : "Stack is empty" ; System.out.println("the top item of the stack is: " + SArray[top-1]); } // method that returns the length of the stack public int stack_length() { return top; } Description of the algorithm Firstly, the stack is initialized by incorporating an array-based stack. Secondly, the size of the array is predefined so that the algorithm works for the finite number of items of the array. Thirdly, with incorporation of a for-loop, the stack will be filled with the number of items. Also, by incorporating another for-loop, the items from the stack will be popped out from its top for each inspection station. Then, after terminating the iteration, the algorithm will terminate itself. Analysis of the above detailed algorithm By considering the space complexity, the algorithm needs a space for storing the item in the stack and space for the pointer to the next item in the stack. This means that for a stack of size n, there would be a requirement of size 2n for the array-based stack. In the initial phase of the algorithm, there is a call for the new operator whose cost will depend on the size n. The cost of this function will be O(n) for the best, worst, and average cases. The first for-loop pushes a single item in each of its iterations, so the cost for this part of the algorithm will be O(n) for the best, the worst, and the average cases. The