Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

UNIT II

CHAPTER – 2
2.1 DATA STRUCTURE:
The organized collection of data is known as data structure.
Data Structure = Organized data + Operations
2.2 DEFINITIONS:
The basic terminologies of data structure are,
Data
Entity
Information
Data type
2.2.1 DATA:
The term data means a value or a set of values.
2.2.2 ENTITY:
An entity is one that has certain attributes and which may be assigned values.
For example,
An employee in an organization is an entity. The possible attributes and their corresponding
values for an entity,
Entity: EMPLOYEE
Attributes: NAME DOB SEX DESIGNATION
Values: ANAND 11/12/1990 MALE TEAM LEADER
2.2.3 INFORMATION:
The term information is meaningful data or processed data and this has been generated as result.
It is used for data with its attributes.

Relation between data and information


2.2.4 DATA TYPE:
A data type is a term which refers to the kind of data that may appear in a computation.
Programming language „C‟ supports a wide variety of data types and a programmer can select
the type of data appropriate to the need of the application.
„C‟ supports the following classes of data type,
 Fundamental (or) Primary Data Type
 Derived (or) Secondary Data Type
 Abstract Data Type (ADT)
Fundamental (or) Primary Data Types:
The fundamental (or) primary data types are,
 Char Holds one character
 Int An integer
 Float A single precision floating point fractional number
 Double A double precision floating point fractional number
Some of these data types may be modified by keywords Short, Long, Signed and Unsigned to
take specific range of data values.
The modifiers Short and Long specify the amount of storage allocation to fundamental data type
they modify.
The modifiers Signed and Unsigned specify whether the most significant bit represents a sign bit
or not.
The data type int should not be used to hold fractions or real numbers.
A variable must be declared by specifying the data type it will store.
Every variable holding data needs to be declared before it is used in a program.
The fundamental data types are also known as Built-in data types.
Derived (or) Secondary Data Types:
The derived (or) secondary data types are,
 Pointer
 Arrays
 Structure
Pointer:
The address of another variable or a memory variable is known as pointer.
Example:
*ptr
Arrays:
Array is a finite ordered set of homogenous data elements, which are stored in adjacent cells in
memory.
Example:
int A[10].
Structure:
A structure is a collection of variables referenced under one name, providing a convenient means
of keeping related information together.
Representation:
We can use combinations of primitive data types to represent abstract data type. Abstract data
type can be created using structures or classes.
For example,
A most commonly used abstract data type stack can be represented using structure data type as
follows,
Struct stack
{
int top;
int items[100];
};
Abstract Data Types (ADT):
An abstract data type is a data structure and collection of functions and procedures which can
operate on that type of data structure. It is also termed as user defined data types.
A useful tool for specifying the logical properties of any data type is the Abstract Data Type (or)
ADT.
A data type is a collection of values and set of operations that can be performed on those values,
such collections and all such operations form a mathematical construct that can be implemented using a
particular data structure.
An abstract data type can be defined as a data type whose properties are specified independent
of any particular implementation.
Stacks and Queues are examples of abstract data type they can be implemented using either
Arrays or Linked list.
ADT concept is a very useful aid in the software design process for computers.
While designing any software program the following are needed:
 Identification of data objects needed by the program i.e., creation of Abstract Data Types –
ADT.
 Specification of algorithms and their design strategies.
