Stacks using arrays

the stack is a type of data structure.this is LIFO (Last In First Out) type of data structure .in the stack, elements can be added or removed only from one end

creating a stack data structure using an array

define a MAX variable, assign any MAXvalue

#define max 5

create a stack of integer and assign MAX variable as its size

int stack[max]

stack follows (LIFO)last in first out, we need to keep track of top element, let’s declare a top variable assign -1 ,-1 indicates that there is no elements

int top=-1

push elements into a stack

if stack is full,display “stack over flow”,the condition of stackoverflow is

top == MAX-1

increment top value by one and use top variable as an index number for stack variable,push data into specific location

stack[++top] = data;

pop elements from a stack

if top<0 then display “satck under flow”

display top most element and decrement top by 1

printf("\n\npopped element is: %d ", stack[top--]);
Last updated on by vishal devxo