Professional Documents
Culture Documents
Unit Ii Chapter - 2 2.1 Data Structure: Data Structure Organized Data + Operations 2.2 Definitions
Unit Ii Chapter - 2 2.1 Data Structure: Data Structure Organized Data + Operations 2.2 Definitions
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.
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.
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.
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:
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.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.