Abstract data type is a kind of abstract collection of data elements and functions where we are
not concerned how the associated functions will be implemented.
2.3 CONCEPTS:
A digital computer can manipulate only primitive data, that is, data in terms of 0s and 1s.
Manipulation of user data requires,
o Storage representation of user data
o Retrieval of stored data
o Transformation of user data
2.3.1 Storage representation of user data
User data should be stored in such a way that the computer can understand it.
2.3.2 Retrieval of stored data
Data stored in a computer should be retrieved in such a way that the user can understand it.
2.3.3 Transformation of user data
Various operations which require to be performed on user data so that it can be transformed
from one form to another.
2.4 PRIMITIVE DATA STRUCTURES:
The organized collection of data is known as data structure.
Data Structure = Organized data + Operations
The data structure deals with the study of how the data is organized in the memory.
The fundamental data types are also called primitive data types. Other data structures can be
built from one or more primitive data structure.
Data structures are classified into following two categories.
1. Linear Data Structure
2. Non Linear Data Structure
2.4.1 Linear Data Structure:
In the linear data strictures processing of data items is possible in linear function i.e., data can be
processed one by one sequentially.
The linear data structures are
Arrays
Linked list
Stack
Queues
2.4.1.1 Arrays:
Array is a finite ordered set of homogenous data elements, which are stored in adjacent cells in
memory. The data are represented by a single name identified by a script.
Types of array:
o One dimensional array
o Two dimensional array
o Three dimensional array
One dimensional array:
One dimensional array is a collection of similar data types where a number is referred by one
script.
Example:
int A[i];
Two dimensional array:
A two dimensional array is a collection of similar data types where a number is referred by two
subscripts. The two major dimensions can be termed as row, column.
Example:
int A[i][j];
Three dimensional array:
A three dimensional array is a collection of similar data types where a number is referred by
three subscripts.
The three major dimensions can be termed as row, column and page.
The following specifications be known for a three dimensional array.
Number of rows = x
Number of columns = y
Number of pages = z
Aijk is an element in the ith row, the jth column and the kth page.
Example:
int A[i][j][k];
Advantage:
o Very fast access if index is known.
o Insertion and deletion can be done randomly at any location.
Disadvantage:
o Slow search facility
o Fixed size i.e., static in nature
2.4.1.2 Linked list:
The linked list is a linear collection of data items called as nodes. Node is divided into two fields.
1) INFO Field
2) LINK Field
Types of Linked list:
1. Single Linked List
2. Double Linked List
3. Circular Linked List
Single Linked List:
In a single linked list, each node contains data and a single link which attaches it to the next node
in the list

Double linked list:


In a double linked list, each node contains data and two links one to the previous node and one
to the next node.

Circular linked list:


In a circular linked list the last node points to the starting node.

Advantage:
 Quick insertion
 Quick deletion
Disadvantage:
 Slow search
2.4.1.3 Stack:
It is a linear data structure in which data is inserted and deleted at one end called as top of the
stack that is data is stored and retrieved in Last In First Out (LIFO) order.
Advantage:
 Provides last in first out access
Disadvantage:
 Slow access to other items
2.4.1.4 Queues:
Queue is a linear list of elements in which deletion can take place only at one end called the
FRONT and insertion can take place only at the other end called REAR.

Queues are also called First In First Out (FIFO) or First Come First Serve (FCFS).
Advantage:
 Provides first in first out access
Disadvantage:
 Slow access to other items
2.4.2 Non Linear Data Structures:
Data structure is said to be nonlinear if its elements do not form a sequence, ie, where insertion
and deletion is not possible in a linear fashion.
The nonlinear data structures are
Trees
Graphs
2.4.2.1 Trees:
Tree is a finite set of nodes, that is specially designated node is called root and remaining nodes
are collection of sub trees.

2.4.2.2 Graphs:
Graph is a collection of non-empty, nonlinear set of nodes and a set of edges.
2.4.3 Operations on Data Structures:
 Insertion: Adding the new elements to the existing data structure.
 Deletion: Deleting the element from a data structure.
 Traversing: Accessing each element exactly once.
 Searching: Searches a particular program element in the list of program elements.
 Sorting: Arranging the program elements in either ascending or descending order.
 Merging: Combining the two different program elements into a single sorted file.

2.5 IMPLEMETATION OF DATA STRUCTURES:


