0% found this document useful (0 votes)
9 views

Unit - 4

Uploaded by

parthskrpp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit - 4

Uploaded by

parthskrpp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Unit - 3

Stack
4.1 Introduction to Stack: Definition, Stack as an ADT, Operations on Stack-(Push, Pop),
Stack Operations Conditions - Stack Full / Stack Overflow, Stack Empty /Stack Underflow.
4.2 Stack Implementation: using Array and Representation Using Linked List.
Applications of Stack: Reversing a List, Polish Notations, Conversion of Infix to Postfix
Expression, Evaluation of Postfix Expression.
4.4 Recursion: Definition and Applications.

Previous Year Questions


1. Show the memory representation of stack using array with the help of a diagram.
2. Convert the following infix expression to its prefix form using stack: A + B – C D / E
+F
3. Evaluate the following postfix expression: 5, 6, 2, +, , 12, 4, /, – Show each step of
evolution diagrammatically using stack.
4. Evaluate the following prefix expression: – + 4 3 2 5 show diagrammatically each step
of evaluation using stack.
5. Show the effect of PUSH and POP operation on to the stack of size 10. The
6. stack contains 40, 30, 52, 86, 39, 45, 50 with 50 being at top of the stack.Show
diagrammatically the effect of : (i) PUSH 59 (ii) PUSH 85 (iii) POP (iv) POP (v)
PUSH 59 (vi)POP
Sketch the final structure of stack after performing the above said operations.
7. Evaluate the following postfix expression : 57 + 62 –
8. Enlist operations on stack.
9. Evaluate the following postfix expression :10, 2, *, 15, 3, /, +, 12, 3, +, + Show
diagrammatically each step of evaluation using stack.
10. Convert the following Infix expression to its prefix form using stack. Show the details
of stack at each step of conversion.
Expression: P*Q↑R-S/T+(U/V)
11. Write a program to implement a stack with push, pop and display operations.
12. Write any two operations performed on the stack.
13. Explain stack overflow and underflow conditions with example.
14. Show the effect of PUSH and POP operation on to the stack of size 10. The
stack contains 10, 20, 30, 40, 50 and 60, with 60 being at top of the stack.
Show diagrammatically the effect of – (i) PUSH 55 (ii) PUSH 70 (iii) POP (iv) POP
Sketch the final structure of stack after performing the above said operations.
15. Convert the infix expression to its postfix expression using stack
((A + B) *D) ^ (E – F). Show diagrammatically each step of conversion.
16. Evaluate the following postfix expression : 4 6 24 + * 6 3 / –
Show diagrammatically each step of evaluation using stack.
17. Differentiate between stack and queue. (any two points)
18. Convert infix expression into prefix expression : (A + B)*(C / G) + F
19. Convert following expression into postfix form. Give stepwise procedure.
A + B ↑ C * (D / E) – F / G
20. Write algorithm for performing push and pop operations on stack.
21. Define the terms ‘overflow’ and ‘underflow’ with respect to stack.
22. Evaluate the following arithmetic expression P written in postfix notation :
P : 4, 2, ^, 3, *, 3, -, 8, 4, /, +
23. Convert the following infix expression to postfix expression using stack and
show the details of stack in each step. ((A+B)*D)^(E-F)
24. Show the effect of PUSH and POP operations on the stack of size 10.
PUSH(10), PUSH(20), POP, PUSH(30).
25. List any four applications of stack.
26. Convert following expression into postfix form with illustration of all steps
using stack : (A + B – C + D*E/F^G)
27. Explain stack overflow and stack underflow with example.
28. Write a menu driven ‘C’ program to implement stack using array with the
following menu : (i) push (ii) pop (iii) display (iv) exit
29. Convert the following infix expression to its postfix form using stack :
A + B – C * D/E + F
30. Convert the given infix expression to postfix expression using stack and the
details of stack at each step of conversion. Expression : A * B ↑ C – D / E + [F / G]
31. Describe working of bubble sort with example.
32. Show the effect of PUSH and POP operation on the stack of size 10. The
stack contains 10, 20, 25, 15, 30 & 40 with 40 being at top of stack. Show
diagrammatically the effect of (i) PUSH (45) (ii) PUSH (50) (iii) POP (iv) PUSH (55)
33. Find out prefix equivalent of the expression : (i) [(A + B) + C] * D, (ii) A[(B * C) + D]
34. Explain the concept of recursion using stack.
35. Define the term recursion. Write a program in C to display the factorial of a entered
number using recursion.
36. Write a 'C' program to calculate the factorial of number using recursion.

