100% found this document useful (1 vote)
69 views

Unit 1

The document defines and describes different types of data structures. It begins by defining data, data structures, and how data structures affect program design. It then classifies data structures as either primitive or non-primitive. Primitive structures like integers are directly operated on by machine instructions, while non-primitive structures like stacks and linked lists are built from primitive structures. The document proceeds to describe common operations on data structures and provides examples of linear and non-linear non-primitive data structures like arrays, linked lists, trees and graphs.

Uploaded by

Pushpendra Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
69 views

Unit 1

The document defines and describes different types of data structures. It begins by defining data, data structures, and how data structures affect program design. It then classifies data structures as either primitive or non-primitive. Primitive structures like integers are directly operated on by machine instructions, while non-primitive structures like stacks and linked lists are built from primitive structures. The document proceeds to describe common operations on data structures and provides examples of linear and non-linear non-primitive data structures like arrays, linked lists, trees and graphs.

Uploaded by

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

Introduction

to
Data Structures
Definition
Data: Collection of raw facts.
Data structure is representation of the
logical relationship existing between
individual elements of data.
Data structure is a specialized format for
organizing and storing data in memory that
considers not only the elements stored but also
their relationship to each other.

2
Introduction

Data structure affects the design of both


structural & functional aspects of a
program.
Program=algorithm + Data Structure
You know that a algorithm is a step by step
procedure to solve a particular function.

Prof. K. Adisesha 3
Classification of Data Structure
Data structure are normally divided
into two broad categories:
◦ Primitive Data Structure
◦ Non-Primitive Data Structure

4
Classification of Data Structure

5
Primitive Data Structure
There are basic structures and directly
operated upon by the machine
instructions.
Data structures that are directly
operated upon the machine-level
instructions are known as primitive data
structures.
Integer, Floating-point number, Character
constants, string constants, pointers etc, fall
in this category.
6
Primitive Data Structure
The most commonly used operation on
data structure are broadly categorized into
following types:
◦ Create
◦ Selection
◦ Updating
◦ Destroy or Delete

Prof. K. Adisesha 7
Non-Primitive Data Structure
There are more sophisticated
data structures.
The Data structures that are derived from the
primitive data structures are called Non-
primitive data structure.
The non-primitive data structures
emphasize on structuring of a group of
homogeneous (same type) or heterogeneous
(different type) data items.

Prof. K. Adisesha 8
Non-Primitive Data Structure
Linear Data structures:
◦ Linear Data structures are kind of data structure that has homogeneous
elements.
◦ The data structure in which elements are in a sequence and form a liner
series.
◦ Linear data structures are very easy to implement, since the memory of the
computer is also organized in a linear fashion.
◦ Some commonly used linear data structures are Stack, Queue and
Linked
Lists.
Non-Linear Data structures:
◦ A Non-Linear Data structures is a data structure in which data item is
connected to several other data items.
◦ Non-Linear data structure may exhibit either a hierarchical relationship or
parent child relationship.
◦ The data elements are not arranged in a sequential structure.
◦ The different non-linear data structures are trees and graphs.
Prof. K. Adisesha 9
Non-Primitive Data Structure
The most commonly used operation on
data structure are broadly categorized into
following types:
◦ Traversal
◦ Insertion
◦ Selection
◦ Searching
◦ Sorting
◦ Merging
◦ Destroy or Delete

10
Different between them
A primitive data structure is generally a
basic structure that is usually built into
the language, such as an integer, a float.

A non-primitive data structure is built


out of primitive data structures linked
together in meaningful ways, such as a or
a linked-list, binary search tree, AVL Tree,
graph etc.

11
Description of various
Data Structures : Arrays
An array is defined as a set of finite
number of homogeneous elements or
same data items.
It means an array can contain one type of
data only, either all integer, all float-
point number or all character.

12
One dimensional array:
 An array with only one row or column is called one-dimensional
array.
 It is finite collection of n number of elements of same type such
that:
◦ can be referred by indexing.
◦ The syntax Elements are stored in continuous locations.
◦ Elements x to define one-dimensional array is:
 Syntax: Datatype Array_Name [Size];
 Where,
Datatype : Type of value it can store (Example: int, char, float)
Array_Name: To identify the array.
 Size : The maximum number of elements that the array can hold.