The implementation of data structures are in two phases.
i) Storage representation
ii) Algorithmic notation of various operations of the classic data structures
Phase1: Storage Representation
Here we will decide how a data structure can be stored in a computer memory. This storage
representation is based on the use of other data structures.
Phase2: Algorithmic notation of various operations of the classic data structures
A function for manipulating a data structure is expressed in terms of algorithms so that the
details of the operation can be understood easily and reader can implement them with any programming
languages.
It will be preferable to use C (or) C++ programming because of their versatile features and
modern programming approaches. Different statements that will be used in the algorithms are very much
similar to the statements in the C language.
There are some basic constructs in the C language such as,
If (condition) then (statement1) else (statememt2)
While (condition) do
For (initialization; condition; update)
Example:
Algorithm (Name of the operation)
Steps:
1. ……………………….
2. If (condition) then
3. ……………………….
4. Else
5. ……………………….
6. End if
7. ……………………….
8. While (condition) do
9. ………………………..
10. End while
11. ………………………..
12. For (loop condition) do
13. ………………………..
14. End for
15. ………………………..
16. Stop
All statements in the algorithm are uniquely identified by their step numbers, so that each of
them can be easily identified and final step of the algorithm is stop which indicates the end of the
algorithm.
CHAPTER - 3
ARRAYS
3.1 ARRAY:
An array is a finite, ordered and collection of homogeneous data elements which are stored in
adjacent cells in memory.
The data are represented by a single name identified by a script.

Example,
An array of integers to store the age of all students in a class.
int age[40];
An array is known as a linear data structure because all elements of the array are stored in a
linear order.
3.2 TERMINOLOGY:
The array terminologies are,
Size
Type
Base
Index
Range of Indices
Word
Size:
The number of elements in an array is called the size of the array. The size is also called length
(or) dimension.
Type:
The type of an array represents the kind of data type. For example, an array of integers, an array
of float, etc.
Base:
The base of an array is the address of the memory location where the first element of the array is
located.
Index:
All the elements in an array can be referenced by a subscript is known as index.
An index is always an integer value. As each array element is identified by a subscript or index,
an array element is also termed subscripted or indexed variable.
Range of Indices:
Indices of array elements may change from a lower bound (L) to an upper bound (U) and these
bounds are called the boundaries of an array.
A[100], the range of indices is from 1 to 100 (or) 0 to 99.
Word:
Word denotes the size of an element.
3.3 ONE-DIMENSIONAL ARRAY:
If only one subscript/index is required to reference all the elements in an array, then the array is
termed one-dimensional array.
3.3.1 Memory allocation for an array:
Memory representation of array is very simple. An array A[100] is to be stored in a memory.

Where M is the first element in the memory location. If each element requires one word, then the
location for any element A[i] in the array can be obtained as,
Address A[i]=M+(i-1)
An array can be written as A[L…..U], where L and U denotes the lower bound and upper bound
for the index.
If the array is stored starting from the memory location M, and for each element it requires W
number of words, then the address for A[i] will be,
Address A[i]=M+(i-L)xW
The above formula is known as the indexing formula which is used to map the logical
presentation of an array to physical presentation.
3.4 OPERATIONS ON ARRAY:
Various operations can be performed on an array are,
 Traversing
 Insertion
 Deletion
 Searching
 Sorting
 Merging
3.4.1 Traversing:
This operation is used to visit all elements in an array. Visiting means processing each of
elements.
Ex:
Counting number of elements in an array.
Algorithm:
Input: An array A with elements
Output: According to process ().
Data Structures: Array A[L….U].
Step 1: Set i=L
Step 2: While i ≤ U do
Step 3: process (A[i])
Step 4: i = i + 1
Step 5: End while
Step 6: Stop
3.4.2 Insertion:
This operation is used to insert an element into an array that the array is not full.
Algorithm:
Input: KEY is the item; LOCATION is the index of element where it is to be inserted.
Output: Array enriched with KEY.
Data Structures: An array A[L…..U].
Step 1: If A[U] ≠ NULL then
Step 2: Print “Array is full: No insertion possible”.
Step 3: Exit
Step 4: Else
Step 5: i = U
Step 6: While i > LOCATION do
Step 7: A[i] = A[i – 1]
Step 8: i = i – 1
Step 9: Endwhile
Step 10: A[LOCATION] = KEY
Step 11: Endif
Step 12: Stop
3.4.3 Deletion:
This operation is used to delete a particular element from an array. The element will be deleted
by overwriting it with its subsequent element and this subsequent element then is also to be deleted.
Algorithm:
Input: KEY the element to be deleted.
Output: Slimed array without KEY.
Data Structures: An array A[L…U].
Step 1: i = SearchArray (A, KEY)
Step 2: if (i = 0) then
Step 3: print “KEY is not found: No deletion”
Step 4: Exit
Step 5: Else
Step 6: while i < U do
Step 7: A[i] = A[i + 1]
Step 8: i = i + 1
Step 9: End while
Step 10: End if
Step 11: A[U] = NULL
Step 12: U = U – 1
Step 13: Stop
3.4.4 Searching:
This operation is applied to search an element of interest in an array.

