The document describes stacks and queues. Stacks are linear lists that only allow insertions and deletions at one end, following the LIFO principle. Queues are linear lists that only allow insertions at one end and deletions at the other, following the FIFO principle. Common applications of stacks include expression evaluation and recursion, while queues are often used for task scheduling and message buffering.
1.
Linear list in which insertions and deletions
can take place only at the top
Last-In-First-Out (LIFO) list
Insertion (push)
Deletion (pop)
1
3.
Linear array or a linear linked list
Representing a Stack using an Array
Requirements
a variable, called top that holds the
index of the top element
an array to hold the elements of the
stack
3
4. const int MAX = 10;
class stack
{
int top, list [MAX];
public:
stack() { top= –1;}
void push (int);
int pop();
int full();
int empty();
void display();
};
4
5.
Inserts an element X onto a stack S of size MAX. TOP is
a pointer to the top element of the stack.
1. [Check for stack overflow]
If TOP MAX
then Write ('STACK OVERFLOW')
Return
2. [Increment TOP]
TOP TOP + 1
3. [Insert element]
S [TOP] X
4. [Finish]
5
7.
Removes and returns the top element from a stack S.
TOP is a pointer to the top element of the stack.
1. [Check for stack underflow]
If TOP = –1
then Write('STACK UNDERFLOW')
Return
2. [Decrement pointer]
TOP TOP – 1
3. [Return former top element of stack and Finish]
Return (S [TOP + 1])
7
13.
(A – B / C) * (A * K – L)
(A – [/BC]) * ([* AK] – L)
[–A/BC] * [–*AKL]
*–A/BC–*AKL
13
14.
(A – B / C) * (A * K – L)
(A – [BC/]) * ([AK*] – L)
[ABC/–] * [AK*L –]
ABC/–AK*L–*
14
15.
The order of operations determined by the
positions of the operators and operands
Parentheses are not needed
15
16.
Programs that perform some sort of translation
often use stacks
Example: The translation of an arithmetic expression
from infix notation to prefix or postfix notation.
Using infix notation, one cannot tell the order
in which the operators should be applied by
looking at the expression
Multiple operations use precedence rules
The expression in the postfix notation is much
easy to evaluate
16
17.
As the operands appear before the operator, there is no need
for operator precedence or for parentheses to group/order
operations.
In order to evaluate a postfix expression
It is scanned from left to right.
Operands are pushed onto a stack
When an operator is encountered
pop top one or two operands (depending on the operator)
perform the operation and place the result back on the stack.
At the end of the expression, the stack contains only one
element (if expression is correct) and that is the resulting
value of the expression
17
18. Input: Infix string.
Output: Postfix string.
1.
Examine the next element in the input.
2.
If it is an operand, output it, go to step 6.
3.
If it is an opening parenthesis, push it, go to step 6.
4.
If it is an operator, then
a.
If stack is empty, or the top of the stack is opening parenthesis, push
the operator.
b. If the operator has higher priority than the top of stack, push the
operator.
Else pop the operator from the stack and output it, repeat step 4a.
5.
If it is a closing parenthesis, pop operators from the stack and output
them until an opening parenthesis is encountered. Pop and discard the
opening parenthesis.
6.
If there is more input, go to step 1.
Else pop the remaining operators to output
18
20. Initialize an empty stack and scan the input string from right to left.
Examine the next element in the input.
If it is an operand, add it to output string, go to step 7.
If it is a closing parenthesis, push it, go to step 7.
If it is an operator, then
a) If stack is empty, or if the top of stack is a closing parenthesis, push the
operator.
b) If the operator has same or higher priority than the top of stack, push
the operator.
Else pop the operator from the stack and add it to output string, go to
step 5a.
6. If it is an opening parenthesis, pop operators from stack and add them to
output string until a closing parenthesis is encountered. Pop and discard
the closing parenthesis.
7. If there is more input, go to step 2.
Else pop the remaining operators and add them to output string.
8. Reverse the output string.
1.
2.
3.
4.
5.
20
22.
Function Calls
Implementing Recursion
Stacks are used to pass parameters between functions. On
call to a function, the parameters and local variables are
stored on a stack.
There are many problems whose algorithmic description is
best described in a recursive manner. Many high-level
programming languages provide support for recursion use
stack.
Checking the validity or correctness of mathematical
expressions.
Conversion and evaluation of mathematical
expressions.
22
23.
An ordered collection of items
deletion at one end (front)
insertion at the other end (rear)
Elements are processed in the same order as
they join the queue
first-in, first-out (FIFO)
first-come, first-served (FCFS)
23
25. 1. [Check for Queue Overflow]
If R N then
Write ('OVERFLOW')
Return
2. [Increment rear pointer]
R R+1
3. [Insert element]
Q(R) X
4. [Is front pointer properly set?]
If F = 0 then
F 1
5. [Finish]
Return
25
26. 1.
2.
3.
4.
[Check for Queue Underflow]
If F = 0 then
Write ('UNDERFLOW')
Return (0)
(0 denotes an empty queue)
[Delete element]
Y Q(F)
[Queue empty?]
If F = R then
F R 0
else
F F+1
[Return element and Finish]
Return (Y)
26
27. Empty
(i)
F R
A
R
Insert B
F
C
(v)
Delete A
R
(ii)
B
C
Insert C
R
(iv)
C
Delete B
F R
(vi)
C
D
F
(vii)
A
F
(iii)
B
Insert A
F R
B
F
A
R
Insert D
C
F
(viii)
D
Insert E
R (Overflow)
27
29. 1.
2.
3.
4.
5.
[Reset rear pointer]
If R = N then
R 1
else
R R+1
[Overflow?]
If F = R then
Write ('OVERFLOW') and Return
[Insert element]
Q[R] X
[Is front pointer properly set?]
If F = 0 then
F 1
[Finish]
Return
29
30. This algorithm deletes and returns the last element
of the queue. Y is a temporary variable.
1. [Underflow?]
If F = 0 then
Write('UNDERFLOW'),
Return (0)
2. [Delete element]
Y Q[F]
3. [Queue empty?]
If F = R then F R
0
4.[Increment front pointer] If F = N then F 1
else F F + 1
5. [Return element and Finish] Return (Y)
30
31.
A queue in which we are able to insert items or
remove items from any position based on some
property (such as priority of the task to be
processed) is often referred to as a priority
queue.
Priorities of 1, 2, and 3 are attached to jobs of
type real-time, on-line, and batch, respectively.
If a job is initiated with priority i, it is inserted
immediately at the end of the list of other jobs
with priority i, for i = 1, 2, or 3.
31
32.
Ascending / descending priority
In an ascending priority queue, elements are
processed in the increasing order of their
priority values.
In a descending priority queue, elements are
processed in the decreasing order of their
priority values.
In both the types, elements with same priority
are processed according to the order in which
they are inserted.
32
33.
In simulations and operating systems.
Operating systems often maintain a queue of processes
that are ready to execute or that are waiting for a
particular event to occur.
“holding area” for messages between two processes, two
programs, or even two systems is (called a “buffer”) often
implemented as a queue.
A file/print server in a computer network handles file
access request from many clients throughout the network.
Servers have a limited capacity to service request from
clients. When that capacity is exceeded, client requests
wait in queues.
33
34.
A homogeneous collection of elements, with a
linear relationship between the elements.
Lists can be unordered - their elements can be
placed without a particular order, or ordered –
their elements are placed in a particular order.
Stacks and queues are lists whose elements are
ordered based on the sequence in which they
have been inserted.
The lists can also be ordered by the value.
34
35.
Ex. A list ordered alphabetically, and a list ordered
numerically (in ascending or descending order).
Value-ordered lists or sorted lists.
On one of the fields in the record, the record key.
Ex. A list of students in a class can be ordered
alphabetically by name, or numerically by roll
number.
Key-ordered-lists.
Lists can be implemented using
Linear Arrays or Linked Lists
35
36.
A linked list is a linear collection of similar
elements, called nodes.
The linear order is given by pointers.
Each node is divided into two or more parts.
A linked list can be one of the following types:
Linear linked list, or singly linked list, or one-way
list
Doubly linked list or two-way list
Circular linked list
36