Upcoming SlideShare
×

# Ds 3

914 views

Published on

Published in: Technology, Education
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
914
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
0
0
Likes
3
Embeds 0
No embeds

No notes for slide

### Ds 3

1. 1. Implementing Stacks and Queues UsingLinked ListObjectivesIn 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 1 of 34
2. 2. Implementing Stacks and Queues UsingLinked ListStacks 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 2 of 34
3. 3. Implementing Stacks and Queues UsingLinked ListStacks (Contd..) Enable the item which is added last to be removed first (Last In First Out i.e. LIFO) TOP PUSH POP DataC DataB DataA Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 3 of 34
4. 4. Implementing Stacks and Queues UsingLinked ListStacks (Contd..) Example: Function calls stored in a stack PUSH the calling POP out the calling function values function values once addresses when the s u b s tring() the called function function is invoked execution is over m ain() Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 4 of 34
5. 5. Implementing Stacks and Queues UsingLinked ListStack Operations Operations that can be carried out on stacks are: Addition (PUSH) Removal (POP) Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 5 of 34
6. 6. Implementing Stacks and Queues UsingLinked ListStack Operations (Contd..) Some Common applications of stacks are: Parameter tracking and passing values with function calls Validating arithmetic expressions Evaluating prefix and postfix notations Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 6 of 34
7. 7. Implementing Stacks and Queues UsingLinked ListApplication of Stacks Towers of Hanoi PIN 1 PIN 2 PIN 3 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 7 of 34
8. 8. Implementing Stacks and Queues UsingLinked ListApplication of Stacks The rules applicable to the Towers of Hanoi are as follows: 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 8 of 34
9. 9. Implementing Stacks and Queues UsingLinked ListInfix Notation The arithmetic expressions that are written with the operator placed between the operands are said to be in Infix Notation Is evaluated using general mathematics rules for operator precedence like: Parenthesis (highest precedence) Exponentiation (second highest precedence) Multiplication and division (third highest precedence) Addition and subtraction (lowest precedence) Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 9 of 34
10. 10. Implementing Stacks and Queues UsingLinked ListPostfix Notation Precedence values or parenthesis are not required to determine the order of operation. 1+2 would be denoted as 12+ 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 10 of 34
11. 11. Implementing Stacks and Queues UsingLinked ListConversion of an Infix Expression to a PostfixExpression 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 11 of 34
12. 12. Implementing Stacks and Queues UsingLinked ListConversion of an Infix Expression to a PostfixExpression (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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 12 of 34
13. 13. Implementing Stacks and Queues UsingLinked ListEvaluation 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 13 of 34
14. 14. Implementing Stacks and Queues UsingLinked ListEvaluation of a Postfix Expression (Contd..) 6. Set RESULT = top of the stack 7. Exit on encountering the ) value Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 14 of 34
15. 15. Implementing Stacks and Queues UsingLinked ListImplementing 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 15 of 34
16. 16. Implementing Stacks and Queues UsingLinked ListImplementing Stacks (Contd..) Example: class Stack { private: //Member data Node *top; public: Stack(); void PUSH(node *); Node *POP(); ~Stack(); }; Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 16 of 34
17. 17. Implementing Stacks and Queues UsingLinked ListProblem Statement 3.D.1Create an application that implements stack by usingsingle linked list and prints the accepted input in reverseorder. Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 17 of 34
18. 18. Implementing Stacks and Queues UsingLinked ListProblem Statement 3.P.1Create an application that accepts an expression withround brackets and validates it. The application shouldensure that the number of opening brackets should beequivalent to the number of closing brackets.Hint: An expression (2+3)*5 can be used to evaluatethe given program. Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 18 of 34
19. 19. Implementing Stacks and Queues UsingLinked ListQueues Are sequential lists in which items are inserted into the tail end of the queue and taken from the head Example: Printer queue Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 19 of 34
20. 20. Implementing Stacks and Queues UsingLinked ListQueues (Contd..) Are also called First In First Out (FIFO) lists REAR B C X D G FRONT Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 20 of 34
21. 21. Implementing Stacks and Queues UsingLinked ListTypes of Queues Deques Priority queues Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 21 of 34
22. 22. Implementing Stacks and Queues UsingLinked ListDeques 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 22 of 34
23. 23. Implementing Stacks and Queues UsingLinked ListDeques (Contd..) Are of two types: Input restricted deques Output restricted deques Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 23 of 34
24. 24. Implementing Stacks and Queues UsingLinked ListPriority 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 by a time-sharing system Are used by operating system schedulers for scheduling activities to be processed by the microprocessor Are used by simulation applications Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 24 of 34
25. 25. Implementing Stacks and Queues UsingLinked ListPriority 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 25 of 34
26. 26. Implementing Stacks and Queues UsingLinked ListOperations 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 26 of 34
27. 27. Implementing Stacks and Queues UsingLinked ListImplementing Queues Implementing queues by using arrays is not flexible as an array is of a fixed size Implementing queues by using linked list is flexible as when a new item is added, it is simply linked to the end of the queue. A queue when implemented using linked lists is called a linked queue. Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 27 of 34
28. 28. Implementing Stacks and Queues UsingLinked ListProblem Statement 3.D.2Create an application that will accept the applicantnames as and when they appear for interview. The list ofapplicants should be updated every hour. Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 28 of 34
29. 29. Implementing Stacks and Queues UsingLinked ListProblem Statement 3.P.2Create an application that will print the details of the firstfive early bird prize winners.Hint: The details to be accepted are the name and e-mailid. Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 29 of 34
30. 30. Implementing Stacks and Queues UsingLinked ListSummaryIn 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 30 of 34
31. 31. Implementing Stacks and Queues UsingLinked ListSummary (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 by using arrays or linked lists Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 31 of 34
32. 32. Implementing Stacks and Queues UsingLinked ListSummary (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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 32 of 34
33. 33. Implementing Stacks and Queues UsingLinked ListSummary (Contd..) A deques is also called a double-ended queue. In this,elements are added or removed from both ends of thequeue 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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 33 of 34
34. 34. Implementing Stacks and Queues UsingLinked ListSummary (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 Implementing Stacks and Queues Using Linked ©NIIT List/Lesson 3/Slide 34 of 34