13
Arrays
Simply, declaration of array is as follows:
int arr[10]
Where int specifies the data type or type
of elements arrays stores.
“arr” is the name of array & the number
specified inside the square brackets is the
number of elements an array can store, this is
also called sized or length of array.

14
Represent a Linear Array in memory
The elements of linear array are stored in
consecutive memory locations. It is
shown below:

15
Arrays
◦ The elements of array will always be stored in the
consecutive (continues) memory location.
◦ The number of elements that can be stored in an
array, that is the size of array or its length is given
by the following equation:
(Upperbound-lowerbound)+1
◦ For the above array it would be (9-0)+1=10,where
0 is the lower bound of array and 9 is the upper
bound of array.
◦ Array can always be read or written through loop.
For(i=0;i<=9;i++)
{ scanf(“%d”,&arr[i]);
printf(“%d”,arr[i]); }
16
Arrays types
Single Dimension
Array
◦ Array with one subscript
Two Dimension Array
◦ Array with two
subscripts (Rows and
Column)
Multi Dimension Array
◦ Array with Multiple
subscripts
17
Basic operations of
Arrays
Some common operation
performed on array are:
◦ Traversing
◦ Searching
◦ Insertion
◦ Deletion
◦ Sorting
◦ Merging

18
Traversing Arrays
 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
19
Insertion into Array
 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:

20
Deletion from Array
 Deletion: It is used to delete an existing data item from the given
collection of data items.

Prof. K. Adisesha 21
Two dimensional array
 A two dimensional array is a collection of elements and each
element is identified by a pair of subscripts. ( A[3] [3] )
 The elements are stored in continuous memory
locations.
 The elements of two-dimensional array as rows and
columns.
 The number of rows and columns in a matrix is called
as
the order of the matrix and denoted as mxn.
 The number of elements can be obtained by
A[0] A[1] A[2]
multiplying
A[0] 10 20 30
number of rows and number of columns.
A[1] 40 50 60
A[2] 70 80 90

Prof. K. Adisesha 22
Representation of Two Dimensional
Array:
A is the array of order m x n.To store m*n
number of elements, we need m*n memory
locations.
The elements should be in contiguous memory
locations.
There are two methods:

o Row-major method
o Column-major method

Prof. K. Adisesha 23
Two Dimensional Array:
 Row-Major Method: All the first-row elements are stored in
sequential memory locations and then all the second-row
elements are stored and so on. Ex: A[Row][Col]
 Column-Major Method: All the first column elements are
stored in sequential memory locations and then all the second-
column elements are stored and so on. Ex: A [Col][Row]
1000 10 A[0][0] 1000 10 A[0][0]
1002 20 A[0][1] 1002 40 A[1][0]
1004 30 A[0][2] 1004 70 A[2][0]
1006 40 A[1][0] 1006 20 A[0][1]
1008 50 A[1][1] 1008 50 A[1][1]
1010 60 A[1][2] 1010 80 A[2][1]
1012 70 A[2][0] 1012 30 A[0][2]
1014 80 A[2][1] 1014 60 A[1][2]

1016 90 A[2][2] 1016 90 A[2][2]

Row-Major Method Col-Major Method

Prof. K. Adisesha 24
Advantages of
Array:
It is used to represent multiple data items of
same type by using single name.
It can be used to implement other data
structures like linked lists, stacks, queues, tree,
graphs etc.
Two-dimensional arrays are used to represent
matrices.
Many databases include one-dimensional arrays
whose elements are records.

Prof. K. Adisesha 25
Disadvantages of
Array
We must know in advance the how many
elements are to be stored in array.
Array is static structure. It means that array is
of fixed size. The memory which is allocated
to array cannot be increased or decreased.
Array is fixed size; if we allocate more memory
than requirement then the memory space will
be wasted.
The elements of array are stored in consecutive
memory locations. So insertion and deletion
are very difficult and time consuming.
Prof. K. Adisesha 26
Lists
A lists (Linear linked list) can be defined as a
collection of variable number of data items
called nodes.
Lists are the most commonly used
non- primitive data structures.
Each nodes is divided into two parts:
◦ The first part contains the information of
the element.
◦ o The second part contains the memory address
of the next node in the list. Also called Link part.