Algorithm:
Input: KEY is the element to be searched.
Output: Index of KEY in A or a message on failure.
Data Structures: An array A[L…U]
Step 1: i = L, found = 0, Location = 0
Step 2: while (i ≤ U) and (found = 0) do
Step 3: if compare (A[i], KEY) = TRUE then
Step 4: found = 1
Step 5: Location = i
Step 6: Else
Step 7: i = i + 1
Step 8: End if
Step 9: End while
Step 10: if found = 0 then
Step 11: print “Search is unsuccessful: KEY is not in the array”
Step 12: Else
Step 13: print “Search is successful: KEY is in location”, location.
Step 14: End if
Step 15: return (location)
Step 16: Stop
3.4.5 Sorting:
Sorting is to arrange the given numbers in a specified order either in ascending or descending.
The following algorithm is used to store the elements of an integer array in ascending order.
Algorithm:
Input: An array with integer data.
Output: An array with sorted elements in an order according to Order ().
Data Structures: An array A[L…U]
Step 1: i = U
Step 2: while i ≥ L do
Step 3: j = L
Step 4: while j < i do
Step 5: if order ( A[j], A[j+1]) = FALSE
Step 6: Swap (A[j], A[j+1])
Step 7: End if
Step 8: j = j+1
Step 9: End while
Step 10: i = i – 1
Step 11: End while
Step 12: Stop
3.4.6 Merging:
Merging is used to combine the elements from two different arrays.
Algorithm:
Input: Two arrays A1[L1….U1] , A2[L2….U2]
Output: Resultant array A[L…U] where, L= L1, and U = U1 +(U2 - L2 + 1) when
A2 is appended after A1.
Data Structures: Array structure
Step 1: i1 = L1, i2 = L2
Step 2: L = L1, U = U1 + U2 – L2 + 1
Step 3: i = L
Step 4: Allocate Memory (Size (U – L + 1))
Step 5: while i1 ≤ U1 do
Step 6: A[i] = A1[i1]
Step 7: i = i + 1, i1 = i1 + 1
Step 8: End while
Step 9: while i2 ≤ U2 do
Step 10: A[i] = A[i2]
Step 11: i = i + 1, i2 = i2 + 1
Step 12: End while
Step 13: Stop
3.5 APPLICATIONS OF ARRAY
There are numerous applications of arrays in computation and almost every programming
language includes this data type as a built in data type.
Example:
We want to store all students‟ records in a class.

3.6 MULTI-DIMENSIONAL ARRAYS


An array with more than one subscript used to denote the number (or) dimension is known as
multi dimensional array.
The multi-dimensional arrays are,
1. Two-Dimensional Arrays (2D Array)
2. Three-Dimensional Arrays (3D Array)
3. n-Dimensional Arrays
3.6.1 TWO-DIMENSIONAL ARRAYS (2D ARRAY):
Two dimensional arrays are a collection of homogeneous data elements where the elements are
ordered in a number of rows and columns.
An example of an m x n matrix, where m denotes the number of rows and n denotes number of
columns.

The subscripts of any arbitrary element, say (Aij) represent the ith row and jth column.
Memory representation of a Matrix:
Matrices are also stored in continuous memory locations. There are two conventions of storing
any matrix in the memory.
 Row-major order
 Column-major order
Row Major Order:
In row major order, the elements of a matrix are stored on a row-by-row basis, ie, all the elements
in the first row, then in second row and so on.
Example:
If an array A has 3 x 3 order
The following formula is used to find the address of any element in 2-Dimensional array.
ADDRESS A[i][j] = Base(A) + W [ N(i-LB) + (j-LB) ]
Where,
Base(A) - Base Address of an Array A
W - Data type size
N - Number of columns
LB - Lower bound
i, j - Subscripts
Example:
To find the address of A[1][2],
Where,
i=1
j=2
N=3
ADDRESS A[1][2] = 4000 + 2 [ 3(1-0) + (2-0) ]
= 4000 + 2 [ 3(1) + 2 ]
= 4000 + 2 [3 + 2]
= 4000 + 2 [5]
= 4000 + 10
= 4010
Column Major Order:
In column major order, all elements are stored column by column, ie, all elements in the first
column are stored then in second column, third column and so on.