What is a Stack?
A Stack is a linear data structure that holds a linear, ordered sequence of elements. It is an
abstract data type. A Stack works on the LIFO process (Last In First Out), i.e., the element that
was inserted last will be removed first. To implement the Stack, it is required to maintain a
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.

Types of Stack Data Structure:


● Fixed Size Stack: As the name suggests, a fixed size stack has a fixed size and
cannot grow or shrink dynamically. If the stack is full and an attempt is made to add
an element to it, an overflow error occurs. If the stack is empty and an attempt is
made to remove an element from it, an underflow error occurs.
● Dynamic Size Stack: A dynamic size stack can grow or shrink dynamically. When
the stack is full, it automatically increases its size to accommodate the new element,
and when the stack is empty, it decreases its size. This type of stack is implemented
using a linked list, as it allows for easy resizing of the stack.
Stack ADT in Data Structures:

The abstract datatype is special kind of datatype, whose behavior is defined by a set of values
and set of operations. The keyword “Abstract” is used as we can use these datatypes, we can
perform different operations. But how those operations are working that is totally hidden from
the user. The ADT is made of with primitive datatypes, but operation logics are hidden.

Here we will see the stack ADT. These are few operations or functions of the Stack ADT.

​ isFull(), This is used to check whether stack is full or not


​ isEmpry(), This is used to check whether stack is empty or not
​ push(x), This is used to push x into the stack
​ pop(), This is used to delete one element from top of the stack
​ peek(), This is used to get the top most element of the stack
​ size(), this function is used to get number of elements present into the stack

Example:
Working of Stack:
Stack works on the LIFO pattern. As we can observe in the below figure there are five memory
blocks in the stack; therefore, the size of the stack is 5.

Suppose we want to store the elements in a stack and let's assume that stack is empty. We have
taken the stack of size 5 as shown below in which we are pushing the elements one by one until
the stack becomes full.
Since our stack is full as the size of the stack is 5. In the above cases, we can observe that it goes
from the top to the bottom when we were entering the new element in the stack. The stack gets
filled up from the bottom to the top.

When we perform the delete operation on the stack, there is only one way for entry and exit as
the other end is closed. It follows the LIFO pattern, which means that the value entered first will
be removed last. In the above case, the value 5 is entered first, so it will be removed only after
the deletion of all the other elements.

Standard Stack Operations:


The following are some common operations implemented on the stack:

○ push(): When we insert an element in a stack then the operation is known as a push. If
the stack is full then the overflow condition occurs.
○ pop(): When we delete an element from the stack, the operation is known as a pop. If the
stack is empty means that no element exists in the stack, this state is known as an
underflow state.
○ isEmpty(): It determines whether the stack is empty or not.
○ isFull(): It determines whether the stack is full or not.'
○ peek(): It returns the element at the given position.
○ count(): It returns the total number of elements available in a stack.
○ change(): It changes the element at the given position.
○ display(): It prints all the elements available in the stack.

i) Push() Operation in Stack Data Structure: Adds an item to the stack. If the stack is full,
then it is said to be an Overflow condition.

Algorithm for Push Operation:

● Before pushing the element to the stack, we check if the stack is full .
● If the stack is full (top == capacity-1) , then Stack Overflows and we cannot insert the
element to the stack.
● Otherwise, we increment the value of top by 1 (top = top + 1) and the new value is
inserted at top position .
● The elements can be pushed into the stack till we reach the capacity of the stack.

ii) Pop() Operation in Stack Data Structure:

Removes an item from the stack. The items are popped in the reversed order in which they are
pushed. If the stack is empty, then it is said to be an Underflow condition.

Algorithm for Pop Operation:

● Before popping the element from the stack, we check if the stack is empty .
● If the stack is empty (top == -1), then Stack Underflows and we cannot remove any
element from the stack.
● Otherwise, we store the value at top, decrement the value of top by 1 (top = top – 1)
and return the stored top value.
iii) Top() or Peek() Operation in Stack Data Structure: Returns the top element of the stack.

Algorithm for Top Operation:


● Before returning the top element from the stack, we check if the stack is empty.
● If the stack is empty (top == -1), we simply print “Stack is empty”.
● Otherwise, we return the element stored at index = top .
iv) isEmpty() Operation in Stack Data Structure: Returns true if the stack is empty, else false.

Algorithm for isEmpty Operation:


● Check for the value of top in stack.
● If (top == -1) , then the stack is empty so return true .
● Otherwise, the stack is not empty so return false .

v) isFull() Operation in Stack Data Structure: Returns true if the stack is full, else false.

Algorithm for isFull Operation:


● Check for the value of top in stack.
● If (top == capacity-1), then the stack is full so return true.
● Otherwise, the stack is not full so return false.
Application of the Stack:

1. A Stack can be used for evaluating expressions consisting of operands and operators.
2. Stacks can be used for Backtracking, i.e., to check parenthesis matching in an expression.
3. It can also be used to convert one form of expression to another form.
4. It can be used for systematic Memory Management.

Advantages of Stack:

1. A Stack helps to manage the data in the ‘Last in First out’ method.
2. When the variable is not used outside the function in any program, the Stack can be used.
3. It allows you to control and handle memory allocation and deallocation.
4. It helps to automatically clean up the objects.

Disadvantages of Stack:

1. It is difficult in Stack to create many objects as it increases the risk of the Stack overflow.
2. It has very limited memory.
3. In Stack, random access is not possible.

Array implementation of Stack:

In array implementation, the stack is formed by using the array. All the operations regarding the
stack are performed using arrays. Lets see how each operation can be implemented on the stack
using array data structure.
Adding an element onto the stack (push operation):
Adding an element into the top of the stack is referred to as push operation. Push operation
involves following two steps.

1. Increment the variable Top so that it can now refere to the next memory location.
2. Add element at the position of incremented top. This is referred to as adding new element
at the top of the stack.

Stack is overflown when we try to insert an element into a completely filled stack therefore, our
main function must always avoid stack overflow condition.

Algorithm:

​ begin
​ if top = n then stack full
​ top = top + 1
​ stack (top) : = item;
​ end
Time Complexity : O(1)

implementation of push algorithm in C language


​ void push (int val,int n) //n is size of the stack
​ {
​ if (top == n )
​ printf("\n Overflow");
​ else
​ {
​ top = top +1;
​ stack[top] = val;
​ }
​ }
Deletion of an element from a stack (Pop operation):
Deletion of an element from the top of the stack is called pop operation. The value of the
variable top will be incremented by 1 whenever an item is deleted from the stack. The top most
element of the stack is stored in an another variable and then the top is decremented by 1. the
operation returns the deleted value that was stored in another variable as the result.

The underflow condition occurs when we try to delete an element from an already empty stack.

Algorithm :
​ begin
​ if top = 0 then stack empty;
​ item := stack(top);
​ top = top - 1;
​ end;
Time Complexity : o(1)

Implementation of POP algorithm using C language


​ int pop ()
​ {
​ if(top == -1)
​ {
​ printf("Underflow");
​ return 0;
​ }
​ else
​ {
​ return stack[top - - ];
​ }
​ }
Visiting each element of the stack (Peek operation):
Peek operation involves returning the element which is present at the top of the stack without
deleting it. Underflow condition can occur if we try to return the top element in an already empty
stack.

Algorithm :

PEEK (STACK, TOP)

​ Begin
​ if top = -1 then stack empty
​ item = stack[top]
​ return item
​ End
Time complexity: o(n)

Implementation of Peek algorithm in C language


​ int peek()
​ {
​ if (top == -1)
​ {
​ printf("Underflow");
​ return 0;
​ }
​ else
​ {
​ return stack [top];
​ }
​ }
C program:

​ #include <stdio.h>
​ int stack[100],i,j,choice=0,n,top=-1;
​ void push();
​ void pop();
​ void show();
​ void main ()
​ {

​ printf("Enter the number of elements in the stack ");
​ scanf("%d",&n);
​ printf("*********Stack operations using array*********");

​ printf("\n----------------------------------------------\n");
​ while(choice != 4)
​ {
​ printf("Chose one from the below options...\n");
​ printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
​ printf("\n Enter your choice \n");
​ scanf("%d",&choice);
​ switch(choice)
​ {
​ case 1:
​ {
​ push();
​ break;
​ }
​ case 2:
​ {
​ pop();
​ break;
​ }
​ case 3:
​ {
​ show();
​ break;
​ }
​ case 4:
​ {
​ printf("Exiting....");
​ break;
​ }
​ default:
​ {
​ printf("Please Enter valid choice ");
​ }
​ };
​ }
​ }

