8-Data Structures For II-PUC
8-Data Structures For II-PUC
U College, Chickballpur
Data Structure:
Data Structure is a collection of organised data items which improve the efficiency of storage and
program
The various operations that can be performed on primitive data structures are:
Create: Create operation is used to create a new data structure. This operation reserves
memory space for the program elements. It can be carried out at compile time
and run-time.
For example, 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. This operation updates or alters data.
Update: Update operation is used to change data of data structures.
For example, int x = 2; Here, 2 is assigned to x.
Again, x = 4; 4 is reassigned to x.
The value of x now is 4 because 2 is automatically replaced by 4, i.e. updated.
Insertion: The process of adding a new data item into the given collection of data
items is called insertion.
Deletion: The process of removing an existing data item from the given collection of
data items is called deletion.
Searching: The process of finding the location of a data item in the given collection
of data items is called as searching.
Merging: The process of combining the data items of two structures to form a single
structure is called merging.
Arrays
An array is a collection of homogeneous elements with same name and same type of
elements
Types of Arrays:
There are three types of arrays:
One-dimensional Array
Two-dimensional Array
Multi-dimensional array
One-dimension Array
An array is a collection of homogeneous elements with same name and same type of
elements, where each element is accessed by single index value.
Syntax data type array_name[size];
int a[5];
Here, size specifies the number of elements in the array and the index (subscript)
values ranges from 0 to n-1.
A[ 0 ] A[ 1 ] A[ 2 ] A[ 3 ] A[ 4 ]
LB UB
Using base address the computer calculates the address of any element of A by the
following formula:
LOC (A[POS]) = Base (A) + w (POS-LB)
=2003
=2006
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.
End of for
Step 2: Exit
Searching an element
It refers to finding the location of the element in a linear array.
The most common methods are linear search and binary search.
Linear Search
The element to be searched is compared with each element of the array one by one from
the beginning till end of the array.
Since searching is one after the other it is also called as sequential search or linear search.
Algorithm: A is the name of the array with N elements. ELE is the element to be searched.
This algorithm finds the location LOC where the search ELE element is
stored.
Step 1: LOC = -1
Step 2: for I = 0 to N-1
if( A[I] == ELE)
LOC =I 50 20 40 10 30
GOTO 3
End of if A[ 0 ] A[ 1 ] A[ 2 ] A[ 3 ] A[ 4 ]
End of for
Step 3: if(LOC >= 0) I
PRINT LOC LOC
else
PRINT “Search is unsuccessful”
Step 4: Exit
Binary Search:
Insertion an element
Insertion refers to inserting an element into the array.
A new element can be inserted provided the array is large enough to accommodate the
new element.
When an element is to be inserted into a particular position, all the elements from the
asked position to the last element should be shifted into the higher order positions.
But the elements should be shifted in reverse order.
Algorithm: A is the array with N elements. ITEM is the element to be inserted in the
position P.
Step 1: for I = N-1 down to Pos 10 30 40 50 60
A[I+1] = A[I]
A[ 0 ] A[ 1 ] A[ 2 ] A[ 3 ] A[ 4 ]
End of for
Step 2: A[Pos] = ITEM
Step 3: N = N+1 P
Step 4: Exit
Insertion Sort:
The first element of the array is assumed to be in the correct position. The next
element is considered as the key element and compared with the elements before the key
element and is inserted in its correct position.
Algorithm: Let A be an array with N unsorted elements. The following algorithm sorts the
elements in order.
Two-dimensional arrays
A two dimensional array is a collection of homogeneous elements and each element is
identified by a pair of indices called as subscripts.
A two-dimensional array has m rows and n columns then the elements are accessed
using two indices I and J
The number of rows and columns in a matrix is called as the order of the matrix and
denoted as m x n.
The number of elements can be obtained by multiplying number of rows and number of
columns.
[0] [1] [2] [3]
A[0] I n t h e a b o v e fi g u r
3 A[1] x4 = 12-elements.
A[2]
Row-major order
Let A be the array of order m x n. In row-major order, all the first-row elements are
stored in sequential memory locations and then all the second-row elements are stored and
so on.
Base(A) is the address of the first element. 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)]
Here, W is the number of words per memory location.
Example: Consider the array of order 3 x 3.
[0] [1] [2]
A[0]
A[1]
A[2]
Applications of arrays
Arrays are used to implement other data structures such as heaps, hash tables,
queues, stacks and strings etc.
Arrays are used to implement mathematical vectors and matrices.
Many databases include one-dimensional arrays whose elements are records.
Advantages of arrays
It is used to represent multiple data items of same type by using only single
name.
It can be used to implement other data structures like linked lists, stacks, queues,
trees, graphs etc.
Two-dimensional arrays are used to represent matrices.
Disadvantages of arrays
Array is static structure. It means that array is of fixed size. The memory which is
allocated to array cannot be increased or reduced.
Since array is of fixed size, if we allocate more memory than requirement then
the memory space will be wasted. If we allocate less memory than requirement,
then it will create problem.
The elements of array are stored in consecutive memory locations. So insertions
and deletions are very difficult and time consuming.
Stack
A stack is an ordered collection of items where the insertion and removal of an item
always take place at only one end.
This end is commonly referred to as the “top”.
This ordering principle is sometimes called LIFO, last-in first-out.
B O O K S
TOP
10 2O 3O
S
Storage of elements in this direction TOP K
TOP O
O
B
stack(): Creates a new stack that is empty. It needs no parameters and returns an
empty stack.
push(item): Adds a new item to the top of the stack. It needs the item and returns
nothing.
pop(): Removes the top item from the stack. It needs no parameters and returns
the item. The stack is modified.
peek(): Returns the top item from the stack but does not remove it. It needs no
parameters. The stack is not modified.
isEmpty(): Tests whether the stack is empty. It needs no parameters and returns a
Boolean value.
size(): Returns the number of items on the stack. It needs no parameters and
returns an integer.
Application of Stacks
2. If the stack is empty or contains a left parenthesis on top, push the incoming
operator onto the stack.
4. If the incoming symbol is a right parenthesis, pop the stack and print the operators
until you see a left parenthesis. Discard the pair of parentheses.
5. If the incoming symbol has higher precedence than the top of the stack, push it on
the stack.
6. If the incoming symbol has equal precedence with the top of the stack, use
association. If the association is left to right, pop and print the top of the stack and
then push the incoming operator. If the association is right to left, push the incoming
operator.
7. If the incoming symbol has lower precedence than the symbol on the top of the
stack, pop the stack and print the top operator. Then test the incoming operator
against the new top of stack.
8. At the end of the expression, pop and print all operators on the stack. (No
parentheses should remain.)
Example
A- B + C becomes A B - C +
1 A A
2 - - A
3 B - AB
4 + + AB-
5 C + AB-C
6 AB-C+
Queues
A queue is an ordered collection of items where insertion of items and removal of
items always take place at different ends.
In a queue, two operations are allowed - enqueue and dequeue.
Enqueueis the process of inserting anitem into the queue.
Dequeueis the process of removing an item from the queue.
Queue is also called as FIFO list, i.e. First-In-First-Out.
Insertions are made at the end called the rear and deletions are made at the end
called front.
Types of queues
Queue can be of four types:
Simple Queue
Circular Queue
Priority Queue
Dequeue (Double Ended queue)
Simple Queue:In Simple queue, insertion occurs at the rear end of the list, and deletion
occurs at the front end of the list.
FRONT REAR
FRONT = 1
A B C D E
REAR = 5
0 1 2 3 4 5 6 7 8
Circular Queue: A circular queue is a queue in which all items are treated as circular such
that the last item follows the first item.
FRONT
REAR
FRONT = 1
F D E A B C
REAR = 5
0 1 2 3 4 5 6 7 8
Priority Queue: A priority queue is a queue that contains items that have some preset
priority. An element can be inserted or removed from any position depending on some
priority.
It is a queue in which insertion and deletion takes place at both the ends.
Insertion Insertion
Deletion Deletion
Operations on queue
Queue() creates a new queue that is empty. It needs no parameters and returns
an empty queue.
Enqueue(item) adds a new item to the rear of the queue. It needs the item and
returns nothing.
Dequeue() removes the front item from the queue. It needs no parameters and
returns the item. The queue is modified.
isEmpty() tests to see whether the queue is empty. It needs no parameters and
returns a boolean value.
size() returns the number of items in the queue. It needs no parameters and
returns an integer.
Memory representation of a queue using arrays
Queue is represented in memory using linear array.
Let QUEUE is a linear queue.
Two pointer variables called FRONT and REAR are maintained.
FRONT contains the location of the element to be removed
REAR contains location of the last element inserted.
The condition FRONT = =NULL indicates that the queue is empty
REAR == N -1 indicates that the queue is full
10 2O 3O 40
Deletion Insertion
FRONT = 1
A B C D E
REAR = 5
0 1 2 3 4 5 6 7 8
Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur
Applications of queues
Simulation
Various features of operating system
Multi-programming platform systems
Different type of scheduling algorithm
Round robin technique or Algorithm
Printer server routines
Various applications software is also based on queue data structure
Operating systems often maintain a queue of processes that are ready to execute or
that are waiting for a particular event to occur.
LINKED LISTS
Disadvantage of using arrays is that arrays are static structures and therefore cannot be
easily extended or reduced to fit the data set.
Linked Lists addresses these limitations of arrays. The linked list uses dynamic memory
allocations.
A linked list is a linear collection of data elements called nodes and the linear order is
given by means of pointers.
Each node contains two fields: the data field and link field.
Data Link
The data field contains the information and the link field contains the address of the
next node in the list.
Here is a linked list with 4 with nodes. A pointer START gives the location of the first
node.
This pointer is also called as HEAD.
The link field of the last node contains NULL.
START
50 40 70 20 NULL
START
50 40 70 20 NULL
STAR
T
Header node
Each node is points both to the next node and also to the previous node.
In doubly linked list each node contains three parts – FORW, BACK and INFO.
INFO
Node *link;
}
Node *node1, *node2;
The above declaration creates two variable structures, node1 and node2.
Each structure will be taken as node.
Algorithm Here, START Pointer contains the address of the first node.
p Pointer temporarily used to visit all the nodes from the
beginning to the end.
Algorithm (inst-beg):This algorithm inserts a node at the beginning of the linked list.
Step 1. p new Node
Step 2. data(p) num
Step 3. link(p) START
Step 4. START p
Step 5. Return
Step 1. START 50 40 70
Step 4. if (pos = 1)
Step 1. START
Step 2. count 0
p1 START
Step 3. while(p1!=NULL)
count = count +1
p1link(p1)
while end
Step 4. if(pos = =1)
CALL DELE-BEG()
else
if(pos = =count)
CALL DELE-END()
else
if(pos<= count)
p2 START
for i = 1 to pos
p1 p2
p2 link(p2)
for end
PRINT data(p2)
link(p1) link(p2)
free(p2)
if end
Step 5. RETURN
Pros 1 Uses memory efficiently as contiguous memory is not required for allocating
data items.
2 The length of the data items is not necessary to be known prior to allocation.
Cons Overhead of the link to the next data item.
TREES
A tree is a data structure consisting of nodes organized as a hierarchy - see figure.
Terminology
A node is a structure which may contain a value, a condition, or represent a separate
data structure.
Each node in a tree has zero or more child nodes
A node that above the child is called the parent node.
A node has at most one parent.
Nodes that do not have any children are called leaf nodes.
They are also referred to as terminal nodes.
The height of a node is the length of the longest
downward path to a leaf from that node.
The height of the root is the height of the tree.
The depth of a node is the length of the path to its root (i.e., its root path).
The topmost node in a tree is called the root node. The root node will not have parent.
An internal node or inner node is any node of a tree that has child nodes and is thus not
a leaf node.
A subtree of a tree T is a tree consisting of a node in T and all of its descendants in T.
Binary trees
The simplest form of tree is a binary tree.
A binary tree is a tree in which each node has at most two
descendants - a node can have just one but it can’t have more
than two.
A binary tree consists of:
a. a node (called the root node) and
b. left and right sub-trees.
Both the sub-trees are themselves binary trees.
Root Node
Node at the “top” of a tree - the one from which all operations on the tree begins.
The root node may not exist (a NULL tree with no nodes in it) or have 0, 1 or 2 children
in a binary tree.
Leaf Node Node at the “bottom” of a tree - farthest from the root.
Leaf nodes have no children.
Complete Tree Tree in which each leaf is at the same distance from the root. i.e. all the
nodes have maximum two subtrees.
Height Number of nodes which must be traversed from the root to reach a leaf of a
tree.
GRAPHS
A graph is a set of vertices and edges which connect them.
A graph is a collection of nodes called vertices, and the
connections between them, called edges.
Undirected and directed graphs
When the edges in a graph have a direction, the graph is called a directed
graph or digraph, and the edges are called directed edges or arcs.
Neighbours and adjacency
A vertex that is the end-point of an edge is called a neighbour of the vertex. i.e., its
starting-point.
The first vertex is said to be adjacent to the second.
The diagram shows a graph with 5 vertices and 7 edges.
The edges between A and D and B and C are pairs that make a bidirectional connection,
represented here by a double-headed arrow.