The following formula is used to find the address of any element in 2-Dimensional array.
ADDRESS A[i][j] = Base(A) + W [ M(j-LB) + (i-LB) ]
Where,
Base(A) - Base Address of an Array A
W - Data type size
M - Number of rows
LB - Lower bound
i, j - Subscripts
Example:
To find the address of A[1][2],
Where, i = 1
j=2
M=3
ADDRESS A[1][2] = 6000 + 2 [ 3(2-0) + (1-0) ]
= 6000 + 2 [ 3(2) + 1 ]
= 6000 + 2 [6 + 1]
= 6000 + 2 [7]
= 6000 + 14
= 6016
3.6.1.1 SPARSE MATRICES:
Matrices with relatively high proportions of zero elements are called sparse matrices.
Let A be a sparse matrix given below.

There exist 6 rows and 6 columns, totally 36 elements. There exists 8 non-zero entries and the
remaining 28 entries are zeros.
The sparse matrix requires an alternate form of representation. The alternate representation is
storing only non-zero elements explicitly.
We might store a matrix as a list of 3 tuple (i, j ,item), we can store the matrix
t = No. of non-zero entries
t = 8
Representation of a sparse matrix
Example:
Convert the following sparse matrix into alternate form

Solution:
In the above sparse matrix, there exists only 5 non-zero entries.
t = Number of non-zero entries
t=5
In the alternate matrix we have 6 rows and 3 columns. Number of rows in alternate matrix is
number of non-zero entries+1.
Number of rows = Number of non-zero entries + 1
=5+1
=6

In the first row, the elements 5, 4 and 5 indicates no. of rows, no. of columns and no. of non-zero
entries is the given sparse matrix.
Types of sparse matrix:

Applications of sparse matrix:


 It is used in matrix manipulation.
 It is used to represent stacks, queues in memory.
 It is used in representation of polynomials.
3.6.2 THREE-DIMENSIONAL ARRAY (3D ARRAY):
The three major dimensions can be termed as row, column and page.

The following specifications be known for a three dimensional array.


Number of rows = x
Number of columns = y
Number of pages = z
Aijk is an element in the ith row, the jth column and the kth page.
Storing a 3D array means, storing the pages one by one. Again storing a page is the same as
storing a 2D array.
If the elements in a page are stored in row major order then we term that 3D array also in row
major order.
3.6.3 n-DIMENSIONAL ARRAY:
We need n indices, i1, i2,……, in to identify an element in n dimensional array.
Let xj be the number of elements in the jth dimension and the range of index for ij varies between lj
and uj where 1 ≤ j ≤ n. So the total number of elements in the array is

Storing this n dimensional array in memory, any element can be referenced using the following
formula:
3.7 POINTER ARRAYS:
Some integer values, real number, string of characters are simple elements that arrays may
contain. In addition to these, another kind of array which is used to store the address of some variables or
the address of other arrays.

The address of a memory variable or array is known as pointer and an array containing pointers
as its elements is known as pointer array.
Example:
We want to store the marks of all students of the BCA department for a year. There are six
classes, BCA1, BCA2, BCA3, BCA4, BCA5 and BCA6 and for each class, there are at most 5 subjects and
50 students in each class.
We should maintain an array of size 6 x 5 x 50 = 1500 to store the marks. We will use pointer
arrays to keep track of the marks of the ith year student in a subject j (Sij) and the starting location where
the marks of subject BCAi begin.
We have maintained three arrays, namely classes, subjects and marks having sizes 6, 50 and 1500
respectively.
The marks array stores the marks obtained by the various students in different subjects. The
subjects array stores the starting location for the marks of different subjects.
The classes array points the starting location for various subjects under a class.
CHAPTER - 4
STACKS
4.1 STACK:
A stack is an ordered collection of homogeneous data elements where the insertion and deletion
operations take place at only one end called as Top of the Stack, that is data is stored and retrieved in
Last In First Out (LIFO) order.
The insertion and deletion operations in the case of a stack are specially termed as PUSH and
POP respectively.
The position of the stack where these operations are performed is known as the top of the stack.
An element in a stack is termed as ITEM. The maximum number of elements that a stack can be
accommodate is termed SIZE.

