Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Representation of Stack using Array

Last Updated on May 5, 2023 by Prepbytes

Stack is a linear data structure that executes all operations according to the LIFO (Last In First Out) principle. Only one end of the stack, starting from the top, is used for insertion and deletion operations. Push operations and pop operations both involve removing data elements from the top of the stack. Push operations include adding a new element to the top of the stack. Stack implementation using array and stack implementation using linked-list are two data structures you may use to implement the stack in memory.

Implementation of Stack using Array

In doing a representation of a stack using an array, the arrays are used to create the stack. Arrays are used for all activities related to the stack implementation.

Many operations are performed while implementing stack using the array.

  1. Push operation
  2. Pop operation
  3. Peek operation

Push Operation

A push operation occurs when you add an element to the top of the stack. The next two steps are involved in the push operation:

  1. The top variable on the stack should be increased so it may refer to the next memory address.
  2. At the top of the increment, add a data element.

When you attempt to put an element into the stack after it is complete, the stack data structure declares an overflow situation.

Algorithm for push operation
Algorithm for push operation while doing representation of stack using array:

  • Begin
  • If top == n
  • The stack is completely filled
  • top = top + 1
  • Stack (top) = item
  • end

Pop Operation

The term "pop operation" refers to the removal of a data element from the stack data structure. The pop operation has the four phases listed below:

  • Every time you remove an item from the stack, the top variable’s value is increased by one.
  • The variable at the top of the stack is put in a different variable, and its value is then reduced by one.
  • The deleted element that was saved in another variable as a result is returned by the pop operation.
  • When a data element is attempted to be deleted when the stack is already empty, the stack data structure reports an underflow problem.

Algorithm for pop operation
Algorithm for pop operation while doing representation of stack using array:

  • Begin
  • if top = 0
  • stack is completely empty
  • item = stack(top)
  • top = top – 1
  • end

Peek Operation

In peek operations, the topmost data element of the stack is returned without being removed from the stack.
If you attempt to return the top-most element while the stack is empty, underflow circumstances could happen.

Algorithm for peek operation
Algorithm for pop operation while doing representation of stack using array:

  • Begin
  • if top = -1
  • stack is empty
  • item = stack[top]
  • return data
  • end

Code Implementation of Stack using Array

Here is a representation of a stack using an array with all the operations i.e.push, pop and peek operations,

#include <iostream>
using namespace std;
#define MAX_SIZE 500 

class Stack {
private:
    int top; 
    int arr[MAX_SIZE]; 
    
public:
    Stack() {
        top = -1; 
    }

    bool push(int x) {
        if (top >= MAX_SIZE - 1) {
            cout << "Stack is full" << endl;
            return false;
        }
        arr[++top] = x; 
        return true;
    }

    int pop() {
        if (top < 0) {
            cout << "Stack underflow" << endl;
            return 0;
        }
        return arr[top--]; 
    }
    int peek() {
        if (top < 0) {
            cout << "Stack is empty" << endl;
            return 0;
        }
        return arr[top]; 
    }
    bool isEmpty() 
    {
        return (top < 0); 
    }
    void display()
    {
        if (top < 0) 
        {
            cout << "Stack is empty" << endl;
            return;
        }
        cout << "\nStack elements: ";
        for (int i = top; i >= 0; i--)
            cout << arr[i] << " ";
        cout << endl;
    }
};
int main() {

    Stack s; 
    cout << "Check the stack is empty or not? "<<endl;
    cout << s.isEmpty() << endl;
    cout << "Insert some elements onto the stack:"<<endl;
    s.push(4); 
    s.push(3);
    s.push(2);
    s.push(1);
    s.display();
    cout << "Remove an element from the stack! "<<endl;
    cout << s.pop(); 
    s.display();
    cout << "Top of the element of the stack:"<<endl;
    cout << s.peek();     
    cout << endl;
    return 0;
}

Output

Check the stack is empty or not? 
1
Insert some elements onto the stack:

Stack elements: 1 2 3 4 
Remove an element from the stack! 
1
Stack elements: 2 3 4 
Top of the element of the stack:
2

Conclusion
A linear data structure known as a stack employs LIFO functionality. Push, pop, peek, empty, and full functions are supported by a stack that may be constructed using an array. The push and pop functions are used, respectively, to add and remove components from the stack.
The top of the stack is maintained using a variable in the stack implementation of the array, and the stack has a fixed size that cannot be expanded in the future.
When compared to other methods, using an array to implement a stack takes much less time.

Frequently Asked Questions related to Representation of Stack using Array

Q1. What are the drawbacks of stack representation versus an array?
Ans. Since the stack’s size is fixed, using an array to implement the stack cannot be increased or decreased. An array keeps the elements in sequential memory regions, making insertion and deletion challenging.

Q2. How long does it take to implement a stack using an array?
Ans. In the worst case scenario, time would be O(n) if an array implementation of a stack had an array that was fully filled, then the array size needed to be altered, and all the items needed to be transferred from one array to the other.

Q3. Can various data types be included in a stack?
Ans. You can add different kinds of data types to a stack.

Q4. Why are stack and heap faster?
Ans. Stack memory size is significantly less than heap memory. Stack memory is much faster than heap memory due to the ease of memory allocation (LIFO).

Q5. What is a stack’s time complexity?
Ans. Due to the necessity of popping the items stored before it, accessing any given element requires an overall search time of O(N). In a stack, operations like insertion and deletion take constant time, or O(1).

Leave a Reply

Your email address will not be published. Required fields are marked *