07 DS and Algorithm Session 10
-
Upload
ashish-srivastava -
Category
Documents
-
view
225 -
download
1
Transcript of 07 DS and Algorithm Session 10
-
8/2/2019 07 DS and Algorithm Session 10
1/64
Data Structures and Algorithms
Session 10Ver. 1.0
Objectives
In this session, you will learn to:
Identify the features of a stack
Implement stacks
Apply stacks to solve programming problems
-
8/2/2019 07 DS and Algorithm Session 10
2/64
Data Structures and Algorithms
Session 10Ver. 1.0
Let us play the game of Rummy.
Stacks
7 7
77
7 7
77
6 6
66
6 6
66
6 6
66
6 6
66
-
8/2/2019 07 DS and Algorithm Session 10
3/64
Data Structures and Algorithms
Session 10Ver. 1.0
What is a Stack ?A stack is a collection of data items that can be accessed at
only one end, called top.
Items can be inserted and deleted in a stack only at the top.
The last item inserted in a stack is the first one to be
deleted.Therefore, a stack is called a Last-In-First-Out (LIFO) data
structure.
Defining a Stack
-
8/2/2019 07 DS and Algorithm Session 10
4/64
Data Structures and Algorithms
Session 10Ver. 1.0
There are two basic operations that are performed on
stacks:
PUSH
POP
Identifying the Operations on Stacks
PUSH: It is the process of inserting a new element on the
top of a stack.
Empty Stack
1
Push an
Element 1
-
8/2/2019 07 DS and Algorithm Session 10
5/64
Data Structures and Algorithms
Session 10Ver. 1.0
PUSH: It is the process of inserting a new element on the
top of a stack.
Identifying the Operations on Stacks (Contd.)
1
Push an
Element 2
2
Push an
Element 3
3
-
8/2/2019 07 DS and Algorithm Session 10
6/64
Data Structures and Algorithms
Session 10Ver. 1.0
POP: It is the process of deleting an element from the top of
a stack.
Identifying the Operations on Stacks (Contd.)
1
POP an
Element
3
2
3
POP an
Element
2
-
8/2/2019 07 DS and Algorithm Session 10
7/64
Data Structures and Algorithms
Session 10Ver. 1.0
Elements in stacks are inserted and deleted on a
___________ basis.
Just a minute
Answer:
LIFO
-
8/2/2019 07 DS and Algorithm Session 10
8/64
Data Structures and Algorithms
Session 10Ver. 1.0
List down some real life examples that work on the LIFO
principle.
Just a minute
Answer:
Pile of books: Suppose a set of books are placed one over
the other in a pile. When you remove books from the pile, thetopmost book will be removed first. Similarly, when you have
to add a book to the pile, the book will be placed at the top of
the pile.
Pile of plates: The first plate begins the pile. The second
plate is placed on the top of the first plate and the third plate is
placed on the top of the second plate, and so on. In general, ifyou want to add a plate to the pile, you can keep it on the top
of the pile. Similarly, if you want to remove a plate, you can
remove the plate from the top of the pile.
Bangles in a hand: When a person wears bangles, the last
bangle worn is the first one to be removed.
-
8/2/2019 07 DS and Algorithm Session 10
9/64
Data Structures and Algorithms
Session 10Ver. 1.0
You need to develop a method to check if the parentheses
in an arithmetic expression are correctly nested.
How will you solve this problem?
You can solve this problem easily by using a stack.
Implementing Stacks
-
8/2/2019 07 DS and Algorithm Session 10
10/64
Data Structures and Algorithms
Session 10Ver. 1.0
Consider an example.
Suppose the expression is:
{(a + b) (c + d) + (c d)]}
Scan the expression from
left to right.The first entry to be
scanned is {, which is a left
parenthesis.
Push it into the stack.
{
{(a + b) (c + d) + (c d)]}
Implementing Stacks (Contd.)
-
8/2/2019 07 DS and Algorithm Session 10
11/64
Data Structures and Algorithms
Session 10Ver. 1.0
The next entry to be
scanned is (, which is a left
parenthesis.
Push it into the stack.
The next entry is a, whichis an operand. Therefore, it
is discarded.
The next entry is +, which
is an operator. Therefore, it
is discarded.The next entry is b, which
is an operand. Therefore, it
is discarded.
{(
{(a + b) (c + d) + (c d)]}
Implementing Stacks (Contd.)
-
8/2/2019 07 DS and Algorithm Session 10
12/64
Data Structures and Algorithms
Session 10Ver. 1.0
The next entry to be
scanned is ), which is a
right parenthesis
POP the topmost entry from
the stack.Match the two brackets.
{(
)(
Brackets Matched
{(a + b) (c + d) + (c d)]}
Implementing Stacks (Contd.)
-
8/2/2019 07 DS and Algorithm Session 10
13/64
Data Structures and Algorithms
Session 10Ver. 1.0
The next entry to be scannedis , which is an operator.
Therefore, it is discarded.
The next entry to be scanned
is (, which is a left
parenthesisPush it into the stack
The next entry to be scanned
is c, which is an operand.
Therefore it is discarded
The next entry to be scannedis +, which is an operator.
Therefore it is discarded
The next entry to be scanned
is d, which is an operand.
Therefore it is discarded
{(
{(a + b) (c + d) + (c d)]}
Implementing Stacks (Contd.)
-
8/2/2019 07 DS and Algorithm Session 10
14/64
Data Structures and Algorithms
Session 10Ver. 1.0
The next entry to be
scanned is ), which is a
right parenthesis.
POP the topmost element
from the stack.Match the two brackets.
{
(
)(
Brackets Matched
{(a + b) (c + d) + (c d)]}
Implementing Stacks (Contd.)
-
8/2/2019 07 DS and Algorithm Session 10
15/64
Data Structures and Algorithms
Session 10Ver. 1.0
The next entry to be scannedis +, which is an operator.
Therefore, it is discarded.
The next entry to be scanned
is (, which is a left
parenthesis.Push it into the stack.
The next entry to be scanned
is c, which is an operand.
Therefore, it is discarded.
The next entry to be scannedis , which is an operator.
Therefore, it is discarded.
The next entry to be scanned
is d, which is an operand.
Therefore, it is discarded.
{
(
{(a + b) (c + d) + (c d)]}
Implementing Stacks (Contd.)
-
8/2/2019 07 DS and Algorithm Session 10
16/64
Data Structures and Algorithms
Session 10Ver. 1.0
The next entry to be
scanned is ), which is a
right parenthesis.
POP the topmost element
from the stack.
Match the two brackets.
{
(
)(
Brackets Matched
{(a + b) (c + d) + (c d)]}
Implementing Stacks (Contd.)
-
8/2/2019 07 DS and Algorithm Session 10
17/64
Data Structures and Algorithms
Session 10Ver. 1.0
The next entry to be
scanned is ], which is a
right parenthesis.
POP the topmost element
from the stack.
Match the two brackets.
{
]{
Brackets Do Not MatchThe Expression is INVALID
{(a + b) (c + d) + (c d)]}
Implementing Stacks (Contd.)
-
8/2/2019 07 DS and Algorithm Session 10
18/64
Data Structures and Algorithms
Session 10Ver. 1.0
A stack is similar to a list in which insertion and deletion is
allowed only at one end.
Therefore, similar to a list, stack can be implemented using
both arrays and linked lists.
To implement a stack using an array:
Declare an array:
int Stack[5]; // Maximum size needs to be specified in// advance
Declare a variable, top to hold the index of the topmost
element in the stacks:
int top;
Initially, when the stack is empty, set:
top = 1
Implementing a Stack Using an Array
-
8/2/2019 07 DS and Algorithm Session 10
19/64
Data Structures and Algorithms
Session 10Ver. 1.0
Let us now write an algorithm for
the PUSH operation.
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
2. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.top = 1
PUSH an element 3
Stack
210 43
Initially:
-
8/2/2019 07 DS and Algorithm Session 10
20/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
10
top =1
Stack
210 43
PUSH an element 3
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
21/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
10
top = 0
Stack
210 43
top = 0
PUSH an element 3
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
22/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
10
top = 0
10Stack
210 43
3 Item pushed
PUSH an element 3
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
23/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
10
top = 0
10Stack
210 43
3
PUSH an element 8
1. Increment top by 1.
2. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
24/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
10
top = 0
10Stack
210 43
3
top = 1
PUSH an element 8
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
25/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3
top = 1
8 Item pushed
PUSH an element 8
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
26/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3
top = 1
8
PUSH an element 5
1. Increment top by 1.
2. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
27/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3
top = 1
8
top = 2
PUSH an element 5
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
28/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3 8
top = 2
5 Item pushed
PUSH an element 5
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
29/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3 8
top = 2
5
PUSH an element 1
1. Increment top by 1.
2. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
30/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3 8
top = 2
5
top = 3
PUSH an element 1
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
31/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3 8 5
top = 3
1 Item pushed
PUSH an element 1
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
32/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3 8 5
top = 3
1
PUSH an element 9
1. Increment top by 1.
2. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
33/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3 8 5
top = 3
1
top = 4
PUSH an element 9
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
34/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3 8 5 1
top = 4
9 Item pushed
PUSH an element 9
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
35/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3 8 5 1
top = 4
9
PUSH an element 2
1. Increment top by 1.
2. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
36/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3 8 5 1
top = 4
9
top = 5
PUSH an element 2
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
37/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3 8 5 1 9
top = 5
Stack overflow
PUSH an element 2
1. Increment top by 1.
1. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
38/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
1010Stack
210 43
3 8 5 1 9
The stack has been implemented
in an array of size 5.
Therefore, you cannot store more
than 5 elements in the stack.
To avoid the stack overflow, you
need to check for the stack full
condition before pushing an element
into the stack.
Let us modify the algorithm to check
for this condition.
1. If top = MAX 1:
a. Display Stack Full
b. Exit
2. Increment top by 1
3. Store the value to be
pushed at index top inthe array
1. Increment top by 1.
2. Store the value to be
pushed at index top in
the array. Top now
contains the index of the
topmost element.
-
8/2/2019 07 DS and Algorithm Session 10
39/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using an Array (Contd.)
Write an algorithm to implement the POP operation on a
stack.
Algorithm for POP operation:
1. If top = 1:
a. Display Stack Empty
b. Exit
2. Retrieve the value stored at index top
3. Decrement top by 1
-
8/2/2019 07 DS and Algorithm Session 10
40/64
Data Structures and Algorithms
Session 10Ver. 1.0
In a stack, data can be stored and removed only from one
end of the stack called the ______ of the stack.
Just a minute
Answer:
top
-
8/2/2019 07 DS and Algorithm Session 10
41/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing a Stack Using a Linked List
Write an algorithm to implement the PUSH and POP
operations using a Linked List.
The algorithm for PUSH operation is as follows:
1. Allocate memory for the new node.
2. Assign value to the data field of the new node.
3. Make the next field of the new node point to top.
4. Make top point to the new node.
The algorithm for POP operation is as follows:
1. Make a variable/pointer tmp point to the topmost node.
2. Retrieve the value contained in the topmost node.
3. Make top point to the next node in sequence.
4. Release memory allocated to the node marked by tmp.
-
8/2/2019 07 DS and Algorithm Session 10
42/64
Data Structures and Algorithms
Session 10Ver. 1.0
Problem Statement:
Write a program to implement a stack by using an array that
can store five elements.
Activity: Implementing a Stack Using an Array
-
8/2/2019 07 DS and Algorithm Session 10
43/64
Data Structures and Algorithms
Session 10Ver. 1.0
Problem Statement:
Write a program to implement a stack by using a linked list.
Activity: Implementing a Stack Using a Linked List
-
8/2/2019 07 DS and Algorithm Session 10
44/64
Data Structures and Algorithms
Session 10Ver. 1.0
What will be the condition for stack full in a stack
implemented as a linked list?
Just a minute
Answer:
When a stack is implemented as a linked list, there is no upperbound limit on the size of the stack. Therefore, there will be no
stack full condition in this case.
-
8/2/2019 07 DS and Algorithm Session 10
45/64
Data Structures and Algorithms
Session 10Ver. 1.0
If a stack is represented in memory by using a linked list,
then insertion and deletion of data will be done ________.
1. At the end of the list
2. At the beginning of the list
3. Anywhere in the list
4. At the beginning and at the end of the list respectively
Just a minute
Answer:2. At the beginning of the list
-
8/2/2019 07 DS and Algorithm Session 10
46/64
Data Structures and Algorithms
Session 10Ver. 1.0
Some of the applications of stacks are:
Implementing function calls
Maintaining the UNDO list for an application
Checking the nesting of parentheses in an expression
Evaluating expressions
Applications of Stacks
-
8/2/2019 07 DS and Algorithm Session 10
47/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing function calls:
Consider an example. There are three functions, F1, F2, and
F3. Function F1 invokes F2 and function F2 invokes F3, as
shown.
Implementing Function Cal ls
-
8/2/2019 07 DS and Algorithm Session 10
48/64
Data Structures and Algorithms
Session 10Ver. 1.0
Implementing Function Cal ls (Contd.)Assuming these instructions at the
given locations in the memory.1100
1101
1103
1120
1121
1122
1140
1141
1102
void F1()
{
int x;
x = 5;
F2();
print(x);
}void F2(int x)
{
x = x + 5;
F3(x);
print(x);
}
void F3(int x)
{
x = x 2;
print x;
}
-
8/2/2019 07 DS and Algorithm Session 10
49/64
Data Structures and Algorithms
Session 10Ver. 1.0
The execution starts fromfunction F11100
1101
1103
1120
1121
1122
1140
1141
1102
Implementing Function Cal ls (Contd.)void F1()
{
int x;
x = 5;
F2();
print(x);
}void F2(int x)
{
x = x + 5;
F3(x);
print(x);
}
void F3(int x)
{
x = x 2;
print x;
}
-
8/2/2019 07 DS and Algorithm Session 10
50/64
Data Structures and Algorithms
Session 10Ver. 1.0
void F1()
{
int x;
x = 5;
F2();
print(x);
}void F2(int x)
{
x = x + 5;
F3(x);
print(x);
}
void F3(int x)
{
x = x 2;
print x;
}
1100
1101
1103
1120
1121
1122
1140
1141
1102
Implementing Function Cal ls (Contd.)
-
8/2/2019 07 DS and Algorithm Session 10
51/64
Data Structures and Algorithms
Session 10Ver. 1.0
x = 5
1100
1101
1103
1120
1121
1122
1140
1141
1102
Implementing Function Cal ls (Contd.)void F1()
{
int x;
x = 5;
F2();
print(x);
}void F2(int x)
{
x = x + 5;
F3(x);
print(x);
}
void F3(int x)
{
x = x 2;
print x;
}
-
8/2/2019 07 DS and Algorithm Session 10
52/64
Data Structures and Algorithms
Session 10Ver. 1.0
x = 5
Address and the local variable of F1
1100
1101
1103
1120
1121
1122
1140
1141
1102
Implementing Function Cal ls (Contd.)void F1()
{
int x;
x = 5;
F2();
print(x);
}void F2(int x)
{
x = x + 5;
F3(x);
print(x);
}
void F3(int x)
{
x = x 2;
print x;
}
1103, x = 5
-
8/2/2019 07 DS and Algorithm Session 10
53/64
Data Structures and Algorithms
Session 10Ver. 1.0
x = 10
1100
1101
1103
1120
1121
1122
1140
1141
1102
x = 5
Implementing Function Cal ls (Contd.)void F1()
{
int x;
x = 5;
F2();
print(x);
}
void F2(int x)
{
x = x + 5;
F3(x);
print(x);
}
void F3(int x)
{
x = x 2;
print x;
}
1103, x = 5
-
8/2/2019 07 DS and Algorithm Session 10
54/64
Data Structures and Algorithms
Session 10Ver. 1.0
1122, x = 10
Address and the local variable of F2
1100
1101
1103
1120
1121
1122
1140
1141
1102
x = 10
Implementing Function Cal ls (Contd.)void F1()
{
int x;
x = 5;
F2();
print(x);
}
void F2(int x)
{
x = x + 5;
F3(x);
print(x);
}
void F3(int x)
{
x = x 2;
print x;
}
1103, x = 5
-
8/2/2019 07 DS and Algorithm Session 10
55/64
Data Structures and Algorithms
Session 10Ver. 1.0
x = 20
Address and the local variable of F2
1100
1101
1103
1120
1121
1122
1140
1141
1102
x = 10
1122, x = 10
Implementing Function Cal ls (Contd.)void F1()
{
int x;
x = 5;
F2();
print(x);
}
void F2(int x)
{
x = x + 5;
F3(x);
print(x);
}
void F3(int x)
{
x = x 2;
print x;
}
1103, x = 5
-
8/2/2019 07 DS and Algorithm Session 10
56/64
Data Structures and Algorithms
Session 10Ver. 1.0
1100
1101
1103
1120
1121
1122
1140
1141
1102
20
x = 20x = 10
1122, x = 10
1122, x = 10
Implementing Function Cal ls (Contd.)void F1()
{
int x;
x = 5;
F2();
print(x);
}
void F2(int x)
{
x = x + 5;
F3(x);
print(x);
}
void F3(int x)
{
x = x 2;
print x;
}
1103, x = 5
-
8/2/2019 07 DS and Algorithm Session 10
57/64
Data Structures and Algorithms
Session 10Ver. 1.0
1100
1101
1103
1120
1121
1122
1140
1141
1102
20 10
x = 5
1103, x = 5
x = 10
Implementing Function Cal ls (Contd.)void F1()
{
int x;
x = 5;
F2();
print(x);
}
void F2(int x)
{
x = x + 5;
F3(x);
print(x);
}
void F3(int x)
{
x = x 2;
print x;
}
1103, x = 5
-
8/2/2019 07 DS and Algorithm Session 10
58/64
Data Structures and Algorithms
Session 10Ver. 1.0
1100
1101
1103
1120
1121
1122
1140
1141
1102
20 10 5
x = 5
Implementing Function Cal ls (Contd.)void F1()
{
int x;
x = 5;
F2();
print(x);
}
void F2(int x)
{
x = x + 5;
F3(x);
print(x);
}
void F3(int x)
{
x = x 2;
print x;
}
-
8/2/2019 07 DS and Algorithm Session 10
59/64
Data Structures and Algorithms
Session 10Ver. 1.0
Maintaining the UNDO list for an application:
Consider that you made some changes in a Word document.
Now, you want to revert back those changes. You can revert
those changes with the help of an UNDO feature.
The UNDO feature reverts the changes in a LIFO manner.
This means that the change that was made last is the first oneto be reverted.
You can implement the UNDO list by using a stack.
Maintaining the UNDO list for an Application
-
8/2/2019 07 DS and Algorithm Session 10
60/64
Data Structures and Algorithms
Session 10Ver. 1.0
Checking the nesting of parentheses in an expression:
You can do this by checking the following two conditions:
The number of left parenthesis should be equal to the number of
right parenthesis.
Each right parenthesis is preceded by a matching left
parenthesis.
Checking the Nesting of Parentheses in an Expression
-
8/2/2019 07 DS and Algorithm Session 10
61/64
Data Structures and Algorithms
Session 10Ver. 1.0
Evaluating Expressions
Evaluating an expression by using stacks:
Stacks can be used to solve complex arithmetic expressions.
The evaluation of an expression is done in two steps:
Conversion of the infix expression into a postfix expression.
Evaluation of the postfix expression.
Data Structures and Algorithms
-
8/2/2019 07 DS and Algorithm Session 10
62/64
Session 10Ver. 1.0
Problem Statement:
Write a program that accepts an infix expression, and then
converts it into a postfix expression. You can assume that the
entered expression is a valid infix expression.
Activity: Implementing a Stack using a Linked List
Data Structures and Algorithms
-
8/2/2019 07 DS and Algorithm Session 10
63/64
Session 10Ver. 1.0
Summary
In this session, you learned that:
A stack is a collection of data items that can be accessed at
only one end, called top. The last item inserted in a stack is the
first one to be deleted.
A stack is called a LIFO data structure.
There are two operations that can be performed on stacks.They are:
PUSH
POP
Stacks can be implemented by using both arrays and linked
lists.
Data Structures and Algorithms
-
8/2/2019 07 DS and Algorithm Session 10
64/64
Summary (Contd.)
Stacks are used in many applications. Some of the applicationdomains of stacks are as follows:
Implementing function calls
Maintaining the UNDO list for an application
Checking the nesting of parentheses in an expression
Evaluating expressions