​ void push ()
​ {
​ int val;
​ if (top == n )
​ printf("\n Overflow");
​ else
​ {
​ printf("Enter the value?");
​ scanf("%d",&val);
​ top = top +1;
​ stack[top] = val;
​ }
​ }





​ void pop ()
​ {
​ if(top == -1)
​ printf("Underflow");
else
​ top = top -1;
​ }
​ void show()
​ {
​ for (i=top;i>=0;i--)
​ {
​ printf("%d\n",stack[i]);
​ }
​ if(top == -1)
​ {
​ printf("Stack is empty");
​ }
​ }

Linked list implementation of the stack:

Instead of using array, we can also use linked list to implement stack. Linked list allocates the
memory dynamically. However, time complexity in both the scenario is same for all the
operations i.e. push, pop and peek.

In linked list implementation of stack, the nodes are maintained non-contiguously in the memory.
Each node contains a pointer to its immediate successor node in the stack. Stack is said to be
overflown if the space left in the memory heap is not enough to create a node.
The top most node in the stack always contains null in its address field. Lets discuss the way in
which, each operation is performed in linked list implementation of stack.

Adding a node to the stack (Push operation):


Adding a node to the stack is referred to as push operation. Pushing an element to a stack in a
linked list implementation is different from that of an array implementation. In order to push an
element onto the stack, the following steps are involved.

1. Create a node first and allocate memory to it.


2. If the list is empty then the item is to be pushed as the start node of the list. This includes
assigning value to the data part of the node and assign null to the address part of the node.
3. If there are some nodes in the list already, then we have to add the new element in the
beginning of the list (to not violate the property of the stack). For this purpose, assign the
address of the starting element to the address field of the new node and make the new
node, the starting node of the list.

Time Complexity : O(1)


C implementation :

​ void push ()
​ {
​ int val;
​ struct node *ptr =(struct node*)malloc(sizeof(struct node));
​ if(ptr == NULL)
​ {
​ printf("not able to push the element");
​ }
​ else
​ {
​ printf("Enter the value");
​ scanf("%d",&val);
​ if(head==NULL)
​ {
​ ptr->val = val;
​ ptr -> next = NULL;
​ head=ptr;
​ }
​ else
​ {
​ ptr->val = val;
​ ptr->next = head;
​ head=ptr;

​ }
​ printf("Item pushed");

​ }
​ }

Deleting a node from the stack (POP operation):


Deleting a node from the top of stack is referred to as pop operation. Deleting a node from the
linked list implementation of stack is different from that in the array implementation. In order to
pop an element from the stack, we need to follow the following steps :

​ Check for the underflow condition: The underflow condition occurs when we try
to pop from an already empty stack. The stack will be empty if the head pointer of
the list points to null.
​ Adjust the head pointer accordingly: In stack, the elements are popped only from
one end, therefore, the value stored in the head pointer must be deleted and the
node must be freed. The next node of the head node now becomes the head node.

Time Complexity : O(n)

C implementation:

​ void pop()
​ {
​ int item;
​ struct node *ptr;
​ if (head == NULL)
​ {
​ printf("Underflow");
​ }
​ else
​ {
​ item = head->val;
​ ptr = head;
​ head = head->next;
​ free(ptr);
​ printf("Item popped");

​ }
​ }

Display the nodes (Traversing):


Displaying all the nodes of a stack needs traversing all the nodes of the linked list organized in
the form of stack. For this purpose, we need to follow the following steps.

​ Copy the head pointer into a temporary pointer.


​ Move the temporary pointer through all the nodes of the list and print the value
field attached to every node.

Time Complexity : O(n)


C Implementation:

​ void display()
​ {
​ int i;
​ struct node *ptr;
​ ptr=head;
​ if(ptr == NULL)
​ {
​ printf("Stack is empty\n");
​ }
​ else
​ {
​ printf("Printing Stack elements \n");
​ while(ptr!=NULL)
​ {
​ printf("%d\n",ptr->val);
​ ptr = ptr->next;
​ }
​ }
​ }

Menu Driven program in C implementing all the stack operations using linked list :

