![]() |
VOOZH | about |
Stacks, a fundamental data structure in computer science, are crucial for understanding algorithmic paradigms and solving complex computational problems. As candidates gear up for the GATE Exam 2024, a solid grasp of stack concepts is indispensable. These notes are designed to provide a concise yet comprehensive overview of stacks, covering key topics that are likely to be assessed in the GATE examination.
Table of Content
A stack is a linear data structure in which the insertion of a new element and removal of an existing element takes place at the same end represented as the top of the stack.
To implement the stack, it is required to maintain the pointer to the top of the stack, which is the last element to be inserted because we can access the elements only on the top of the stack.
This strategy states that the element that is inserted last will come out first. You can take a pile of plates kept on top of each other as a real-life example. The plate which we put last is on the top and since we remove the plate that is at the top, we can say that the plate that was put last comes out first.
In order to make manipulations in a stack, certain operations are provided to us.
| Operations | Complexity |
|---|---|
| push() | O(1) |
| pop() | O(1) |
| isEmpty() | O(1) |
| size() | O(1) |
To implement a stack using the singly linked list concept, all the singly linked list operations should be performed based on Stack operations LIFO(last in first out) and with the help of that knowledge, we are going to implement a stack using a singly linked list.
So we need to follow a simple rule in the implementation of a stack which is last in first out and all the operations can be performed with the help of a top variable. Let us learn how to perform Pop, Push, Peek, and Display operations in the following article:
In the stack Implementation, a stack contains a top pointer. which is the βheadβ of the stack where pushing and popping items happens at the head of the list. The first node has a null in the link field and second node-link has the first node address in the link field and so on and the last node address is in the βtopβ pointer.
The main advantage of using a linked list over arrays is that it is possible to implement a stack that can shrink or grow as much as needed. Using an array will put a restriction on the maximum capacity of the array which can lead to stack overflow. Here each new node will be dynamically allocated. so overflow is not possible.
- Initialise a node
- Update the value of that node by data i.e. node->data = data
- Now link this node to the top of the linked list
- And update top pointer to the current node
- First Check whether there is any node present in the linked list or not, if not then return
- Otherwise make pointer let say temp to the top node and move forward the top node by 1 step
- Now free this temp node
- Check if there is any node present or not, if not then return.
- Otherwise return the value of top node of the linked list
- Take a temp node and initialize it with top pointer
- Now start traversing temp till it encounters NULL
- Simultaneously print the value of the temp node
To convert infix expression to postfix expression, use the stack data structure. Scan the infix expression from left to right. Whenever we get an operand, add it to the postfix expression and if we get an operator or parenthesis add it to the stack by maintaining their precedence.
Below are the steps to implement the above idea:
To evaluate a postfix expression we can use a stack.
Iterate the expression from left to right and keep on storing the operands into a stack. Once an operator is received, pop the two topmost elements and evaluate them and push the result in the stack again.
Tower of Hanoi is a mathematical puzzle where we have three rods (A, B, and C) and N disks. Initially, all the disks are stacked in decreasing value of diameter i.e., the smallest disk is placed on the top and they are on rod A. The objective of the puzzle is to move the entire stack to another rod (here considered C), obeying the following simple rules:
Examples:
Input:3
Output:Disk 1 moved from A to C
Disk 2 moved from A to B
Disk 1 moved from C to B
Disk 3 moved from A to C
Disk 1 moved from B to A
Disk 2 moved from B to C
Disk 1 moved from A to C
The idea is to use the helper node to reach the destination using recursion. Below is the pattern for this problem:
- Shift βN-1β disks from βAβ to βBβ, using C.
- Shift last disk from βAβ to βCβ.
- Shift βN-1β disks from βBβ to βCβ, using A.
Follow the steps below to solve the problem:
Time complexity: O(2N), There are two possibilities for every disk. Therefore, 2 * 2 * 2 * . . . * 2(N times) is 2N
Auxiliary Space: O(N), Function call stack space
The Fibonacci numbers are the numbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, β¦β¦..
Input : n = 9
Output : 34
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation:
π F_{n} = F_{n-1} + F_{n-2}The end of a stack, traditionally known as the position where PUSH and POP operations performed, is known as:
Option 4 : TOP
The top of the stack refers to the end of the stack where operations are performed. This is where elements are added (pushed) and removed (popped). When an element is added to an empty stack, it becomes the top element. When additional elements are pushed onto the stack, they become the new top elements.
What is the equivalent infix expression of the following postfix expression?
M, N, O, +, *, P, /, Q, R, S, T, /, +, *, β
Option 2 : (((M * (N + O)) / P) β (Q * (R + (S / T))))
Let's apply this algorithm to the given postfixexpression - M, N, O, +, *, P, /, Q, R, S, T, /, +, *, β
Step 1 - Push M, N, O onto the stack Stack - O, N, M
Step 2 - Pop O, N, M and concatenate them with + and * Stack - (M*(N+O))
Step 3 - Push P onto the stack Stack - P, (M*(N+O))
Step 4 - Pop P and concatenate it with / Stack - ((M*(N+O))/P)
Step 5 - Push Q, R, S, T onto the stack Stack - T, S, R, Q, ((M*(N+O))/P)
Step 6 - Pop T, S, R, Q and concatenate them with / and + and * Stack - ((Q*(R+(S/T))), ((M*(N+O))/P)
Step 7 - Pop the final expression from the stack after "-" Infix expression - (((M*(N+O))/P) - ((Q*(R+(S/T))))
What is the postfix representation of the following infix expression?
(A + B) * C β D * E / F
Option 4 : A B + C * D E * F / -
() has highest precedence
* and / has same precedence while + and β has same precedence
(* and /) and higher precedence than (+, -)
Associativity is left to right:
(A + B) * C β D * E / F
A B + * C β D * E / F
A B + C * β D * E / F
A B + C * β D E * / F
A B + C * β D E * F /
A B + C * D E * F / β
The result evaluating the postfix expression 10 5 + 60 6 / * 8 β is
Option 3 : 142
The five items P,Q,R,S and T are pushed in a stack, one after the other starting from P. The stack is popped four times and each element is inserted in a queue. Then two elements are deleted from the queue and pushed back on the stack. now one item is popped from the stack. The popped item is:
Option 4 : S
Consider the following postfix expression with single digit operands:
6 2 3 * / 4 2 * + 6 8 * -
The top two elements of the stack after second * is evaluated, are:
Option 2 : 8, 1
What is the outcome of the prefix expression +, -, *, 3, 2, /, 8, 4, 1?
Option 2 : 5
A stack is implemented with an array of βA [0..N β 1]β and a variable βposβ. The push and pop operations are defined by the following code.
push(x)
A[pos] β x
pos β pos β 1
end push
pop( )
pos β pos + 1
return A[pos]
end pop
Which of the following will initialize an empty stack with capacity N for the above implementation?
Option 4 : pos β N - 1
A stack can be implemented using queue, but then we need to use atleast:
Option 2 : 2 queues
Which of the following applications may use a stack?
(a) Parenthesis balancing program
(b) Process scheduling operating system
(c) Conversion of infix arithmetic expression to postfix form
Option 3 : (a) and (c)