Javatpoint Logo
Javatpoint Logo

Stack in C

A stack is a basic data structure in computer science that adheres to the Last-In-First-Out (LIFO) rule. It is comparable to a stack of books where the last book added is the first one to be taken out. Stacks are frequently used in programming and have a wide range of uses. In this blog article, we will examine the idea of a stack in C, along with its implementation, syntax, code examples, and accompanying output.

A stack is a collection of components that can be accessed using the push and pop operations. While the pop operation takes the top element out of the stack, the push operation adds an element to the top of the stack. The topmost element can also be examined without deleting it due to the peek procedure.

In C, an array or linked list can be used to implement a stack. Due to its simplicity and increased usage, we will concentrate on the array-based solution in this article.

Definition of a stack structure in C:

Here, we have defined a structure called Stack, which consists of an array of integers named arr and an integer top used to identify the element that is now at the top. The MAX_SIZE constant denotes the stack's maximum capacity.

We must initialize a stack before using it by setting the top variable to -1, which denotes an empty stack. The initialization function is seen here:

When performing a push, an element is added to the top of the stack. The top variable is increased, and the new element is inserted at the corresponding index. Let's put the push operation into action:

In the code above, we determine whether the stack is already complete (top == MAX_SIZE - 1). If so, we return without advancing the element and print a message indicating a stack overflow.

The topmost element in the stack is removed during the pop action. We decrease the top variable, to replicate the removal. Let's put the pop operation into action:

The code above checks whether the stack is empty previously (top == -1). In this case, we return an incorrect value (here, -1) to signal the underflow condition and print an error message indicating a stack underflow.

We can inspect the topmost element using the peek method without removing it. Here is how it will be done:

The stack is examined in the code above to see if it is empty (top == -1). Upon finding an empty stack, we print an error message and return an invalid value (-1).

Example:

Let's now write a straightforward program to show how stack operations work. We'll add a few pieces to the stack, remove one, and then take a peek inside.

Output:

Top element: 30
Popped element: 30
Top element after popping: 20

Here are applications of stack data structure

1. Parentheses Matching:

Output:

Enter an expression: (a + b) * (c - d)
Parentheses are balanced.

Explanation:

This program employs a stack to determine whether the brackets in an expression are balanced. After each iteration, it cycles through each expression character and adds an opening parenthesis to the stack. It pops an element from the stack when it comes across a closing parenthesis. The expression is balanced, and all brackets are matched if the stack is empty at the end of the iteration.

2. Infix to Postfix Conversion:

Output:

Enter an infix expression: (a + b) * c - d
Postfix expression: ab+c*d-

Explanation:

Using the stack, this program transforms an infix expression into postfix notation. Based on the order of the operators and brackets, it iterates through each letter in the infix expression and performs the relevant operations. The postfix expression is produced by removing operators from the stack and adding them to the output string.

3. Undo/Redo Functionality:

Output:

Undo operation: Replace text
Redo operation: Replace text

Explanation:

This program uses two stacks to demonstrate the undo/redo capability. An operation is simulated using the performOperation function, and the result is pushed onto the undo stack. When the undo function is invoked, an operation is removed from the undo stack and placed on the redo stack. The redo function executes the reverse process by popping from the redo stack and pushing it back into the undo stack.

Conclusion:

In conclusion, effective programming requires a grasp of the stack data structure and how it is implemented in C. The Last-In-First-Out (LIFO) principle can be used to manipulate elements in a stack by employing the push, pop, and peek operations. This blog post's array-based implementation offers a straightforward and widely used method. We can guarantee the correct operation of our stack by managing potential faults like stack overflow and underflow. Gaining command of the stack data structure paves the way for effectively resolving a variety of programming issues and improving code efficiency.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA