![]() |
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. Implementation of Stack Using Arrays in CIn 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 Ctypedef 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 Operations in CThere are five basic stack operations:
1. isFull ImplementationThe 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
2. isEmpty ImplementationWe have set the top = -1 initially when there were no element. This will be true for whenever the stack is empty. Algorithm
3. Push ImplementationThe 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
4. Pop ImplementationThe 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.
5. Peek ImplementationThe peek operation returns the top element of the stack without removing it. We can access the top element as arr[top]. Algorithm
C Program to Implement Stack Using Arrays
Output 1 pushed to stack 2 pushed to stack 3 pushed to stack 3 popped from stack Top element is 2 Elements present in stack: 2 1 Advantages of Array Implementation of Stack in C
Limitation of Array Implementation of Stack in C
Other Implementations of StackApart from the array, we can also implement the stack in the following ways: |
Reffered: https://www.geeksforgeeks.org
C Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 18 |