Professional Documents
Culture Documents
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Understand all the aspects of a stack as data type including :
Last In, First Out (LIFO) data access
Push, Pop, and other stack operations
Contiguous implementation of a stack
Learn realization of stack using arrays(: Contiguous stack)
Learn to choose appropriate realization suitable for practical
applications
Learn and implement the multi-stacks
Use of stacks in expression conversion, recursion, reversing data and
many more
2
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
A stack is an ordered list in which all insertions and deletions
are made at one end, called the top
3
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Following are some examples in which where we need to use
stacks are generally used:
4
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
An array is a finite ordered collection of homogeneous data
elements which provides direct access (or random access) to
any of its elements.
An array as a data structure is defined as a set of pairs (index,
value) such that with each index a value is associated.
index — indicates the location of an element in an array.
value - indicates the actual value of that data element.
Declaration of an array in ‘C++’:
int Array_A[20];
5
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
6
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
The Stack Full Condition (Stack
Capacity =3)
7
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
The Pop Operation
8
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
The Empty Stack
9
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
The Get Top Operation
10
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
11
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
A polish Mathematician Han Lukasiewicz suggested a notation
called polish notation, which gives two alternatives to represent an
arithmetic expression the notations are Postfix and prefix notation
12
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
……….. C B A
13
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Stacks Using Template
A template is variable which can be instantiated to any
data type
This data type could be build in or user defined type
14
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Stack 1 Stack2
A 1 2 3 ……….. B A
0 1 3 n-3 n-1
2 n-2
Top1 =2 Top2=n-2
15 15
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Initial configuration for m stacks in A [0,
…, n-1]
16
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Applications Of Stack
Convert infix expression to postfix and prefix
expressions
Evaluate the postfix expression
Reverse a string
Check well-formed (nested) parenthesis
Reverse a string
Process subprogram function calls
Parse (analyze the structure) of computer programs
Simulate recursion
In computations like decimal to binary conversion
In Backtracking algorithms (often used in
optimizations
and in games)
17
Expression Evaluation And
Conversion
The most frequent application of stacks is in evaluation of
arithmetic expressions.
18
The following operators are written is in
descending order of their precedence:
<, £ , =, ¹, ³, >
AND
OR
19
The Operators and priorities
21
The example expression
… in various forms-
infix, prefix and postfix
22
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Repetition Construct
23
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Relationship between Data, Data
Structures, and Algorithms
24
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Flow Charts
A very effective tool to show the logic flow of a program
A flow chart is a pictorial representation of an algorithm
It hides all of the details of an algorithm by giving the picture;
It shows how the algorithm flows from beginning to end
25
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Flow Charts
26
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Analysis of Algorithms
Complexity of Algorithms
Space Complexity
Time Complexity
Computing Time Complexity of Algorithm
Big-O Notation
27
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Space Complexity
Amount of computer memory required during the program
execution, as a function of the input size
Space complexity measurement which is space requirement of
an algorithm can be done at two different times:
28
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Compile time space
complexity
Compile time space complexity is defined as the storage
requirement of a program at compile time
29
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Runtime space
complexity
If program is recursive or uses dynamic variables or dynamic
data structure then there is a need to determine space
complexity at runtime
The memory requirement is summation of the
program space
data space and
stack space
30
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Time Complexity
The time complexity of an algorithm is a measure of how much
time is required to execute an algorithm for a given number of
inputs
Time Complexity T(P) is the time taken by program P and the
sum of the compile and execution time
31
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Best, Worst and Average Cases
32
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Computing Time Complexity of
Algorithm
The total time taken by the algorithm or program is calculated
using the sum of the time taken by each of executable statement in
algorithm or program
Time required by each statement depends on
Time required for executing it once.
Number of times the statement is executed.
33
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Software Engineering
Software Engineering is the establishment and use of sound
engineering methods and principles to obtain reliable software that
works on real machines.
Cycle (SDLC)
34
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Software Engineering
Analysis Phase
Design Phase
Implementation Phase
Testing Phase
Verification
35
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Software Engineering
Analysis Phase
Design Phase
Implementation Phase
Testing Phase
Verification
36
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Analysis Phase
Define the User
Define the Needs
Design Phase
Define the Methods
37
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Design Phase
Modularity:
The design phase uses a very well-established principle called
modularity
Tools
38
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Implementation Phase
Tools
flowchart
Pseudo Code
Coding
39
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Testing Phase
Testing Phase
Once the programs have been written, they must be
tested.
There are two types of testing:
Black box and
White box
40
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Verification
Program verification is a process to prove that the
program does what it is intended to do.
It is said that 'even verification must be verified'.
This means, along with system, tests made are to be
verified.
Also, every software quality must be verified.
41
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Summary
A stack is an ordered list in which all insertions and deletions are
made at one end, called the top
Adding an element is called as pushing the element onto the
stack. The function, which does this, is called ‘push’.
Removing an element from the stack is called as popping the
element from the stack and the function, which does this, is
called ‘pop’
Stack can be implemented using arrays or using linked lists.
For array implementation its size should be predefined and also it
cannot exceed run time
Stack is used in wide number of applications such as recursion,
expression conversion, well -formed parenthesis check etc..
Program verification is a process to prove that the program does
what it is intended to do
It is said that 'even verification must be verified'
This means, along with system, tests made are to be verified.
Also, every software quality must be verified
42
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Summary
The most frequent application of stack is in evaluation of arithmetic
expressions. The conventional way of writing the expression is called infix,
because the binary operators occur in between the operands and unary
operators precede their operand.
Polish Mathematician Han Lukasiewicz suggested a notation called Polish
notion, which gives two alternatives to represent an arithmetic
expression, the notations are postfix and prefix notations.
In postfix notation, the operator is written after its operands, whereas in
prefix notation the operator precedes its operands.·
The postfix expressions can be evaluated easily hence infix expression is
converted into postfix expression using stack.
In computer programming processing of function calls and their terminations
uses stack.
Stack is used to remember the place where the call was made; so that it can
return there after the function is complet the check etc..
Program verification is a process to prove that the program does what it is
intended to do.
It is said that 'even verification must be verified'.
This means, along with system, tests made are to be verified.
Also, every software quality must be verified.
43
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Key Terms
Data
Data type
DATA OBJECT
DATA STRUCTURE
ABSTRACT DATA TYPE
LINEAR DATA STRUCUTRE
NON LINEAR DATA STRUCTURE
ALGORITHM
ASSEMBLER
COMPILER
Program
Pseudo code
Flowchart
Software Engineering
44
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
End
of
Chapter 3 …!
45