​ #include <stdio.h>
​ #include <stdlib.h>
​ void push();
​ void pop();
​ void display();
​ struct node
​ {
​ int val;
​ struct node *next;
​ };
​ struct node *head;

​ void main ()
​ {
​ int choice=0;
​ printf("\n*********Stack operations using linked list*********\n");
​ printf("\n----------------------------------------------\n");
​ while(choice != 4)
​ {
​ printf("\n\nChose one from the below options...\n");
​ printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
​ printf("\n Enter your choice \n");
​ scanf("%d",&choice);
​ switch(choice)
​ {
​ case 1:
​ {
​ push();
​ break;
​ }
​ case 2:
​ {
​ pop();
​ break;
​ }
​ case 3:
​ {
​ display();
​ break;
​ }
​ case 4:
​ {
​ printf("Exiting....");
​ break;
​ }
​ default:
​ {
​ printf("Please Enter valid choice ");
​ }
​ };
​ }
​ }
​ void push ()
​ {
​ int val;
​ struct node *ptr = (struct node*)malloc(sizeof(struct node));
​ if(ptr == NULL)
​ {
​ printf("not able to push the element");
​ }
​ else
​ {
​ printf("Enter the value");
​ scanf("%d",&val);
​ if(head==NULL)
​ {
​ ptr->val = val;
​ ptr -> next = NULL;
​ head=ptr;
​ }
​ else
​ {
​ ptr->val = val;
​ ptr->next = head;
​ head=ptr;

​ }
​ printf("Item pushed");

​ }
​ }

​ void pop()
​ {
​ int item;
​ struct node *ptr;
​ if (head == NULL)
​ {
​ printf("Underflow");
​ }
​ else
​ {
​ item = head->val;
​ ptr = head;
​ head = head->next;
​ free(ptr);
​ printf("Item popped");

​ }
​ }
​ void display()
​ {
​ int i;
​ struct node *ptr;
​ ptr=head;
​ if(ptr == NULL)
​ {
​ printf("Stack is empty\n");
​ }
​ else
​ {
​ printf("Printing Stack elements \n");
​ while(ptr!=NULL)
​ {
​ printf("%d\n",ptr->val);
​ ptr = ptr->next;
​ }
​ }
​ }

Recursion
What is Recursion?

Recursion is defined as a process that calls itself directly or indirectly and the corresponding
function is called a recursive function.

Properties of Recursion:

Recursion has some important properties. Some of which are mentioned below:

● The primary property of recursion is the ability to solve a problem by breaking it


down into smaller sub-problems, each of which can be solved in the same way.
● A recursive function must have a base case or stopping criteria to avoid infinite
recursion.
● Recursion involves calling the same function within itself, which leads to a call stack.
● Recursive functions may be less efficient than iterative solutions in terms of memory
and performance.

Types of Recursion:

1. Direct recursion: When a function is called within itself directly it is called direct
recursion. This can be further categorised into four types:
● Tail recursion,
● Head recursion,
● Tree recursion and
● Nested recursion.
2. Indirect recursion: Indirect recursion occurs when a function calls another function
that eventually calls the original function and it forms a cycle.

Applications of Recursion:

Recursion is used in many fields of computer science and mathematics, which includes:

● Searching and sorting algorithms: Recursive algorithms are used to search and sort
data structures like trees and graphs.
● Mathematical calculations: Recursive algorithms are used to solve problems such as
factorial, Fibonacci sequence, etc.
● Compiler design: Recursion is used in the design of compilers to parse and analyze
programming languages.
● Graphics: many computer graphics algorithms, such as fractals and the Mandelbrot
set, use recursion to generate complex patterns.
● Artificial intelligence: recursive neural networks are used in natural language
processing, computer vision, and other AI applications.

Advantages of Recursion:

● Recursion can simplify complex problems by breaking them down into smaller, more
manageable pieces.
● Recursive code can be more readable and easier to understand than iterative code.
● Recursion is essential for some algorithms and data structures.
● Also with recursion, we can reduce the length of code and become more readable and
understandable to the user/ programmer.
Disadvantages of Recursion:

● Recursion can be less efficient than iterative solutions in terms of memory and
performance.
● Recursive functions can be more challenging to debug and understand than iterative
solutions.
● Recursion can lead to stack overflow errors if the recursion depth is too high.

You might also like