Data Structure

How to Represent Stack in C – Data Structure


The stack provides for the insertion and deletion of items, so that a stack is a dynamic, constantly changing object. Before that, you must know what is data structure?

#define MAXSIZE 100
struct stack {
int items[MAXSIZE];
int top;

Note: If the value of s-top is 4 there are five elements on the stack i.e s.item[0], s.items[1], s.items[2], s.items[3], s.items[4]

When the stack is popped, the value of the stack is changed to 3 and indicates that there are now 4 elements and the top element is s.item[3].

When the new items are pushed onto the stack, the value of must be increased by 1 i.e becomes 5 and the new item is inserted into s.items[5].

To indicate the stack s to the empty stack we should initialize to -1

To determine whether the stack is full or not the condition -1 should be tested. 
Also to determine whether or not a stack or not a stack is empty, the condition == -1 should be tested. 
Implementing the POP operation

There is a possibility of underflow in implementing the POP operation, since the user may attempt to POP an element from an empty stack.

To avoid such conditions, we should create a function POP that performs the following three actions

If the stack is empty, print a warning message & halt the execution. 
Remove the top element from the stack. 
Return this element to the calling program. 
C Code to implement POP

int pop (struct stack *ps)
if (ps -> top == -1){
printf ("n Stack Underflow");

return (ps -> items[ps -> top--]);

/* top.item is returned and after that the top is decremented */

Implementing PUSH operation

A stack is a dynamic data structure that is constantly allowed to grow and shrink and thus change it’s size whereas an array has a predetermined size. 

The array implementation of stack’s push operation may thus sometimes outgrow the array size that was set aside to contain the stack.

The situation occurs when the array is full i.e when the stack contains as many elements as the array and an attempt is made to push yet another element onto the stack. The result of such an attempt is called an overflow. 

C code to implement push

void push (struct stack *ps, int r)
if(ps -> top == STACKSIZE-1)
printf("n Stack overflow");

printf("n Enter the value you want to push:");
ps-> items [ps-top]=x;


About Author provides tutorials related to tech and programmings. We are also setting up a community for the users and students.

You may also like

data types
Data Structure Differences

Difference Between Primitive and Non-Primitive Data Types

  • July 22, 2019
Primitive data type: In simple terms “data type” and “primitive data type” are simply known and used as interchangeable variables. Primitive
data structure
Data Structure What is & Queries

What is Data Structure? Why Do We Require This?

  • July 28, 2019
Question: What is Data Structure? Data is the basic factor entity that is utilized in calculation or manipulation. There are two different type