0% found this document useful (0 votes)
9 views

Ch - 4 Data Structures

Computer science -c++ 2nd puc

Uploaded by

mariyamnafeesa7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Ch - 4 Data Structures

Computer science -c++ 2nd puc

Uploaded by

mariyamnafeesa7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

-1-

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.

Shridhar, MCA, M.Sc, Lecturer in Computer Science, S.S.P.U.College, Subrahmanya


-2-

Types of Arrays: There are three types of arrays:


1) One (Single) - dimensional Array
2) Two - dimensional Array
3) Multi - dimensional array
Applications of Arrays:
1) Arrays are used to implement other data structures such as stacks, queues, heaps etc.
2) Arrays are used to implement mathematical vectors and matrices
3) Many databases include one-dimensional arrays whose elements are records.

One-dimension array: An array with only one row or column is called one - dimensional array.
Syntax: datatype arrayname [ size ];
Ex: int a[ 5 ];

Calculating the length of Array:


The length of an array can be calculated by L = UB - LB + 1
Here, UB is the largest index and LB is the smallest index of an array.
Ex:
If an array A has values 10, 20, 30, 40, 50, 60 stored in locations 0, 1, 2, 3, 4, 5 then UB = 5 and LB = 0
Size of the array L = 5 - 0 + 1 = 6

We can calculate the address any element of A by the formula:


LOC( A[ P ] ) = Base( A ) + W( P – LB )
Ex:
Suppose if a string S is used to store a string ABCDE in it with starting address at 1000, one can find the address of fourth element
as follows:
Now the address of element S[3] can be calculated as follows:
Address( S [ 3 ] ) = Base( S ) + W( P – LB ) Here W = 1 for characters
= 1000 + 1(3 - 0)
= 1003
Memory representation of one - dimensional array:
If an array has the N elements, the address of first element is identified as a[0], the address of second element is identified as a[1]
and so on.
An array of 5 elements is represented as follows:

a[0] a[1] a[2] a[3] a[4]

Basic operations can be performed on (one – dimensional) arrays:


1) Traversing
2) Searching
3) Sorting
4) Insertion
5) Deletion
6) Merging
Traversing: Accessing each element of the array exactly once to do some operation.
Searching: Finding the location of an element in the array.
Sorting: Arranging the elements of the array in some order.
Insertion: Inserting an element into the array.
Deletion: Removing an element from the array.
Merging: Combining one or more arrays to form a single array.

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

Shridhar, MCA, M.Sc, Lecturer in Computer Science, S.S.P.U.College, Subrahmanya


-3-

Searching: Finding the location of the array.


Types of searching: There are two types –
1) Linear search
2) Binary search

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

Advantages: Linear search is the simplest method.


Disadvantages: It is time consuming.

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

Shridhar, MCA, M.Sc, Lecturer in Computer Science, S.S.P.U.College, Subrahmanya


-4-

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

Sorting: Sorting refers to job of ordering the elements.


Different types of sorting techniques are –
1) Insertion sort
2) Bubble sort
3) Selection sort
4) Quick sort

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 ];

Shridhar, MCA, M.Sc, Lecturer in Computer Science, S.S.P.U.College, Subrahmanya


-5-

Memory representation of Two - dimensional array:


Let ‘a’ is a two-dimensional array of 3 rows and 3 columns

[0] [1] [2]

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]

5001 2 a[0][1] 1st row

5002 3 a[0][2]

5003 4 a[1][0]

5004 5 a[1][1] 2nd row

5005 6 a[1][2]

5006 7 a[2][0]

5007 8 a[2][1] 3rd row

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]

5001 4 a[1][0] 1st column

5002 7 a[2][0]

5003 2 a[0][1]

5004 5 a[1][1] 2nd column

5005 8 a[2][1]

5006 3 a[0][2]

5007 6 a[1][2] 3rd column

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)

Shridhar, MCA, M.Sc, Lecturer in Computer Science, S.S.P.U.College, Subrahmanya


-6-

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

Basic operations can be performed on stacks:


1) stack( )
2) push( item )
3) pop( )
4) peek( )
5) isEmpty( )
6) size( )
stack( ) : creates a new stack that is empty.
push( item ) : adds a new item to the top of the stack.
pop( ) : removes the top item from the stack.
peek( ) : returns the top item from the stack but does not remove it.
isEmpty( ) : tests whether the stack is empty.
size( ) : returns the number of items in the stack.

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

Algorithm for PUSH Operation: PUSH(STACK ,TOP, SIZE, ITEM)

Step 1 : If TOP = N then [ check overflow ]


PRINT “Stack is full”
Exit
End of If
Step 2 : TOP = TOP + 1 [ Increment the TOP ]
Step 3 : STACK[ TOP ] = ITEM [ Insert the ITEM ]
Step 4 : Return

Algorithm for POP Operation: POP(STACK , TOP, ITEM)

Step 1 : If TOP = 0 then [ Check underflow ]


PRINT “Stack is empty”
Exit
End of If
Step 2 : ITEM = STACK[ TOP ] [ Copy the top element ]
Step 3 : TOP = TOP - 1 [ Decrement the top ]
Step 4 : Return
Queues : A queue is an ordered collection of items where the addition of new items and the removal of existing items always take
place at different ends.
Queue is also called FIFO (First – In First – Out), here item is inserted at one end called the “rear,” and an existing item is
removed at the other end, called the “front.”
Types of queues : There are four types:
1) Simple Queue
2) Circular Queue
3) Priority Queue
4) Dequeue (Double Ended queue)
Simple Queue: Here insertion occurs at the rear end of the list, and deletion occurs at the front end of the list.

Shridhar, MCA, M.Sc, Lecturer in Computer Science, S.S.P.U.College, Subrahmanya


-7-

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( )

queue( ) : creates a new queue that is empty.


enqueue( item ) : adds a new item to the rear of the queue.
dequeue( ) : removes the front item from the queue.
IsEmpty( ) : tests whether the queue is empty.
size( ) : returns the number of items in the queue.

Algorithm to INSERT data element to the queue:

Step 1 : If REAR = N Then [Check for overflow]


PRINT “Overflow”
Exit
Step 2 : If FRONT = NULL Then [Check whether QUEUE is empty]
Step 3 : FRONT = 0
REAR = 0
Else
REAR = REAR + 1 [Increment REAR Pointer]
QUEUE [ REAR ] = ITEM [Copy ITEM to REAR position]
Step 4 : Return

Algorithm to DELETE data element from the queue:

Step 1 : If FRONT = NULL Then [Check whether QUEUE is empty]


PRINT “Underfiow”
Exit
Step 2 : ITEM = QUEUE [ FRONT ]
Step 3 : If FRONT = REAR Then [If QUEUE has only one element ]
FRONT = 0
REAR = 0
Else
FRONT = FRONT + 1 [Increment FRONT pointer]
Step 4 : Return

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.

Types of linked lists: There are three types of linked lists -


1) Singly linked list ( SLL )
2) Doubly linked list ( DLL )
3) Circular linked list ( CLL )
Single linked list: A singly linked list contains two fields in each node - the data field and link field.

Shridhar, MCA, M.Sc, Lecturer in Computer Science, S.S.P.U.College, Subrahmanya


-8-

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

Note: For the remaining topics, please refer text book.

☯☯☯☯☯

Shridhar, MCA, M.Sc, Lecturer in Computer Science, S.S.P.U.College, Subrahmanya

You might also like