×

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

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.