Stack in C
A stack is a linear dynamic data structure that follows Last-In/First-Out (LIFO) principle. In a stack, addition of a new element and deletion of an element occurs at the same end which implies that the element which is added last in the stack will be the first to be removed from the stack.
Features of stack
- It is a dynamic data structure.
- It has dynamic size.
- It uses dynamic memory allocation.
Operations of a stack
- isEmpty(): Checks whether the stack is empty or not.
- size(): Returns the size of the stack.
- topElement(): Returns the top element of the stack.
- push(x): Adds a new element ‘x’ at the top of the stack. Consequently, size of the stack increases by 1.
- pop(): Deletes the top element of the stack. Consequently, size of the stack decreases by 1.
Implementation of Stack
#include <stdio.h> #define MAX 100 //define structure of a stack struct stack { int array[MAX]; int top; }; typedef struct stack stk; void CreateStack(stk *s) { s->top = -1; } // create a function to check whether // the stack is empty or not void isEmpty(stk *s) { if(s->top == -1) { printf("Stack is empty.\n"); } else { printf("Stack is not empty.\n"); } } //create a function to return size of the stack int size(stk *s) { return s->top+1; } //create a function to add new element void push(stk *s, int x){ if(s->top == (MAX - 1)){ printf("Stack size limit reached.\n"); } else { s->array[++s->top] = x; printf("%i is added into the stack.\n", x); } } //create a function to delete top element void pop(stk *s){ if(s->top < 0){ printf("Stack is empty.\n"); } else { int x = s->array[s->top--]; printf("%i is deleted from the stack.\n", x); } } //create a function to get top element int topElement(stk *s) { if(s->top < 0) { printf("Stack is empty.\n"); return 0; } else { return s->array[s->top]; } } // test the code int main() { stk s; stk *MyStack; MyStack = &s; CreateStack(MyStack); push(MyStack, 10); push(MyStack, 20); push(MyStack, 30); push(MyStack, 40); pop(MyStack); isEmpty(MyStack); return 0; }
The above code will give the following output:
10 is added into the stack. 20 is added into the stack. 30 is added into the stack. 40 is added into the stack. 40 is deleted from the stack. Stack is not empty.
Recommended Pages
- C Program - To Check Prime Number
- C Program - Bubble Sort
- C Program - Selection Sort
- C Program - Maximum Subarray Sum
- C Program - Reverse digits of a given Integer
- C - Swap two numbers
- C Program - Fibonacci Sequence
- C Program - Insertion Sort
- C Program - Find Factorial of a Number
- C Program - Find HCF of Two Numbers
- C Program - To Check Whether a Number is Palindrome or Not
- C Program - To Check Whether a String is Palindrome or Not
- C Program - Heap Sort
- C Program - Quick Sort
- C - Swap Two Numbers without using Temporary Variable
- C Program - To Check Armstrong Number
- C Program - Counting Sort
- C Program - Radix Sort
- C Program - Find Largest Number among Three Numbers
- C Program - Print Floyd's Triangle