Prof. K. Adisesha 27
Lists
Types of linked lists:

◦ Single linked list


◦ Doubly linked list
◦ Single circular linked list
◦ Doubly circular linked
list

Prof. K. Adisesha 28
Single linked list
A singly linked list contains two fields in each node - an
information field and the linked field.
•The information field contains the data of that node.
•The link field contains the memory address of the next node.
There is only one link field in each node, the linked list is
called singly linked list.

Prof. K. Adisesha 29
Single circular linked list
The link field of the last node contains the
memory address of the first node, such a linked
list is called circular linked list.
·In a circular linked list every node is accessible
from a given node.

Prof. K. Adisesha 30
Doubly linked
list
It is a linked list in which 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 : It is a pointer field that contains the address of the next node
◦ BACK: It is a pointer field that contains the address of the previous
node.
◦ INFO: It contains the actual data.
 In the first node, if BACK contains NULL, it indicated that it is the
first
node in the list.
 The node in which FORW contains, NULL indicates that the node is the
last node.

Prof. K. Adisesha 31
Doubly circular linked
list

Prof. K. Adisesha 32
Operation on Linked List
The operation that are performed on
linked lists are:
◦ Creating a linked list
◦ Traversing a linked list
◦ Inserting an item into a linked list.
◦ Deleting an item from the linked list.
◦ Searching an item in the linked list
◦ Merging two or more linked lists.

Prof. K. Adisesha 33
Creating a linked list
The nodes of a linked list can be created by
the following structure declaration.
struct Node
{
int info;
struct Node *link;
}*node1, node2;
 Here info is the information field and link is the link
field.
 The link field contains a pointer variable that refers the same
node structure. Such a reference is called as Self addressing
pointer. Prof. K. Adisesha 34
Operator new and delete
Operators new allocate memory space.
◦ Operators new [ ] allocates memory space
for array.
Operators delete deallocate memory
space.
◦ Operators delete [ ] deallocate memory
space for array.

Prof. K. Adisesha 35
Traversing a linked list:
 Traversing is the process of accessing each node of the
linked list exactly once to perform some operation.
 ALGORITHM: TRAVERS (START, P) START contains
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: PROCESS data (P) [Fetch the data]
Step 4: P = link(P) [Advance P to next node]
Step 5: End of while
Step 6: Return

Prof. K. Adisesha 36
Inserting a node into the
linked list
Inserting a node at the beginning of the
linked list
Inserting a node at the given position.
Inserting a node at the end of the
linked list.

Prof. K. Adisesha 37
Inserting node at Front
Inserting a node at the beginning of the linked list
1. Create a node.
2. Fill data into the data field of the new node.
3. Mark its pointer field as NULL
4. Attach this newly created node to START
5. Make the new node as the START node.

Prof. K. Adisesha 38
Inserting node at Front
ALGORITHM: INS_BEG (START, P)
START contains the address of the first
node.
Step 1: P new Node;
Step 2: data(P) num;
Step 3: link(P) STAR
Step 4: START T P
Step 5: Return

Prof. K. Adisesha 39
Inserting node at Last

Prof. K. Adisesha 40
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


Step 4: while
data(N)
Step 3:Step
item; N 5: link(N)
new
Node;
null Step 6: link(P) N
Step 7: Return Prof. K. Adisesha 75
Inserting node at a given Position
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] Count 0 For(i=1;
P Start i<=pos; i++)
Step 3: while P!= null P
count count+1 next(P);
P next (P) end for N new node
[create]
End while data(N) item;
Step 4: if (POS=1) link(N) link(P)
Call function INS_BEG( ) link(P) N
else if (POS=Count +1) else
Call function INS_END( ) PRINT “Invalid position”
Step 5: Return
Prof. K. Adisesha 42
Deleting an node
Deleting an item from the linked list:
o Deletion of the first node
o Deletion of the last node
o Deletion of the node at the give position

Prof. K. Adisesha 43
Deleting node from end
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
Step 5: end
link(P1) NULL
Step 4: PRINT Free(P2)
data(p2)
Step 6: STOP

Prof. K. Adisesha 44
Deleting node from end

Prof. K. Adisesha 45

You might also like