1Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
3. Stack
2Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Objectives
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
3Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Introduction To Programming
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
4Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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.
5Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Arrays 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];
6Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
7Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
The Stack Full Condition (Stack Capacity =3)
8Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
The Pop Operation
9Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
The Empty Stack
10Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
The Get Top Operation
11Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
REPRESENTATION OF STACK S USING SEQUENTIAL ORGANIZATION
(ARRAYS)
12Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
13Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Stack using Array
……….. C B A
p
14Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
15Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
MULTIPLE STACKS
Initial configuration for two stacks in A[0],, …, A[n-1]
………..1 2 3 B A
Stack2Stack 1
A
Top1 =2 Top2=n-2
0 1 2 3 n-3 n-2 n-1
15
16Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Initial configuration for m stacks in A [0, …, n-1]
B[0]T[0]
B[1]T[1]
B[2]T[2]
B[m-1]T[m-1]
0 n/m 2[n/m] N-1
A
17
Applications Of Stack
Convert infix expression to postfix and prefixexpressions
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
optimizationsand in games)
18
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.
19
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
20
The Operators and priorities
Oxferd Univercity Data Structures in C++ by Dr. Varsha Patil
21
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
22Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
…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.
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
Repetition Construct
23
24Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
25Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Flow ChartsA very effective tool to show the logic flow of a programA flow chart is a pictorial representation of an
algorithmIt hides all of the details of an algorithm by giving the picture; It shows how the algorithm flows from beginning to end
26Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Flow Charts
Flow chart for adding three numbers
27Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Analysis of Algorithms
Complexity of Algorithms Space Complexity Time Complexity Computing Time Complexity of Algorithm Big-O Notation
28Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
29Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
30Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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 theprogram spacedata space andstack space
31Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
32Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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.
33Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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 programTime required by each statement depends on
Time required for executing it once. Number of times the statement is executed.
34Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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)
35Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Software Engineering
Analysis PhaseDesign PhaseImplementation PhaseTesting PhaseVerification
36Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Software Engineering
Analysis PhaseDesign PhaseImplementation PhaseTesting PhaseVerification
System Development Phases
37Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Analysis Phase
Define the UserDefine the NeedsDesign PhaseDefine the Methods
38Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Design Phase
Modularity: The design phase uses a very well-established principle called modularity
The whole package is divided into small modules
Tools
39Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Implementation Phase
Tools
flowchartPseudo CodeCoding
40Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Testing Phase
Testing PhaseOnce the programs have been written, they must be tested. There are two types of testing:
Black box and White box
41Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
VerificationProgram 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.
42Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
43Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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.
44Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
45
End of Chapter 3 …!