Examples of stacks:
 Trains in a railway station
 Goods in a cargo
 Plates on a tray
4.2 REPRESENTATION OF STACK:
There are two main ways are used to represent a stack.
 Using a one-dimensional array
 Using linked list
4.2.1 Array representation of stacks:
First we have to allocate a memory block of sufficient size to accommodate the full capacity of
the stack then starting from the first location of the memory block, the items of the stack can be stored in a
sequential fashion.
Item i denotes the ith item in the stack, l and u denote the index range of the array in use.
Top is a pointer to point the position of the array with this representation, the following two
ways can be started.
EMPTY: Top < L
FULL: Top ≥ U
4.2.2 Linked List representation of stacks:
Array representation of stack is very easy and convenient but it allows the representation of only
fixed sized stacks. In several applications, the size of the stack may vary during program execution. A
solution to this problem is to represent a stack using a linked list.
A single linked list structure is sufficient to represents any stack. The DATA field is for the ITEM
and LINK field is to point the next item.

In the linked list representation, the first node on the list is the current item that is the item at the
top of the stack and the last node is the node containing the bottom most item.
PUSH operation will add a new node in the front and POP operation will remove a node from
the front of the list.
The SIZE of the stack is not important here, because this representation allows dynamic stacks
instead of static stacks.
4.3 OERATIONS ON STACKS:
The basic operations required to manipulate a stack are,
PUSH: To insert an item into a stack
POP: To remove an item from a stack
STATUS: To know the present state of a stack

4.3.1 Operations using Array:


Algorithm for PUSH:
Step 1: If Top ≥ SIZE then
Step 2: Print “Stack is FULL”
Step 3: Else
Step 4: Top = Top + 1
Step 5: A[Top] = ITEM
Step 6: End if
Step 7: Stop
Algorithm for POP:
Step 1: If Top< 1 then
Step 2: Print “Stack is empty”
Step 3: Else
Step 4: ITEM = A[Top]
Step 5: Top = Top - 1
Step 6: End if
Step 7: Stop
Algorithm for STATUS:
Step 1: If Top< 1 then
Step 2: Print “Stack is empty”
Step 3: Else
Step 4: If Top ≥ SIZE then
Step 5: Print “Stack is FULL”
Step 6: Else
Step 7: Print “The element at Top is”, A[Top]
Step 8: free = (SIZE – Top) / SIZE * 100
Step 9: Print “Percentage of free stack is”, free
Step 10: End if
Step 11: End if
Step 12: Stop
4.3.2 Operations using Linked List:
Algorithm for PUSH:
Step 1: new = GetNode (NODE)
Step 2: newDATA = ITEM
Step 3: newLINK = Top
Step 4: Top = new
Step 5: STACK_HEADLINK = Top
Step 6: Stop
Algorithm for POP:
Step 1: If Top = NULL
Step 2: Print “Stack is empty:
Step 3: Exit
Step 4: Else
Step 5: ptr = TopLINK
Step 6: ITEM = TopDATA
Step 7: STACK_HEADLINK = ptr
Step 8: Top = ptr
Step 9: End if
Step 10: Stop
Algorithm for STATUS:
Step 1: ptr = STACK_HEADLINK
Step 2: If(ptr = NULL) then
Step 3: Print “Stack is empty”
Step 4: Else
Step 5: nodecount = 0
Step 6: While(ptr ≠ NULL) do
Step 7: nodecount = nodecount + 1
Step 8: ptr = ptrLINK
Step 9: End while
Step 10: Print “The item at the front is”, TopDATA, “Stack contains”,
nodecount, “Number of items”.
Step 11: End if
Step 12: Stop
4.4 APPLICATIONS OF STACKS:
The applications of stacks are,
 Recursion
 Parenthesis matching
 Evaluation of Arithmetic Expression
 Postfix expression evaluation
 Infix to postfix conversion
 Infix to prefix conversion
