Download presentation
Presentation is loading. Please wait.
Published byVirgil Lindsey Modified over 6 years ago
1
Objectives In this lesson, you will learn to: Define stacks
Identify applications of stacks Implement stacks using linked lists Define queues Identify the two types of queues: Deques Priority queue Implement queues using linked lists
2
Stacks Are a form of single linked lists in which addition and deletion are performed in the beginning of the list Are also called push-down lists
3
Stacks (Contd.) Enable the item which is added last to be removed first (Last In First Out i.e. LIFO) DataC DataB DataA TOP PUSH POP
4
Stacks (Contd.) Example: Function calls stored in a stack substring()
main() PUSH the calling function values & addresses when the function is invoked POP out the calling function values once the called function execution is over
5
Stacks (Contd.) Operations that can be carried out on stacks are:
Addition (PUSH) Removal (POP)
6
Applications of Stacks
Parameter tracking and passing values with function calls Validating arithmetic expressions Evaluating prefix and postfix notations
7
Towers of Hanoi PIN 1 PIN 2 PIN 3
8
Towers of Hanoi Rules: Only one disc at a time should be moved, specifically the topmost disc from any pin A larger disc should never be placed on a smaller disc
9
Infix Notation Is denoted as ‘1+2’
Is evaluated using general mathematics rules Requires usage of parenthesis to change operator precedence
10
Postfix Notation Is denoted as ‘12+’
Is used by computers to evaluate an expression Does not require usage of parenthesis
11
Conversion of an Infix Expression to a Postfix Expression
Is done by following the listed steps: 1. Scan the expression from left to right and execute steps 2 to 5 until the stack is empty 2. If the scanned item is an operand, add it to the postfix string, which is the string that will contain the final converted postfix expression 3. If the scanned item is a parenthesis, PUSH it to the stack 4. If the scanned item is an operator, add the current operator to the stack
12
Conversion of an Infix Expression to a Postfix Expression (Contd.)
5. If a right parenthesis is encountered then, 5.1 POP each operator from the top of the stack until the left parenthesis is encountered 5.2 Remove the left parenthesis from the stack 6. Exit
13
Evaluation of a Postfix Expression
Is done by following the listed steps: 1. Add a ')' to the end of the postfix expression 2. Scan the expression from left to right 3. Repeat steps 4 and 5 4. If an operand is encountered, PUSH every operand encountered to the stack 5. If an operator is encountered, 5.1 POP the top two elements 5.2 Evaluate the expression of the two elements 5.3 PUSH the result to the stack
14
Evaluation of a Postfix Expression (Contd.)
6. Set RESULT = top of the stack 7. Exit on encountering the ')' value
15
Implementing Stacks Using linked lists
Is preferred because of dynamic allocation of memory Using arrays Is disadvantageous because it requires the size of the array to be pre-defined
16
Implementing Stacks (Contd.)
Example: class Stack { private: //Member data Node *top; public: Stack(); void PUSH(node *); Node *POP(); ~Stack(); };
17
Problem Statement 12.D.1 Create an application that implements stack by using single linked list and prints the accepted input in reverse order.
18
Problem Statement 12.P.1 Create an application that accepts an expression with round brackets and validates it. The application should ensure that the number of opening brackets should be equivalent to the number of closing brackets. Hint: An expression (2+3)*5 can be used to evaluate the given program.
19
Queues Are sequential lists in which items are inserted into the tail end of the queue and taken from the head Example: Printer queue
20
Queues (Contd.) Are also called First In First Out (FIFO) lists B REAR
X D G FRONT
21
Types of Queues Deques Priority queues
22
Deques Stands for double-ended queues
Are queues in which elements can be added or removed from both the ends of the queue Example: FRONT REAR Ann Bob Tim Joe Ken
23
Deques (Contd.) Are of two types: Input restricted deques
Output restricted deques
24
Priority Queues Are queues which have priority associated with the items added to the queue that determines the order in which the elements are processed and deleted Are used in a time-sharing system Are used by printing applications
25
Priority Queues (Contd.)
In priority queues: An element with a higher priority is processed first If two elements share the same priority, then the element that came in first is processed first
26
Priority Queues (Contd.) Are used in:
Time-sharing systems Operating system schedulers for scheduling activities to be processed by the microprocessor Simulation applications
27
Operations on Queues Primitive operations are: Insertion of nodes
Deletion of nodes Additional operations are: Checking if a queue is empty Emptying a queue Counting the number of elements in a queue
28
Problem Statement 12.D.2 Create an application that will accept the applicant names as and when they appear for interview. The list of applicants should be updated every hour.
29
Problem Statement 12.P.2 Create an application that will print the details of the first five early bird prize winners. Hint: The details to be accepted are the name and id.
30
Summary In this lesson, you learned that:
A stack is a data structure in which operations like adding and removing data are performed from only one end called as TOP Stacks can be used to: Validate arithmetic expressions Evaluate infix and postfix notation Store function arguments and address whenever a function calls another function
31
Summary (Contd.) The operations that can be carried out on stacks are:
Push Pop In computers where rules for precedence do not exist, the evaluation of an arithmetic expression written in infix is done in two steps: Conversion of an infix expression to a postfix expression Evaluation of the postfix expression Stacks can be implemented using arrays or linked lists
32
Summary (Contd.) Defining stacks by using linked list is preferred because of dynamic memory allocation A queue is a sequential list where items are inserted into the tail end of the queue and taken from the head The "tail" is referred to as the REAR element and the "head" is the FRONT element of a queue, where each element is deleted from the end called FRONT and added at the other end called REAR Queues are called First In First Out (FIFO) lists Queues can be categorized into two types: Deques (pronounced either deck or deQueue) Priority queue
33
Summary (Contd.) A deques is also called a double-ended queue. In this, elements are added or removed from both ends of the queue The two types of deques are: Output-restricted Input-restricted Often, the items added to a queue have a priority associated with them that determines the order in which the processing and deletion of the each element in the queue happens. Such a kind of queue is called a priority queue
34
Summary (Contd.) The different applications of queues are:
Time-sharing systems Operating system schedulers for scheduling activities to be processed by the microprocessor Simulation applications A queue can be implemented using arrays or linked list A queue when implemented using linked lists is called a linked queue
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.