C Program to Perform Stack Operations Using Pointer !


Write a C program using pointers to implement a stack with all the operations.

Program to implement stack with its operations using pointers

Output :

Explanation of Program :

We are declaring the stack with MAX size specified by the preprocessor.

As soon as after defining the stack we are initializing the top location of stack to -1. We are passing the structure to the function using pointer thus we can see the “struct stack*” as data type in the function call.

Whenever we are accessing the member of the structure using the structure pointer then we are using arrow operator. If we have to access the top element of stack then we are writing

Similarly if we have to access oth element of the stack array then we can write –

Since we have to access the topmost element we can write it as –

Procedure of Pushing :

  1. Check whether the size reached upto the maximum or not,
  2. Increment the top
  3. Insert the element.

Similar steps are depicted in the code –

Procedure of Poping element :

  1. Check whether the size reached upto the minimum or not(underflow)
  2. Remove Element
  3. Decrement the top