Implementation of Stack Operations in C

There are five basic stack operations:

Operation

Description

Time Complexity

Space Complexity

Push

Add one element to the top of stack.

O(1)

O(1)

Pop

Delete one element from the top of stack.

O(1)

O(1)

Peek

Return the top element.

O(1)

O(1)

IsFull

Returns true is stack is full. If not, returns false.

O(1)

O(1)

IsEmpty

Returns true is stack is empty. If not, returns false.

O(1)

O(1)

1. isFull Implementation

The top pointer will always point to the index that have the current top element. So the stack full condition will be top is greater that or equal to the MAX_SIZE i.e. the size of the array.

Algorithm

  • If top == capacity – 1, return true.
  • Else, return false

2. isEmpty Implementation

We have set the top = -1 initially when there were no element. This will be true for whenever the stack is empty.

Algorithm

  • If top == -1, return true.
  • Else, return false

3. Push Implementation

The push operation will be performed at the top of the stack. The top variable points to the current top element, so we need to increment it to insert the new element the top. But before, we need to check whether the given stack is full or not. Pushing an element while the stack is full is called stack overflow.

Algorithm

  • If stack is full, print stack overflow and return.
  • Else, increment the top and insert the new element at arr[top].

4. Pop Implementation

The pop operation is also performed at the top. But in array, we cannot remove the element directly. So, we just decrement the top pointer effectively removing the current top element. But removing element from the stack that is empty leads to the stack underflow condition.

  • If the stack is empty (top == -1), print “stack underflow” and return.
  • Otherwise, decrement the top.

5. Peek Implementation

The peek operation returns the top element of the stack without removing it. We can access the top element as arr[top].

Algorithm

  • If the stack is empty, print “Stack is Empty!” and return -1.
  • Else, return arr[top].

Implementation of Stack Using Array in C

A stack is a linear data structure that follows the Last In First Out (LIFO) principle. This means that the most recently added element is the first one to be removed. In this article, we will learn how to implement a stack using an array in C.

Similar Reads

Implementation of Stack Using Arrays in C

In the array-based implementation of a stack, we use an array to store the stack elements. The top of the stack is represented by the end of the array. Hence, we keep an index pointer named top. We can also enclose this array and index pointer in the structure data type....

Implementation of Stack Operations in C

There are five basic stack operations:...

C Program to Implement Stack Using Arrays

C // C Program to implement stack along with its basic // operation using arrays #include #include #include #define MAX_SIZE 100 // A structure to represent a stack struct Stack { int top; int capacity; int* array; }; // Function to create a stack of given capacity. It // initializes size of stack as 0 struct Stack* createStack(unsigned capacity) { struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); stack->capacity = capacity; stack->top = -1; stack->array = (int*)malloc(stack->capacity * sizeof(int)); return stack; } // Stack is full when top is equal to the last index int isFull(struct Stack* stack) { return stack->top == stack->capacity - 1; } // Stack is empty when top is equal to -1 int isEmpty(struct Stack* stack) { return stack->top == -1; } // Function to add an item to stack. It increases top by 1 void push(struct Stack* stack, int item) { if (isFull(stack)) { printf("Overflow\n"); return; } stack->array[++stack->top] = item; printf("%d pushed to stack\n", item); } // Function to remove an item from stack. It decreases top // by 1 int pop(struct Stack* stack) { if (isEmpty(stack)) { printf("Underflow\n"); return INT_MIN; } return stack->array[stack->top--]; } // Function to return the top from stack without removing it int peek(struct Stack* stack) { if (isEmpty(stack)) { printf("Stack is empty\n"); return INT_MIN; } return stack->array[stack->top]; } // Function to display stack elements void display(struct Stack* stack) { if (isEmpty(stack)) { printf("Stack is empty\n"); } else { for (int i = stack->top; i >= 0; i--) { printf("%d\n", stack->array[i]); } } } // Driver program to test above functions int main() { struct Stack* stack = createStack(100); push(stack, 1); push(stack, 2); push(stack, 3); printf("%d popped from stack\n", pop(stack)); printf("Top element is %d\n", peek(stack)); printf("Elements present in stack:\n"); display(stack); return 0; }...

Other Implementations of Stack

Apart from the array, we can also implement the stack in the following ways:...