4.4.1 Parenthesis matching:
Stacks are most useful for parenthesis matching. The purpose of this is to check the matching of
parenthesis in an expression.
Example:
((1 + 2) * 3) is a valid expression.
(1 + 2) * 3) is not valid.
Stack is useful to match parenthesis in an operation, whenever left parenthesis is encountered,
PUSH it to STACK. Once a right parenthesis is encountered, POP one left parenthesis, at the end of
arithmetic expression, if stack is empty, then expression is valid, else it is not valid.

At the end stack is empty, hence the expression is valid.


4.4.2 Evaluation of Arithmetic Expression:
An arithmetic expression consists of operands and operators.
Operands are variables or constants and operators are of various types such as arithmetic unary
and binary operators.
Arithmetic expression is classified into three types depending on how the operator is placed
among the operands.
 Infix notation
 Prefix notation
 Postfix notation
Infix Notation:
The conventional way of writing an expression is called infix, if the operator is placed between its
two operands.
The notation is,
<OPERAND> <OPERATOR> <OPERAND>
For example,
A + B, C – D, E * F, G/H
This is called infix because the operator comes in between the operands.
Prefix Notation:
The operator symbol is placed before its two operands is called prefix notation. The notation is,
<OPERATOR> <OPERAND> <OPERAND>
For example,
+AB, -CD, *EF, /GH
The prefix notation is introduced by the polish mathematician Jan Lukasiewicz, hence it is also
termed as polish notation.
Postfix Notation:
In postfix notation, operator symbol is placed after its operands.
The notation is,
<OPERAND> <OPERAND> <OPERATOR>
For example,
AB+, CD-, EF*, GH/
Postfix notation is also called as suffix notation and also called as reverse polish notation.
Converting Infix expression into Prefix:
a) (A + B) + C = [+AB]+C
= ++ABC
b) A + (B * C) = A+[*BC]
= +A*BC
c) (M - N) / (O + P) = (-MN)/(+OP)
= /(-MN)(+OP)
= /-MN+OP
d) (M + N) * O = *(M + N)O
= *+MNO
Converting Infix expression into Postfix:
a) (A + B) * C = [AB+]*C
= AB+C*
b) (M - N) / (O + P) = (MN-)/(OP+)
= /(-MN)(+OP)
= MN-OP+/
Converting Prefix expression into Infix:
a) + - PQR = +(P – Q)R
= (P – Q) + R
b) + I – JK = + I (J – K)
= I + (J – K)
Converting Postfix expression into Infix:
a) MN * Q - = (M * N) Q - = (M * N) - Q
b) MN * - = (M * N) -
= - (M * N)
4.4.3 Algorithm for Infix to Postfix:
Step 1: Top=0, PUSH ( „(‟ )
Step 2: While (Top > 0) do
Step 3: item = E.ReadSymbol( )
Step 4: x = POP ( )
Step 5: Case: item = operand
Step 6: PUSH(x)
Step 7: Output(item)
Step 8: Case: item = „)‟
Step 9: While x ≠ „(„ do
Step 10: Output(x)
Step 11: x = POP ( )
Step 12: End while
Step 13: Case: ISP(x) ≥ ICP(item) // ISP – In Stack Priority
Step 14: While (ISP(x) ≥ ICP(item)) do // ICP – In Coming Priority
Step 15: Output(x)
Step 16: x = POP ( )
Step 17: End while
Step 18: PUSH (x)
Step 19: PUSH (item)
Step 20: Case: ISP(x) < ICP(item)
Step 21: PUSH (x)
Step 22: PUSH (item)
Step 23: Otherwise: Print “Invalid expression”
Step 24: End while
Step 25: Stop
Example:
Convert the infix form (((A+B)^C) – ((D*E)/F)) into postfix form.
Solution:

4.4.4 Evaluation of Postfix expression:


If P is an arithmetic expression written in postfix notation, the following algorithm evaluates
expression P using stack.
Algorithm:
Step 1: Append a special delimiter „#‟ at the end of expression
Step 2: item = E.ReadSymbol ( )
Step 3: While (item ≠ „#‟) do
Step 4: If (item = operand) then
Step 5: PUSH (item)
Step 6: Else
Step 7: op = item // op is operator
Step 8: y = POP ( ) // Rightmost operand
Step 9: x = POP ( ) // Leftmost operand
Step 10: t = x op y
Step 11: PUSH (t)
Step 12: End if
Step 13: item = E.ReadSymbol ( )
Step 14: End while
Step 15: value = POP ( )
Step 16: Return (value)
Step 17: Stop
Example:
Input: A B C * D / + #, A = 2, B = 3, C = 4, D = 6

4.4.5 Recursion:
In recursion, stack is used to store the current value and return address of the function call.
At each recursive procedural call, the stack is pushed to save the necessary values; the stack is
poped to restore the saved values of the preceding level.
Implementation of Recursion:
Recursion is the process of calling a function itself (or) function calls another function.
Example:
Calculation of the factorial value for an integer n.
n! = n x (n-1) x (n-2) x …. x 3 x 2 x 1
n! = n x (n-1)!
These two types of definitions are expressed as,
i) Iterative
ii) Recursive
Iterative definition of factorial
Algorithm:
Input: an integer number N
Output: the factorial value of N, i.e. N!
Step1: fact = 1
Step2: for i = 1 to N do
Step3: fact = i * fact
Step4: end for
Step5: return (fact)
Step6: stop
Recursive definition of factorial
Algorithm:
Input: an integer number N
Output: the factorial value of N, that is N!
Step1: if (N = 0) then
Step2: fact = 1
Step3: else
Step4: fact = N * factorial_R (N – 1)
Step5: end if
Step6: return (fact)
Step7: stop
Stack can be used to implement recursion. The programming languages such as C, Pascal which
have dynamic memory management mechanism, can directly accept the recursive definitions of
procedures. The compilers of these programming languages are responsible to produce an object code for
execution using a stack called runtime stack.
FORTRAN, COBOL, BASIC are the programming languages, which do not have a dynamic
memory management mechanism, it is user responsibility to define and maintain the stack to implement
recursive definition of the procedure.
Example:
The calculation of 5! using recursive definition.

