×

Stack implementation in C

Stack implementation in C

Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where the elements added at the end will be the first ones to eliminate while the elements added in the beginning will be the last ones to be eliminated.

Two particular operations can be performed on the stack concept, such as Push operation and pop operation. The push operation performed will do the following: inserts an element into the stack.

On the other hand, the pop operation pops or removes the element from the inserted stack. Two other critical undesirable conditions will be occurred at times, like when the stack is complete. Yet, the programmer tries to insert another element, and then the stack overflow happens as the memory space will not be available. On the contrary, if the stack is empty but the developer tries to pop the operation, then it results in the stack underflow.

Using a stack in C requires no extra memory for storing the pointers. But the stack size will be pre-determined, and it is not possible to increase or decrease the size of the stack.

Implementation of the stack:

The essential functions that are allowed in the stack are:

  • Push: the push operation will add or insert an element on the top of the stack.
  • Pop: the pop operation will remove the element in a mannered way
  • Top: the top function displays the element which is present at the top of the stack.
  • isEmpty: this function checks whether the stack is empty or not.
  • isFull: this function checks whether the stack is complete or not.

E.g.:

 #include<stdio.h>
 #include<stdlib.h>
 #define MAX 5 //Maximum number of elements that can be stored
 int top=-1,stack[MAX];
 void push();
 void pop();
 void display();
 void main()
 {
 int ch;
 while(1) //infinite loop, will end when the choice will be 4
 {
 printf("\n*** Stack Menu ***");
 printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
 printf("\n\nEnter your choice(1-4):");
 scanf("%d",&ch);
 switch(ch)
 {
 case 1: push();
 break;
 case 2: pop();
 break;
 case 3: display();
 break;
 case 4: exit(0);
 default: printf("\nWrong Choice!!");
 }
 }
 }
 void push()
 {
 int val;
 if(top==MAX-1)
 {
 printf("\nStack is full!!");
 }
 else
 {
 printf("\nEnter element to push:");
 scanf("%d",&val);
 top=top+1;
 stack[top]=val;
 }
 }
 void pop()
 {
 if(top==-1)
 {
 printf("\nStack is empty!!");
 }
 else
 {
 printf("\nDeleted element is %d",stack[top]);
 top=top-1;
 }
 }
 void display()
 {
 int i;
 if(top==-1)
 {
 printf("\nStack is empty!!");
 }
 else
 {
 printf("\nStack is...\n");
 for(i=top;i>=0;--i)
 printf("%d\n",stack[i]);
 }
 } 

Output:

 *** Stack Menu ***
 1.Push
 2.Pop
 3.Display
 4.Exit
 Enter your choice(1-4):1
 Enter element to push: 3
 *** Stack Menu ***
 1.Push
 2.Pop
 3.Display
 4.Exit
 Enter your choice(1-4): 1
 Enter element to push: 6
 *** Stack Menu ***
 1.Push
 2.Pop
 3.Display
 4.Exit
 Enter your choice(1-4): 1
 Stack is...
 6  3
 *** Stack Menu ***
 1.Push
 2.Pop
 3.Display
 4.Exit
 Enter your choice(1-4): 2
 Deleted element is 6
 *** Stack Menu ***
 1.Push
 2.Pop
 3.Display
 4.Exit
 Enter your choice(1-4): 5
 Wrong choice
 *** Stack Menu ***
 1.Push
 2.Pop
 3.Display
 4.Exit
 Enter your choice(1-4): 4 

There are many real-life examples of a stack. Consider the simple example of plates stacked over one another in a canteen. The plate which is at the top is the first to be removed, i.e., the plate placed at the bottommost position remains in the stack for the most extended period of time. So, it can be simply seen to follow the LIFO/FILO order.

Time complexity:

While performing the push or the pop operations, it always takes O(1) time.

Applications:

Stack is generally used in problems such as:

  • Tower of Hanoi
  • Infix conversions and prefix conversions
  • Queens problem
  • Balancing the symbols
  • Undo and redo operations while editing and photoshopping
  • Forward and backward features in the web browser.
  • Tree traversals
  • Histogram problems.
  • In memory management, any modern computer uses a stack as the primary management for a running purpose. Each program that is running in a computer system has its memory allocations
  • String reversal is also another application of stack. Here one by one, each character gets inserted into the stack. So the string’s first character is on the bottom of the stack, and the last element of a string is on the top of the stack. So after Performing the pop operations on the stack, we get the string in reverse order.

Related Topics

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.