You are on page 1of 45

1

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

 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 complete

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:

Applications that maintain a list of printing jobs waiting at the a


network printer. Here we need to keep one queue which can hold
all print requests from different users

Handling function calls in programs very often restricts the


access only at one end. In such implementation we need to use
stacks. We can keep track of the return address to earlier function
after furnishing/finishing a function call using stacks
 If we intend to store a group of data together in a sequential
manner in computer’s memory, then arrays can be one of the
possible data structures.

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

Initial configuration for two stacks in A[0],, …, A[n-1]

15 15
Data Structures Using C++ by Dr Varsha Patil Oxford University Press © 2012
Initial configuration for m stacks in A [0,
…, n-1]

0 n/m 2[n/m] N-1

B[0] B[1] B[2] B[m-1]


T[0] T[1] T[2] T[m-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.

 An arithmetic expression is made up of operands, operators,


and delimiters.

 When higher level programming language came into existence


one of the major difficulty faced by computer scientists was to
generate machine language instruction, which would properly
evaluate any arithmetic expression.

18
The following operators are written is in
descending order of their precedence:

 Exponentiation ^, Unary +, Unary –, and not ~


 Multiplication * and division /

 Addition + and subtraction –

 <, £ , =, ¹, ³, >

 AND

 OR

19
The Operators and priorities

Data Structures in C++ by Dr. Varsha Patil Oxferd Univercity 20


Polish Notation and Expression Conversion:

 Polish Mathematician Han Lukasiewicz suggested a notation


called pPolish notation, which gives two alternatives to
represent an arithmetic expression, the notation are postfix
and prefix notations
 The fundamental property of Polish notation is that the order
in which the operations are to be performed is determined by the
positions of the operators and operands in the expression.
 Hence the advantage is parenthesis is not required while writing
expressions in pPolish notation

21
The example expression
… in various forms-
infix, prefix and postfix

 The postfix expressions can be evaluated easily hence infix


expression is converted into postfix expression using stack.

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

 A data structure represents a set of data items with a specific


relationship between them
 The success of a software project often depends upon the choices
made in the representation of data and algorithms designed to
process the data
 The proper choice of a data structure can be a key point in the
design of many 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

Flow chart for adding three numbers

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:

 Compile time and


 Execution time

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

 The space complexity = Space needed of 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

 The worst case :


Complexity of the algorithm is the function defined by the
maximum number of steps taken on any instance of size n
 The best case:
Complexity of the algorithm is the function defined by the
minimum number of steps taken on any instance of size n
 The average case:
Complexity of the algorithm is the function defined by an
average number of steps taken on any instance of size n.

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.

A fundamental concept in Software Engineering is the Software


Development Life

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

System Development Phases

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

The whole package is divided into small modules

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

You might also like