Here, it is required to push the intermediate calculations till the terminal condition is required.
Here, steps 1 to 6 are PUSH operations and steps 7 to 11 subsequent POP operations will evaluate
the value of intermediate calculations until stack is empty.
4.5 Factorial calculation:
The factorial for an integer N can be defined recursively as follows.
Algorithm: Factorial (N)
Step1: If (N = 0) then
Step2: fact = 1
Step3: Else
Step4: fact = N * factorial (N – 1)
Step5: End if
Step6: Return (fact)
Step7: Stop
We require two stacks,
i) Storing the parameter N (PARAM)
ii) To hold the return address (ADDR)
We assume PUSH (X, Y) operation to push the items X and Y into the stack PARAM and ADDR
respectively.
Algorithm: Factorial with stack
Input: an integer N and MAIN, the address of the main routine.
Output: Factorial value of N (N!)
Data structure: Array representation of stack
Step1: val = N, top = 0, addr = step15
Step2: PUSH (val, addr)
Step3: val = val – 1, addr = step11
Step4: If (val = 0) then
Step5: fact = 1
Step6: Goto step12
Step7: Else
Step8: PUSH (val, addr)
Step9: Goto step3
Step10: End if
Step11: fact = val * fact
Step12: val = POP_PARAM( ), addr = POP_ADDR( )
Step13: Goto addr
Step14: Return (fact)
Step15: Stop
QUESTIONS
CHAPTER - 3
1) Write a short note on array.
2) Explain about one dimensional array.
3) Explain the operations on array in detail.
4) Describe multi-dimensional array.
5) Explain in detail about two dimensional array with example.
6) Explain sparse matrix.
7) Explain 3 dimensional array and n-dimensional array.
8) Explain pointer arrays.
CHAPTER – 4
1) Explain the representation of stack in detail.
2) Describe the operations of stack.
3) Explain the applications of stack.
4) Explain how to evaluate the arithmetic expression.
5) Write the algorithm to convert infix expression into postfix expression with example.
6) Explain how to evaluate the postfix expression.
7) Write a note on recursion.
8) Write the algorithm for factorial calculation.

You might also like