Communication Skills
Communication Skills
Faculty Name:
Index -
Stack: ADT, Representation, Operations, Applications of Stack. ADT of Queue, Representation of Queue,
Operations on Queue, Circular Queue, Priority Queue, Applications of Queue. 5
22
2
Lecture 5
Stack is an important data structure which stores its elements in an ordered manner.
Consider the pile of plates where one plate is placed on top of another as shown in Fig.
Stack of plates
Now, when you want to remove a plate, you remove the topmost plate first.
Hence, you can add and remove an element (i.e., a plate) only at/from one position which is the topmost position.
4
Introduction to Stack (Real World Example)
• A stack is a linear data structure which uses the same principle, i.e., the elements in a stack are
added and removed only from one end, which is called the TOP.
• Hence, a stack is called a LIFO (Last-In-First-Out) data structure, as the element that was inserted
last is the first one to be taken out.
• In other words, a stack can be defined as a container in which insertion and deletion can be done
from the one end known as the top of the stack.
6
Representation of Stack
7
Operations on Stack
Push Operation
• The push operation is used to insert an element into the stack.
• The new element is added at the topmost position of the stack.
• However, before inserting the value, we must first check if TOP=MAX–1, because if that is the case, then the stack
is full and no more insertions can be done.
• If an attempt is made to insert a value in a stack that is already full, an OVERFLOW message is printed.
• Consider the stack given in Fig.
8
Operations on Stack
9
Operations on Stack
10
Operation: Pushing into a Stack
Exercise
http://ds1-iiith.vlabs.ac.in/data-structures-1/exp/stacks-
queues/exp.html#Stacks%20:%20Arrays
Problem:
http://ds1-iiith.vlabs.ac.in/data-structures-1/exp/stacks-
queues/exp.html#Stacks%20:%20Demo
Step 1: If TOP=Max-1,then
print “overflow”
[End of If]
top
print “Underflow”
[End of If]
top
Step 2: Set value=Stack[TOP]
top
Step 3: Set TOP=TOP-1
top
Step 4:End
print “Underflow”
[End of If]
top
Step 2: Return value=Stack[TOP]
Step 3:End
Pop Operation
• The pop operation is used to delete the topmost element from the stack.
• However, before deleting the value, we must first check if TOP=NULL because if that is the case, then it means
the stack is empty and no more deletions can be done.
• If an attempt is made to delete a value from a stack that is already empty, an UNDERFLOW message is printed.
• Consider the stack given in Fig.
Stack
17
Operations on Stack
18
Operations on Stack
19
Applications of Stack
Applications of Stack
int main()
{
cout<<"Hello";
cout<<“RAIT";
}
As we know, each program has an opening and closing braces; when the opening braces come, we push the
braces in a stack, and when the closing braces appear, we pop the opening braces from the stack. Therefore, the
net value comes out to be zero. If any symbol is left in the stack, it means that some syntax occurs in a program.
20
Applications of Stack
• Reversing a list
• Parentheses checker
• Conversion of an infix expression into a postfix expression
• Evaluation of a postfix expression
• Conversion of an infix expression into a prefix expression
• Evaluation of a prefix expression
• Recursion
• Tower of Hanoi
21
Applications of Stack
Parentheses checker
• Stacks can be used to check the validity of parentheses in any algebraic expression.
• For example, an algebraic expression is valid if for every open bracket there is a corresponding closing bracket.
• For example, the expression (A+B} is invalid but an expression {A + (B – C)} is valid.
22
Applications of Stack
23
Applications of Stack
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.
24
Applications of Stack
25
Applications of Stack
26
Applications of Stack
27
Applications of Stack
28
Applications of Stack
•String reversal:
• Stack is also used for reversing a string. For example, we want to reverse a “RAIT" string, so we can achieve this
with the help of a stack.
• First, we push all the characters of the string in a stack until we reach the null character.
• After pushing all the characters, we start taking out the character one by one until we reach the bottom of the
stack.
•UNDO/REDO:
• It can also be used for performing UNDO/REDO operations.
•For example, we have an editor in which we write 'a', then 'b', and then 'c'; therefore, the text written in an editor is
abc.
•So, there are three states, a, ab, and abc, which are stored in a stack.
•There would be two stacks in which one stack shows UNDO state, and the other shows REDO state.
If we want to perform UNDO operation, and want to achieve 'ab' state, then we implement pop operation.
29
Applications of Stack
•String reversal: Stack is also used for reversing a string. For example, we want to reverse a “RAIT" string, so we
can achieve this with the help of a stack.
First, we push all the characters of the string in a stack until we reach the null character.
After pushing all the characters, we start taking out the character one by one until we reach the bottom of the stack.
30
Applications of Stack
•Recursion: The recursion means that the function is calling itself again.
•To maintain the previous states, the compiler creates a system stack in which all the previous records of the
function are maintained.
•DFS(Depth First Search): This search is implemented on a Graph, and Graph uses the stack data structure.
•Backtracking:
•Suppose we have to create a path to solve a maze problem.
•If we are moving in a particular path, and we realize that we come on the wrong way.
•In order to come at the beginning of the path to create a new path, we have to use the stack data structure.
•Expression conversion:
•Stack can also be used for expression conversion.
•This is one of the most important applications of stack.
•The list of the expression conversion is given below:
31
Applications of Stack
32
Applications of Stack
33
Applications of Stack
34
C Program for evaluation of postfix expression
35
C Program for evaluation of postfix expression
36
C Program for evaluation of postfix expression
37
Applications of Stack
38
Applications of Stack
Fig. 1
39
Applications of Stack
40
Algorithm for evaluation of a prefix expression
41
Applications of Stack
42
Applications of Stack
43
Applications of Stack
•Memory management:
•The stack manages the memory.
•The memory is assigned in the contiguous memory blocks.
•The memory is known as stack memory as all the variables are assigned in a function call stack memory.
•The memory size assigned to the program is known to the compiler.
•When the function is created, all its variables are assigned in the stack memory.
•When the function completed its execution, all the variables assigned in the stack are released.
44
Operations on Stack
45
Operations on Stack
46
Operations on Stack
POP operation
The steps involved in the POP operation is given below:
•Before deleting the element from the stack, we check whether the stack is empty.
•If we try to delete the element from the empty stack, then the underflow condition occurs.
•If the stack is not empty, we first access the element which is pointed by the top
•Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.
47
Operations on Stack
48
LINKED REPRESENTATION OF STACK
• Stack can be created using an array, This technique of creating a stack is easy, but the drawback is that the
array must be declared to have some fixed size.
• In case the stack is a very small one or its maximum size is known in advance, then the array implementation of
the stack gives an efficient implementation.
• But if the array size cannot be determined in advance, then the other alternative, i.e., linked representation, is
used.
• In a linked stack, every node has two parts—one that stores data and another that stores the address of the next
node.
• The START pointer of the linked list is used as TOP.
• All insertions and deletions are done at the node pointed by TOP.
• If TOP = NULL, then it indicates that the stack is empty
49
LINKED REPRESENTATION OF STACK
The storage requirement of linked representation of the stack with n elements is O(n), and the typical time
requirement for the operations is O(1).
• The O(n) notation means that the runtime complexity of your algorithm has a linear relationship with the size of
input data.
• If the size of input data is increased by 2, then the runtime complexity of your algorithm will be increased by 2 as
well. O(1) means the running time of an algorithm is independent of the input size and is bounded by a constant
'c'.
50
LINKED REPRESENTATION OF STACK
Push Operation
• The push operation is used to insert an element into the stack.
• The new element is added at the topmost position of the stack.
• Consider the linked stack shown in Fig.
Linked stack
51
LINKED REPRESENTATION OF STACK
52
LINKED REPRESENTATION OF STACK
53
LINKED REPRESENTATION OF STACK
54
LINKED REPRESENTATION OF STACK
Pop Operation
• The pop operation is used to delete the topmost element from a stack.
• However, before deleting the value, we must first check if TOP=NULL, because if this is the case, then it means
that the stack is empty and no more deletions can be done.
• If an attempt is made to delete a value from a stack that is already empty, an UNDERFLOW message is printed.
Consider the stack shown in Fig.
Linked stack
55
LINKED REPRESENTATION OF STACKs
In case TOP!=NULL, then we will delete the node pointed by TOP, and make TOP point to the second
element of the linked stack.
Thus, the updated stack becomes as shown in Fig.
56
LINKED REPRESENTATION OF STACKs
Algorithm to delete an
element from a linked stack
57
LINKED REPRESENTATION OF STACKs
MULTIPLE STACKS
• While implementing a stack using an array, the size of the array must be known in advance.
• If the stack is allocated less space, then frequent OVERFLOW conditions will be encountered.
• To deal with this problem, the code will have to be modified to reallocate more space for the array.
• In case we allocate a large amount of space for the stack, it may result in sheer wastage of
memory.
• Thus, there lies a trade-off between the frequency of overflows and the space allocated.
• So, a better solution to deal with this problem is to have multiple stacks or to have more than one stack in the
same array of sufficient size.
• Figure illustrates this concept.
58
LINKED REPRESENTATION OF STACKs
• In Fig., an array STACK[n] is used to represent two stacks, Stack A and Stack B.
• The value of n is such that the combined size of both the stacks will never exceed n.
• While operating on these stacks, it is important to note one thing—Stack A will grow from left to right, whereas
Stack B will grow from right to left at the same time.
• Extending this concept to multiple stacks, a stack can also be used to represent n number of
stacks in the same array.
• That is, if we have a STACK[n], then each stack I will be allocated an equal amount of space bounded by indices
b[i] and e[i].
• This is shown in Fig.
Multiple stacks
59
LINKED REPRESENTATION OF STACKs
60
LINKED REPRESENTATION OF STACKs
61
LINKED REPRESENTATION OF STACKs
62
LINKED REPRESENTATION OF STACKs
63
Queue
INTRODUCTION
• People moving on an escalator. The people who got on the escalator first will be the first one
to step out of it.
• People standing outside the ticketing window of a cinema hall. The first person in the line
will get the ticket first and thus will be the first one to move out of it.
• Luggage kept on conveyor belts. The bag which was placed first will be the first to come out
at the other end.
• Cars lined at a toll bridge. The first car to reach the bridge will be the first to leave.
64
Introduction to Queues (Real World Example)
OUT
IN
STACK – Last In First Out (LIFO) QUEUE– First In First Out (FIFO)
Deletion
@Front
OUT End
IN
Insertion
@ Rear
End QUEUE– First In First Out (FIFO)
A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In
First Out (FIFO) order.
The elements in a queue are added at one end called the REAR and removed from the other end called the FRONT
67
Queue
68
Queue
Queue
69
Queue
70
Queue
71
Queue
72
Queue
73
C Program to implement linear queue
74
C Program to implement linear queue
75
C Program to implement linear queue
76
C Program to implement linear queue
77
C Program to implement linear queue
78
C Program to implement linear queue
79
C Program to implement linear queue
80
C Program to implement linear queue
81
C Program to implement linear queue
82
C Program to implement linear queue
83
C Program to implement linear queue
84
C Program to implement linear queue
85
Queue
When REAR = MAX – 1, where MAX is the size of the queue, we have an overflow condition.
86
LINKED REPRESENTATION OF QUEUE
• In a linked queue, every element has two parts, one that stores the data and another that stores the address of
the next element.
• The START pointer of the linked list is used as FRONT.
• Here, we will also use another pointer called REAR, which will store the address of the last element in the queue.
• All insertions will be done at the rear end and all the deletions will be done at the front end.
• If FRONT = REAR = NULL, then it indicates that the queue is empty.
87
LINKED REPRESENTATION OF QUEUE
• In a linked queue, every element has two parts, one that stores the data and another that stores the address of
the next element.
• The START pointer of the linked list is used as FRONT.
• Here, we will also use another pointer called REAR, which will store the address of the last element in the queue.
• All insertions will be done at the rear end and all the deletions will be done at the front end.
• If FRONT = REAR = NULL, then it indicates that the queue is empty.
Linked queue
88
LINKED REPRESENTATION OF QUEUE
89
LINKED REPRESENTATION OF QUEUE
90
LINKED REPRESENTATION OF QUEUE
91
LINKED REPRESENTATION OF QUEUE
92
LINKED REPRESENTATION OF QUEUE
Delete Operation
• The delete operation is used to delete the element that is first inserted in a queue, i.e., the element whose address is
stored in FRONT.
• However, before deleting the value, we must first check if FRONT=NULL because if this is the case, then the queue is
empty and no more deletions can be done.
• If an attempt is made to delete a value from a queue that is already empty, an underflow message is printed.
• Consider the queue shown in Fig.
Linked queue
93
LINKED REPRESENTATION OF QUEUE
94
LINKED REPRESENTATION OF QUEUE
95
LINKED REPRESENTATION OF QUEUE
96
Queue
97
Circular Queue
98
Circular Queue
99
Circular Queue
• The circular queue solves the major limitation of the normal queue.
• In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we can not
insert the next element even if there is a space in front of queue.
Here, indexes 0 and 1 can only be used after resetting the queue (deletion of all elements).
This reduces the actual size of the queue.
100
Circular Queue
•deQueue() This function is used to delete an element from the circular queue. In a circular queue, the element is
always deleted from the front position.
• Check whether the queue is Empty.
• If it is empty then display Queue is empty.
• If the queue is not empty, then get the last element and remove it from the queue.
101
Circular Queue
102
Types of Queue
TYPES OF QUEUES
1. Circular Queue 2. Deque 3. Priority Queue 4. Multiple Queue
Circular Queues
In linear queues, insertions can be done only at one end called the REAR and deletions are always done from the
other end called the FRONT.
Linear queue
Here, FRONT = and REAR = .
103
Types of Queue
• Now, if we want to insert another value, it will not be possible because the queue is completely full.
• There is no empty space where the value can be inserted.
• Consider a scenario in which two successive deletions are made.
• The queue will then be given as shown in Fig.
104
Types of Queue
• The second option is to use a circular queue. In the circular queue, the first index comes right after the last index.
Circular queue
105
Types of Queue
For insertion, we now have to check for the following three conditions:
Full queue
2. If rear != MAX – 1, then rear will be incremented and the value will be inserted as illustrated in Fig
106
Types of Queue
If front != 0 and rear = MAX – 1, then it means that the queue is not full.
So, set rear = 0 and insert the new element there, as shown in Fig.
107
Types of Queue
108
C Program to implement circular queue
109
C Program to implement circular queue
110
C Program to implement circular queue
111
C Program to implement circular queue
112
Algorithm to insert an element in a circular queue
113
Types of Queue
114
Types of Queue
Empty queue
If the queue is not empty and front = rear, then after deleting the element at the front the queue becomes empty
and so front and rear are set to –1. This is illustrated in Fig.
115
Types of Queue
If the queue is not empty and front = MAX–1, then after deleting the element at the front, front is set to 0. This is
shown in Fig.
116
Types of Queue
117
Types of Queue
118
Types of Queue
Double-ended queues
119
Types of Queue
120
Types of Queue
•Palindrome checking: Deques can be used to check if a word or phrase is a palindrome. By inserting each
character of the word or phrase into a deque, it is possible to check if the word or phrase is a palindrome by
comparing the first and last characters, the second and second-to-last characters, and so on.
•Graph traversal: Deques can be used to implement Breadth-First Search (BFS) on a graph. BFS uses a queue to
keep track of the vertices to be visited next, and a deque can be used as an alternative to a queue in this case.
•Task scheduler: Deques can be used to implement a task scheduler that keeps track of tasks to be executed.
Tasks can be added to the back of the deque, and the scheduler can remove tasks from the front of the deque and
execute them.
121
Types of Queue
122
Types of Queue
Priority Queues
• A priority queue is a data structure in which each element is assigned a priority.
• The priority of the element will be used to determine the order in which the elements will be processed.
• The general rules of processing the elements of a priority queue are
➢ An element with higher priority is processed before an element with a lower priority.
➢ Two elements with the same priority are processed on a first-come-first-served (FCFS) basis.
• A priority queue can be thought of as a modified queue in which when an element has to be removed from the
queue, the one with the highest-priority is retrieved first.
• The priority of the element can be set based on various factors.
• Priority queues are widely used in operating systems to execute the highest priority process first.
• The priority of the process may be set based on the CPU time it requires to get executed completely.
• For example, if there are three processes, where the first process needs 5 ns to complete, the second process
needs 4 ns, and the third process needs 7 ns, then the second process will have the highest priority and will thus
be the first to be executed.
123
Types of Queue
• However, CPU time is not the only factor that determines the priority.
• Another factor is the importance of one process over another.
• In case we have to run two processes at the same time, where one process is concerned with online order
booking and the second with printing of stock details, then obviously the online booking is more important and
must be executed first.
124
Types of Queue
125
Types of Queue
126
Types of Queue
127
Types of Queue
128
Types of Queue
129
Types of Queue
130
Types of Queue
APPLICATIONs OF QUEUES
• Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU.
• Queues are used to transfer data between two processes (IO buffers), e.g., pipes, file IO, sockets.
• Queues are used as buffers on MP3 players and portable CD players, iPod playlist.
• Queues are used in Playlist for jukebox to add songs to the end, play from the front of the list.
• Queues are used in operating system for handling interrupts.
• When programming a real-time system that can be interrupted, for example, by a mouse click, it is necessary to
process the interrupts immediately, before proceeding with the current job.
• If the interrupts have to be handled in the order of arrival, then a FIFO queue is the appropriate data structure.
131
Types of Queue
132
Thank You