Weitere ähnliche Inhalte
Kürzlich hochgeladen (20)
Ds 3
- 1. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 1 of 34
- 2. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 2 of 34
- 3. Implementing Stacks and Queues Using
Linked List
Stacks (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. Implementing Stacks and Queues Using
Linked List
Stacks (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. Implementing Stacks and Queues Using
Linked List
Stack 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. Implementing Stacks and Queues Using
Linked List
Stack 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. Implementing Stacks and Queues Using
Linked List
Application 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. Implementing Stacks and Queues Using
Linked List
Application 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. Implementing Stacks and Queues Using
Linked List
Infix 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. Implementing Stacks and Queues Using
Linked List
Postfix 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. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 11 of 34
- 12. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 12 of 34
- 13. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 13 of 34
- 14. Implementing Stacks and Queues Using
Linked List
Evaluation 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. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 15 of 34
- 16. Implementing Stacks and Queues Using
Linked List
Implementing 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. Implementing Stacks and Queues Using
Linked List
Problem Statement 3.D.1
Create an application that implements stack by using
single linked list and prints the accepted input in reverse
order.
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 17 of 34
- 18. Implementing Stacks and Queues Using
Linked List
Problem Statement 3.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.
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 18 of 34
- 19. Implementing Stacks and Queues Using
Linked List
Queues
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. Implementing Stacks and Queues Using
Linked List
Queues (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. Implementing Stacks and Queues Using
Linked List
Types of Queues
Deques
Priority queues
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 21 of 34
- 22. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 22 of 34
- 23. Implementing Stacks and Queues Using
Linked List
Deques (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. Implementing Stacks and Queues Using
Linked List
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 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. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 25 of 34
- 26. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 26 of 34
- 27. Implementing Stacks and Queues Using
Linked List
Implementing 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. Implementing Stacks and Queues Using
Linked List
Problem Statement 3.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.
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 28 of 34
- 29. Implementing Stacks and Queues Using
Linked List
Problem Statement 3.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 e-mail
id.
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 29 of 34
- 30. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 30 of 34
- 31. Implementing Stacks and Queues Using
Linked List
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 by using arrays or linked
lists
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 31 of 34
- 32. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 32 of 34
- 33. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 33 of 34
- 34. Implementing Stacks and Queues Using
Linked List
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
Implementing Stacks and Queues Using Linked
©NIIT
List/Lesson 3/Slide 34 of 34