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.

Representation of Array Stack in C

typedef struct stack  {
     int top;
     int capacity;
     type* arr;
}Stack;

Here, we typedef is used for convenience for shorter struct declaration. It is recommended to define the array as the last element as to avoid overwriting the top variable in case of out of bound writing of array. We can define the size of the array dynamically using desired value.

Initially, we set the top index pointer to -1 representing there are no elements in the stack. We can then implement the basic stack operation according to this representation.

The below illustration shows the working of array stack in C:

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:...