Tutorial Playlist
A stack in C refers to a linear data structure that allows the insertion of a new element and deletion of an existing element at the same end which is depicted as the top of the stack.
For stack implementation, the pointer must be maintained at the top of the stack, i.e., the last element to be inserted in the stack. You can access the elements only at the top of the stack.
The concept of the stack is widely used in backtracking, expression evaluation, recursion, and execution of other data structures like queues.
You can manipulate a stack using certain stack operations in C. The push and pop in stack in C are the two widely used stack operations. Let’s look at the briefs of all basic operations on the stack.
push() | It inserts an element into the stack. If the stack is full, it is called an Overflow condition. |
pop() | It discards an element from the stack. The elements are popped in the reverse order in which they are being pushed. In case the stack is empty, it is called an Underflow condition. |
top() | It returns the top element of the stack. |
size() | It returns the size of the stack. |
isEmpty() | It returns true if the stack is empty otherwise false. |
Operations | Complexity |
push() | O(1) |
pop() | O(1) |
size() | O(1) |
isEmpty() | O(1) |
Two major types of stacks are fixed size stacks and dynamic size stacks. They are discussed here:
Fixed size stacks: The size of this type of stack in C is fixed size. Its size can’t increase or reduce dynamically.
Dynamic size stacks: The size of a dynamic size stack in C can increase or reduce dynamically. It is implemented through a linked list.
Here’s the list of a few other types of stacks:
You can implement a stack in C using a linked list or an array.
When implementing it using an array-based method, the push operation is used to increment the index of the top element and assign the new element at the particular index.
In a linked list-based implementation, the push operation is used to create a new node with the new element and initialise the next pointer of the existing top node to the new node. Subsequently, the value of the existing top node is returned.
Here’s an example program to implement stack in C using an array:
#include <stdio.h> |
Output:
The stack has pushed element 10 into the stack. |
In the above program, a Stack struct stores an array of integers. It also stores the value of the top index to track the current top element. The program performs stack operations using initialise, isEmpty, isFull, push, pop, and peek in stack in C.
In the main function, the program creates an instance of the Stack struct. Different stack operations in C are performed, like pushing elements, popping elements, and analysing the top element. During stack underflow (popping out from an empty stack) and stack overflow (pushing into a full stack), the program displays relevant messages to the user.
Here’s an example code to implement stack in C using linked list:
#include <stdio.h> |
Output:
The stack has pushed element 5 into the stack. |
The above program uses the Stack and Node structures. It performs stack operations using functions like initialise, push, pop, isEmpty, and peek. The program creates a new node and links it at the top of the stack after updating the next pointers. If the stack is already empty, the new node is the top node. Otherwise, the new node points to the former top node, and subsequently, it works as the new top node.
The program employs dynamic memory allocation with free and malloc functions to create and free up each node. Moreover, the program supports error handling in cases like stack underflow.
A stack in C provides a convenient way to add and remove elements from a data structure. The ease of accessing elements ensures you can use stack to evaluate an expression, check parenthesis matching, and other applications. One advantage of using stacks in C is that they require minimal memory usage.
In addition to learning the tutorials, pursuing upGrad’s Executive Post Graduate Programme in Software Development by IIIT-B can prove to be a step forward to your career development in the tech industry. The outstanding perks of this course include live lectures, an exclusive job opportunities portal, an AI-powered profile builder, interview preparation, and more. With industry expert guidance, upGrad can help accelerate your career growth in STEM.
Enroll now to initiate your journey!
Q. Is stack a container of objects?
Yes, a stack works as a container of objects which are inserted and removed as per the last-in-first-out (LIFO) rule. The pushdown stacks support only two operations, i.e., push the item into the stack and pop out the item from the stack.
Q. How to check whether a stack is full or empty?
You can use the stack. empty() to check whether the stack is empty or not. This method doesn’t need any parameters. It returns true if the stack is empty and false if the stack isn’t empty.
Q. What is a peek in the stack in C?
Peek() is one of the significant operations in the stack that helps you to obtain the top element of the stack without deleting the particular element.
Q. What is a static stack in C?
A static stack in C contains a finite number of elements. It is also called a bounded stack.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...