Introduction
to
Data Structures
Definition
2
Introduction
Prof. K. Adisesha
3
Program=algorithm + Data Structure
Classification of Data Structure
4
Classification of Data Structure
5
Primitive Data Structure
6
Primitive Data Structure
7
Non-Primitive Data Structure
8
Non-Primitive Data Structure
9
Linear Data structures:
elements.
series.
Lists.
Non-Linear Data structures:
connected to several other data items.
parent child relationship.
Non-Primitive Data Structure
10
Different between them
11
Description of various Data Structures : Arrays
Prof. K. Adisesha
12
One dimensional array:
13
array.
that:
Datatype : Type of value it can store (Example: int, char, float)
Array_Name: To identify the array.
Arrays
14
Represent a Linear Array in memory
15
The elements of linear array are stored in consecutive memory locations. It is shown below:
Arrays
16
(Upperbound-lowerbound)+1
For(i=0;i<=9;i++)
{ scanf(“%d”,&arr[i]);
printf(“%d”,arr[i]); }
Arrays types
Prof. K. Adisesha
17
Basic operations of Arrays
18
Traversing Arrays
19
Traversing: It is used to access each data item exactly once so
that it can be processed.
E.g.
We have linear array A as below:
| 1 | 2 | 3 | 4 | 5 |
| 10 | 20 | 30 | 40 | 50 |
Here we will start from beginning and will go till last element and during this process we will access value of each element exactly once as below:
A [1] = 10
A [2] = 20
A [3] = 30
A [4] = 40
A [5] = 50
Insertion into Array
20
Insertion: It is used to add a new data item in the given collection of data items.
E.g.We have linear array A as below:
1 | 2 | 3 | 4 | 5 |
10 | 20 | 50 | 30 | 15 |
New element to be inserted is 100 and location for insertion is 3. So shift the elements from 5th location to 3rd location downwards by 1 place.And then insert 100 at 3rd location. It is shown below:
Deletion from Array
21
Deletion: It is used to delete an existing data item from the given
collection of data items.
Searching in Arrays
22
Searching: It is used to find out the location of the data item if it exists in the given
collection of data items.
E.g. We have linear array A as below:
1 | 2 | 3 | 4 | 5 |
15 | 50 | 35 | 20 | 25 |
Suppose item to be searched is 20.We will start from beginning and will compare 20 with each element. This process will continue until element is found or array is finished. Here:
20 # 15, go to next element.
20 # 50, go to next element.
20 #35, go to next element.
20 = 20, so 20 is found and its location is 4.
Linear Search
23
Binary Search
24
Binary Search
25
Binary Search
26
Searching
27
Sorting
28
Insertion Sort
29
unsorted elements.
While(J >= 1)
if ( A[J] < A[J-1] ) then
Temp = A[J]; A[J] = A[J-1]; A[J-1] = Temp;
[End if]
J = J-1
[End of While loop] [End of For loop]
Merging from Array
30
Merging: It is used to combine the data items of two sorted
files into single file in the sorted form
We have sorted linear array A as below:
1 | 2 | 3 | 4 | 5 | 6 |
10 | 40 | 50 | 80 | 95 | 100 |
And sorted linear array B as below: | |||||
1 | 2 | 3 | 4 | ||
20 | 35 | 45 | 90 | ||
After merging merged array C is as below:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
10 | 20 | 35 | 40 | 45 | 50 | 80 | 90 | 95 | 100 |
Two dimensional array
31
columns.
the order of the matrix and denoted as mxn.
number of rows and number of columns.
| A[0] | A[1] | A[2] |
A[0] | 10 | 20 | 30 |
A[1] | 40 | 50 | 60 |
A[2] | 70 | 80 | 90 |
Representation of Two Dimensional Array:
32
o Column-major method
Two Dimensional Array:
33
1000 | 10 | A[0][0] |
1002 | 20 | A[0][1] |
1004 | 30 | A[0][2] |
1006 | 40 | A[1][0] |
1008 | 50 | A[1][1] |
1010 | 60 | A[1][2] |
1012 | 70 | A[2][0] |
1014 | 80 | A[2][1] |
1016 | 90 | A[2][2] |
Row-Major Method
1000 | 10 | A[0][0] |
1002 | 40 | A[1][0] |
1004 | 70 | A[2][0] |
1006 | 20 | A[0][1] |
1008 | 50 | A[1][1] |
1010 | 80 | A[2][1] |
1012 | 30 | A[0][2] |
1014 | 60 | A[1][2] |
1016 | 90 | A[2][2] |
Col-Major Method
Advantages of Array:
34
Disadvantages of Array
35
Stack
36
Representation of Stack in Memory
37
Operation on Stacks:
Prof. K. Adisesha
38
Stack Conditions
Prof. K. Adisesha
39
PUSH Operation
The process of adding one element or item to the stack is represented by an operation called as the PUSH operation.
Prof. K. Adisesha
40
PUSH Operation:
Prof. K. Adisesha
41
ALGORITHM:
PUSH (STACK, TOP, SIZE, ITEM)
STACK is the array with N elements. TOP is the pointer to the top of the element of the array. ITEM to be inserted.
Step 1: if TOP = N then [Check Overflow]
PRINT “ STACK is Full or Overflow”
Exit
[Increment the TOP]
[Insert the ITEM]
[End if]
Step 2: TOP = TOP + 1
Step 3: STACK[TOP] = ITEM
Step 4: Return
POP Operation
Prof. K. Adisesha
42
The process of deleting one element or item from the stack is represented by an operation called as the POP operation.
When elements are removed continuously from a stack, it shrinks at same end i.e., top
POP Operation
Prof. K. Adisesha
43
The process of deleting one element or item from the stack is represented by an operation called as the POP operation.
ALGORITHM: POP (STACK, TOP, ITEM)
STACK is the array with N elements. TOP is the pointer to the top of the element of the array. ITEM to be inserted.
Step 1: if TOP = 0 then [Check Underflow] PRINT “ STACK is Empty or Underflow”
Exit [End if]
[copy the TOP Element] [Decrement the TOP]
Step 2: ITEM = STACK[TOP] Step 3: TOP = TOP - 1
Step 4: Return
PEEK Operation
Prof. K. Adisesha
44
The process of returning the top item from the stack but does not remove it called as the POP operation.
ALGORITHM: PEEK (STACK, TOP)
STACK is the array with N elements. TOP is the pointer to
the top of the element of the array.
Step 1: if TOP = NULL then [Check Underflow]
PRINT “ STACK is Empty or Underflow”
Exit
[End if]
Step 2: Return (STACK[TOP] [Return the top
element of the stack] Step 3:Exit
Application of Stacks
Prof. K. Adisesha
45
Runtime memory management.
– letter by letter and then pop letter from the stack.
Arithmetic Expression
Prof. K. Adisesha
46
Example: +ab
An expression is a combination of operands and operators that after evaluation results in a single value.
Infix Expression: If an operator is in between two operands, it is called
infix expression.
Postfix Expression: If an operator follows the two operands, it is called
postfix expression.
Prefix Expression: an operator precedes the two operands, it is called
prefix expression.
Arithmetic Expression
47
Prof. K. Adisesha
Queue
48
Prof. K. Adisesha
Queue
49
Prof. K. Adisesha
Types of Queues
Prof. K. Adisesha
50
Simple Queue
Prof. K. Adisesha
51
Simple Queue: In simple queue insertion occurs at the rear end of the list and deletion occurs at the front end of the list.
Circular Queue
Prof. K. Adisesha
52
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
A priority queue is a queue that contains items that have some present priority. An element can be inserted or removed from any position depending upon some priority.
Prof. K. Adisesha
53
Dequeue Queue
Prof. K. Adisesha
54
Dequeue: It is a queue in which insertion and deletion takes place at the both ends.
Operation on Queues
Prof. K. Adisesha
55
Memory Representation of a queue using array
Prof. K. Adisesha
56
element inserted.
empty.
Memory Representation of a queue using array
Prof. K. Adisesha
57
Queue Insertion Operation (ENQUEUE):
ALGORITHM: ENQUEUE (QUEUE, REAR, FRONT, ITEM)
QUEUE is the array with N elements. FRONT is the pointer that contains the location of the element to be deleted and REAR contains the location of the inserted element. ITEM is the element to be inserted.
Step 1: if REAR = N-1 then [Check Overflow] PRINT “QUEUE is Full or Overflow”
Exit [End if]
Step 2: if FRONT = NULL then [Check Whether Queue is empty] FRONT = -1
REAR = -1
else
REAR = REAR + 1 [Increment REAR Pointer]
Step 3: QUEUE[REAR] = ITEM [Copy ITEM to REAR position]
Prof. K. Adisesha
58
Step 4: Return
Queue Deletion Operation (DEQUEUE)
ALGORITHM: DEQUEUE (QUEUE, REAR, FRONT, ITEM)
QUEUE is the array with N elements. FRONT is the pointer that contains the location of the element to be deleted and REAR contains the location of the inserted element. ITEM is the element to be inserted.
Step 1: if FRONT = NULL then [Check Whether Queue is empty] PRINT “QUEUE is Empty or Underflow”
Exit [End if]
Step 2: ITEM = QUEUE[FRONT]
Step 3: if FRONT = REAR then [if QUEUE has only one element] FRONT = NULL
REAR = NULL
else
FRONT = FRONT + 1 [Increment FRONT pointer]
Prof. K. Adisesha
59
Step 4: Return
Application of Queue
Prof. K. Adisesha
60
Lists
Prof. K. Adisesha
61
collection of variable number of data items called nodes.
Lists
Prof. K. Adisesha
62
Single linked list
Prof. K. Adisesha
63
A singly linked list contains two fields in each node - an
information field and the linked field.
Single circular linked list
Prof. K. Adisesha
64
The link field of the last node contains the memory address of the first node, such a linked list is called circular linked list.
Doubly linked list
Prof. K. Adisesha
65
It is a linked list in which each node is points both to the next node and also to the previous node.
node in the list.
Doubly circular linked list
Prof. K. Adisesha
66
Operation on Linked List
Prof. K. Adisesha
67
Creating a linked list
Prof. K. Adisesha
68
The nodes of a linked list can be created by the following structure declaration.
struct Node
{
int info;
struct Node *link;
}*node1, node2;
Operator new and delete
Prof. K. Adisesha
69
Traversing a linked list:
Prof. K. Adisesha
70
the address of the first node. Another pointer p is temporarily used to visit all the nodes from the beginning to the end of the linked list.
Step 1: P = START
Step 2: while P != NULL
Step 3:
Step 4:
PROCESS data (P)
P = link(P)
[Fetch the data] [Advance P to next node]
Step 5: End of while
Step 6: Return
Inserting a node into the linked list
Prof. K. Adisesha
71
Inserting node at Front
Prof. K. Adisesha
72
Inserting a node at the beginning of the linked list
Inserting node at Front
Prof. K. Adisesha
73
ALGORITHM: INS_BEG (START, P)
START contains the address of the first node.
Step 1: P new Node;
num; START P
Step 2: data(P)
Step 3: link(P)
Step 4: START
Step 5: Return
Inserting node at Last
Prof. K. Adisesha
74
Inserting node at Last
ALGORITHM: INS_END (START, P) START contains
the address of the first node.
Step 1: START
Step 2: P START [identify the last node]
while P!= null P next (P) End while
Step 3: N new Node;
Step 4: data(N) item; Step 5: link(N) null Step 6: link(P) N Step 7: Return
Prof. K. Adisesha
75
Inserting node at a given Position
Prof. K. Adisesha
76
count+1 next (P)
Count 0 Step 3: while P!= null
count P
End while
Step 4: if (POS=1)
Call function INS_BEG( ) else if (POS=Count +1) Call function INS_END( )
For(i=1; i<=pos; i++)
P next(P);
end for
new node item; link(P)
N
[create] N data(N) link(N) link(P) else
PRINT “Invalid position”
Step 5: Return
ALGORITHM: INS_POS (START, P) START contains the
address of the first node.
Step 1: START else if (POS<=Count) Step 2: P START [Initialize node] P Start
Deleting an node
Prof. K. Adisesha
77
Deleting node from end
Prof. K. Adisesha
78
ALGORITHM: DEL_END (P1, P2, START) This used two
pointers P1 and P2. Pointer P2 is used to traverse the linked list and pointer P1 keeps the location of the previous node of P2.
Step 1: START
Step 2: P2 START;
Step 3: while ( link(P2) ! = NULL) P1 P2
P2 link(P2) While end
Step 4: PRINT data(p2)
NULL
Step 5: link(P1)
Free(P2) Step 6: STOP
Deleting node from end
Prof. K. Adisesha
79
Non-Linear Data structures
80
Trees
81
Terminology of a Tree
82
Binary Tree
83
Binary tree using array represents a node which is numbered sequentially level by level from left to right. Even empty nodes are numbered.
Graph
84
Graph
85
Example of graph:
v2
v1
v5
v3
10
15
8
6
11
9
v4
v1
v2 v4
v3
[a] Directed & Weighted Graph
[b] Undirected Graph
Graph
86
Graph
87
Thank you
88