Ch - 4 Data Structures
Ch - 4 Data Structures
CHAPTER 4
DATA STRUCTURES
Data structure: A data structure is a specialized format for organizing and sorting data.
Types of Data structure: There are two types –
1) Primitive data structure
2) Non - primitive data structure
Primitive data structures: Data structures that are directly operated upon by machine-level instructions.
Primitive data structures are integer, real (floating point), character, pointers, logical data and reference.
Operations on primitive data structures:
The various operations that can be performed on primitive data structures are:
1) Create
2) Destroy
3) Select
4) Update
Create: Create operation is used to create a new data structure.
Ex: int x ;
Destroy: Destroy operation is used to destroy or remove the data structures from the memory space.
Select: Select operation is used by programmers to access the data within data structure.
Update: Update operation is used to change data of data structures.
Non - primitive data structures: Non - primitive data structures are more complex data structures. These data structures are
derived from the primitive data structures.
Non-primitive data structures are Arrays, Lists and Files.
Data structures under Lists are classified as linear and non-linear data structures.
Linear data structure: Linear data structures are a kind of data structure that has homogenous elements.
Linear data structures are Stacks, Queues and Linked Lists.
Non - linear data structure: A non - linear data structure is a data structure in which a data item is connected to several other data
items.
Non - linear data structures are Trees and Graphs.
Tree: A tree is a data structure consisting of nodes organized as a hierarchy.
Binary tree: A binary tree is a tree in which each node has at most two descendants (Childrens).
Depth of a tree (Depth): The depth of the node is the length of the path to its root.
Height of a tree (Height): The height of the node is the length of the longest downward path to a leaf from that node.
Leaf node: A node that do not have any children are called leaf node.
Root node: The topmost node in a tree is called root node.
Graph: A graph is a set of vertices and edges which connect them.
Basic operations can be performed on linear data structure:
1) Traversal
2) Insertion
3) Deletion
4) Searching
5) Sorting
6) Merging
Traversal: The process of accessing each data item exactly once to perform some operation.
Insertion: The process of adding a new data item into the given collection of data items.
Deletion: The process of removing an existing data item from the given collection of data items.
Searching: The process of finding the location of a data item in the given collection of data items.
Sorting: The process of arrangement of data items in ascending or descending order.
Merging: The process of combining the data items of two structures to form a single structure.
Arrays: An array is a collection of homogeneous elements.
One-dimension array: An array with only one row or column is called one - dimensional array.
Syntax: datatype arrayname [ size ];
Ex: int a[ 5 ];
Traversing a Linear array: Traversing is the process of visiting each subscript at least once from the beginning to last element.
Algorithm:
for LOC = LB to UB
PROCESS A[ LOC ]
end of for
Exit
Linear search :
Algorithm:
Step 1: Start
Step 2: LOC = -1
Step 3: for P = 0 to N - 1
if( A [ P ] == ELE)
LOC = P
GOTO step 4
End of if
End of for
Step 4: If( LOC > = 0)
PRINT LOC
else
PRINT “Search is unsuccessful”
Step 5: Exit
Binary search:
Advantages: Binary search is the most efficient method when elements are in the sorted order.
Algorithm:
Step 1: B = 0
Step 2: E = n - 1
Step 3: loc = - 1
Step 4: while ( B < = E )
mid = int( B + E ) / 2
if ( ELE == A[ mid ] ) then
loc = mid
GOTO step 5
[ end if ]
else
if ( ELE < A [ mid ] )
E = mid - 1
else
B = mid + 1
[ end if ]
[ end of while ]
Step 5: if( LOC > = 0 )
PRINT LOC
else
PRINT “Search is unsuccessful”
[ end if ]
Step 6: Exit
Inserting an element to the array: Insertion refers to inserting an element into the array.
Algorithm:
Step 1: for i = n - 1 downto p
m[ i + 1 ] = m[ i ]
[ End of for ]
Step 2: m[ p ] = ele
Step 3: n = n + 1
Step 4: Exit
Deleting an element from the array: Deletion refers to removing an element from the array.
Algorithm:
Step 1:ele = m[ p ]
Step 2: for i = p to n - 1
m[ i ] = m[ i + 1 ]
[ End of for ]
Step 3: n = n - 1
Step 4: Exit
Insertion Sort:
Algorithm:
Step 1 : Start
Step 2 : for i = 1 to n – 1
Step 3 : j = i
Step 4 : while ( j > = 1)
Step 5 : if ( m [ j ] < m [ j – 1] ) then
Step 6 : temp = m [ j ]
Step 7 : m [ j ] = m [ j – 1]
Step 8 : m [ j – 1] = temp
[ end if ]
Step 9 : j - -
[ end of while ]
[ end of for ]
Step 10 : Exit
Two-dimension array: It is a collection of elements and each element is identified by a pair of indices or subscripts.
Syntax: datatype arrayname [ RSize ] [ CSize ];
Ex: int a[ 5 ][ 3 ];
a[0] 1 2 3
a[1] 4 5 6
a[2] 7 8 9
Row major order: The elements of the first-row are stored first in consecutive memory locations and then the elements of the
second-row are stored and so on.
Physical Logical
Address Elements Address
5000 1 a[0][0]
5002 3 a[0][2]
5003 4 a[1][0]
5005 6 a[1][2]
5006 7 a[2][0]
5008 9 a[2][2]
The memory address of any element A[I][J] can be obtained by the formula
LOC(A[I][J]) = Base (A) + W[ n (I - LB) + (J - LB)]
Column major order: The elements of the first-columns are stored first in consecutive memory locations and then the elements of
the second- columns are stored and so on.
Physical Logical
Address Elements Address
5000 1 a[0][0]
5002 7 a[2][0]
5003 2 a[0][1]
5005 8 a[2][1]
5006 3 a[0][2]
5008 9 a[2][2]
The memory address of any element A[I][J] can be obtained by the formula
LOC(A[I][J]) = Base (A) + W[(I - LB) + m (J - LB)]
Stacks: A stack is an ordered collection of items where the addition of new items and the removal of existing items always take
place at the same end.
Stack is a non – primitive data structure which is called LIFO (Last – In First – Out)
The most recently added item is the one that is in position to be removed first. This ordering principle is sometimes called LIFO.
Newer items are near the top, while older items are near the base.
Ex: Stack of books
Application of Stacks:
1) The simplest application of a stack is to reverse a word.
2) Undo and Redo operations
3) Backtracking
4) Quick sort
5) Language processing:
6) Runtime memory management
Circular Queue: A circular queue is a queue in which all nodes are treated as circular such that the last node follows the first node.
Priority Queue: An element can be inserted or removed from any position depending on some Priority.
Dequeue (Double Ended queue): It Is a queue in which insertion and deletion takes place at both the ends.
Operations can be performed on queue:
1) Queue( )
2) enqueue( item )
3) dequeue( )
4) IsEmpty( )
5) size( )
Applications of queues:
1) Simulation
2) Multi-programming platform systems
3) Different type of scheduling algorithm
4) Round robin technique or Algorithm
5) Printer server routines
6) Process management
7) In shared resource management
Linked list : A linked list is a linear collection of data elements called nodes and the linear order is given by means of pointers.
Circular linked lists: If the link field of the last node contains the address of the first node, such a linked list is called as circular
linked list.
Doubly linked lists: Here, each node is points both to the next node and also to the previous node.
Operations can be performed on linked lists:
1) Creating a linked list
2) Traversing a linked list
3) Inserting an item into a linked list
4) Deleting an item from the linked list
5) Searching an item in the linked list
6) Merging two or more linked lists
☯☯☯☯☯