Join Regular Classroom : Visit ClassroomTech

Basic Implementation of Stack using Array – Codewindow

Hot Topics

Data Structure

Stack using array:

 

Algorithm

We know stack is a normal array which follows LIFO.So to create stack we will need an array,the size of the array and a variable which will determine the top element of stack.

push():To push element into stack we need to make sure that there is atleast 1 empty space in the stack to determine that we have to subtract top from the size of the stack.

pop():To take out elements from stack we need to make sure that there must be an element present in the stack i.e the top variable must be pointing to an index inside stack.

peak():The element pointed by the top variable will give top element.

empty():The stack can be said as empty when the top is not pointing to an index which is a part of stack.

Code:


class Stack
{
public:
    // creating the variables required to implement stack
    int *arr;
    int size;
    int top;

    // Constructor function which will create the desired stack
    Stack(int size)
    {
        /*this->size is denoting the size variable we created to
implement stack and size represents the parameter
taken as input in Stack constructor function*/
        this->size = size;
        /*at first stack is empty so top is pointing to -1 which is not
          a part of stack as stack indexing starts from 0*/
        top = -1;
        // creating an array of the given size
        arr = new int[size];
    }

    // creating the push function to push element into stack
    void push(int element)
    {
        /*to push we need atleast 1 empty position in stack so we
are checking if there is atleast 1 empty space in stack.*/
        if (size - top > 1)
        {
            /*at first to insert element into stack we need to
        bring top variable point to 0 as it is a part of stack
index and -1 is not.Then once we start with every
insertion   we need to increase top so that it points
to next empty location.*/
            arr[top++] = element;
        }
    } // end of push

    // function to remove element from stack
    void pop()
    {
        /*we can remove element only when there is atleast one
         element present and that can be determined when top is
         pointing to 0 or to some index greater than 0*/
        if (top >= 0)
        {
            /*reassigning top after removing the previous
element from stack*/
            top--;
        }
    } // end of pop

    // function for getting the top most element in stack
    int peak()
    {
        // checking if top is pointing to an index of stack
        if (top >= 0)
        {
            // returning the value in that index
            return arr[top];
        }
    } // end of peak

    // checking if stack is empty
    bool empty()
    {
        /*indexing of stack starts from 0 and goes in positive
direction.So if top is pointing to -1 means it is not a part
of stack and can be said as stack is empty.*/
        if (top == -1)
        {
            cout <<”Stack is empty” << endl;
        }
    } // end of empty

} // end of stack class

int main()
{
    // Creating a stack called s.
    class Stack s;
    s.push(10);
    s.push(20);
    s.push(30);
    cout << s.pop() << " Popped from stack\n";

    // print top element of stack after popping
    cout << "Top element is : " << s.peak() << endl;

    // print all elements in stack :
    cout << "Elements present in stack : ";
    while (!s.isEmpty())
    {
        // print top element in stack
        cout << s.peak() << " ";
        // remove top element in stack
        s.pop();
    }

    return 0;
}

Nagarro Solved

Automata Fixing

      

We Love to Support you

Go through our study material. Your Job is awaiting.

Recent Posts
Categories