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

Module 1

The document discusses various data structures and their classifications. It describes primitive data structures like integers and characters that are directly operated on by machine instructions. Non-primitive data structures are more sophisticated structures derived from primitive ones, like linked lists, stacks, queues, trees and graphs. Linear data structures like linked lists arrange elements sequentially while non-linear structures like trees are not organized sequentially. Common operations on data structures include traversing, searching, inserting, deleting, sorting and merging.

Uploaded by

Divya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Module 1

The document discusses various data structures and their classifications. It describes primitive data structures like integers and characters that are directly operated on by machine instructions. Non-primitive data structures are more sophisticated structures derived from primitive ones, like linked lists, stacks, queues, trees and graphs. Linear data structures like linked lists arrange elements sequentially while non-linear structures like trees are not organized sequentially. Common operations on data structures include traversing, searching, inserting, deleting, sorting and merging.

Uploaded by

Divya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 175

Module 1

Introduction
Data Structures
 Data structure is representation of the logical
relationship existing between individual
elements of data.

 In other words, a data structure is a way of


organizing all data items that considers not
only the elements stored but also their
relationship to each other.
Classifications
Data structure are normally divided into two
broad categories:

◦ Primitive Data Structure


◦ Non-Primitive Data Structure
Primitive Data Structure
 These are basic structures and directly
operated upon by the machine instructions.

 In general, there are different representation


on different computers.

 Integer, Floating-point number, Character


constants, string constants, pointers etc, fall
in this category.
Non Primitive Data Structure
 There are more sophisticated data structures.

 These are derived from the primitive data


structures.

 The non-primitive data structures emphasize


on structuring of a group of homogeneous
(same type) or heterogeneous (different type)
data items.
 Linked List, Stack, Queue, Tree, Graph are
example of non-primitive data structures.

 The design of an efficient data structure must


take operations to be performed on the data
structure.
Linear Data Structure
 A Linear data structure have data elements
arranged in sequential manner and each
member element is connected to its previous
and next element.

 Such data structures are easy to implement as


computer memory is also sequential. 

 Examples of linear data structures are Linked


List, Queue, Stack, Array etc.
Non-Linear Data Structure
 The data structure where data items are not
organized sequentially is called non linear
data structure.

  Examples of non linear data structures are


Trees and Graphs.

 A tree is collection of nodes where these


nodes are arranged hierarchically and form a
parent child relationships.
Stack
 Stack is a linear data structure which works
on LIFO order. So that Last In First Out .

 In stack element is always added at top of


stack and also removed from top of the stack.

 Stack is useful in recursive function, function


calling, mathematical expression calculation,
reversing the string etc.
Queue
 Queue is also a linear data structure which
works on FIFO order. So that First In First Out.

 In queue element is always added at rear of


queue and removed from front of queue.

 Queue applications are in CPU scheduling,


Disk Scheduling,IO Buffers, pipes, file IO.
Linked List
 A linked list is a linear collection of data elements,
in which linear order is not given by their physical
placement in memory.

 Linked List is a collection of elements called


nodes.

 Each node contains two parts: data and link part

 Linked List may be use for dynamic


implementation of stack and queue.
Tree
 A tree can be empty with no nodes or a tree is
a structure consisting of one node called the
root and zero or one or more subtrees.
 Nodes can be added at any different node.
 Tree applications includes:-
1. Manipulate hierarchical data.
2. Make information easy to search (see tree
traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital
images for visual effects.
5. Router algorithms
Graph
 A graph is a non linear data structure. A set
of items connected by edges. Each item is
called a vertex or node. Formally, a graph is a
set of vertices and a binary relation between
vertices, adjacency.

 Graph applications:-
 finding shortest routes, searching, social
network connections, internet routing.
Data Structures operations
Following operations can be performed on the
data structures:
 1. Traversing
 2. Searching
 3. Inserting
 4. Deleting
 5. Sorting
 6. Merging
Cont..
1. Traversing- It is used to access each data
item exactly once so that it can be processed.
2. Searching- It is used to find out the location
of the data item if it exists in the given
collection of data items.
3. Inserting- It is used to add a new data item
in the given collection of data items.
4. Deleting- It is used to delete an existing
data item from the given collection of data
items.
Cont…
5. Sorting- It is used to arrange the data items
in some order i.e. in ascending or descending
order in case of numerical data and in
dictionary order in case of alphanumeric data.
6. Merging- It is used to combine the data
items of two sorted files into single file in the
sorted form.
Review of Arrays
 An array is a data structure that can store a
fixed-size sequential collection of elements
of the same data type.

 Syntax
datatype arrayName [ arraySize ];

Example:
float salary[15000];
 Arrays can be initialized at declaration time:
int age[5]={22,25,30,32,35};
One Dimensional Array
 An array which has only one subscript is
known as one dimensional array .

 Syntax: data_type varname[size];


Eg: int arr[10].
Rules For Declaring One Dimensional Array

 An array variable must be declared before being


used in a program.
 The declaration must have a data type(int, float,

char, double, etc.), variable name, and


subscript.
 The subscript represents the size of the array. If

the size is declared as 10, programmers can


store 10 elements.
 An array index always starts from 0. For

example, if an array variable is declared as


s[10], then it ranges from 0 to 9.
 Each array element stored in a separate memory

location.
Two Dimensional Array
Declaring a two dimensional array
Initializing Two Dimensional Array
Structures
 Structure  is a user defined data type that
groups logically related data items of
different data types into a single unit.

 Syntax
struct [structure tag]
{
data type variable name;
data type variable name;
data type variable name;
};
Access members of a structure

There are two types of operators used for


accessing members of a structure.

 . - Member operator


 -> - Structure pointer operator
Self Referential Structures
 Self Referential structures are those 
structures that have one or more pointers
which point to the same type of structure, as
their member.
 In the above example ‘link’ is a pointer to a
structure of type ‘node’. Hence, the structure
‘node’ is a self-referential structure with
‘link’ as the referencing pointer.

 An important point to consider is that the


pointer should be initialized properly before
accessing, as by default it contains garbage
value.
Union
 Union  is a user defined data type that groups
logically related data items of different data
types into a single unit.
Example
#include <stdio.h>  
#include <string.h>  
union employee    
{   int id;    
    char name[50];    
}e1;  //declaring e1 variable for union  

int main( )  
{  
   //store first employee information  
   e1.id=101;  
   strcpy(e1.name, “abc");//copying string into char array  
   //printing first employee information  
   printf( "employee 1 id : %d\n", e1.id);  
   printf( "employee 1 name : %s\n", e1.name);  
   return 0;  
Difference between Structure and
Union
Why this difference in the size of union and structure variables?
 Here, the size of sjob is 40 bytes because
 the size of name[32] is 32 bytes
 the size of salary is 4 bytes
 the size of workerNo is 4 bytes

 However, the size of ujob is 32 bytes. It's because the size of


a union variable will always be the size of its largest element.

 In the above example, the size of its largest element,


(name[32]), is 32 bytes.

 With a union, all members share the same memory.


Pointers
 Pointer is a derived data-type whose value
refers directly to (or "points to") another value
stored elsewhere in the computer memory
using its address.

 Declaring and initializing Pointers


Syntax:
data-type *pointer-variable
Reference operator (&) and Dereference operator (*)

 To get the value stored in the memory


address, we use the dereference operator (*).

 For example: If a number variable is stored in


the memory address 0x123, and it contains a
value 5.

 The reference (&) operator gives the


value 0x123, while the dereference
(*) operator gives the value 5.
Example
Dynamic Memory Allocation
Functions
 Dynamic Memory Allocation can be defined as a
procedure in which the size of a data structure
(like Array) is changed during the runtime.

 C provides some functions to achieve these


tasks. There are 4 library functions provided by C
defined under <stdlib.h> header file to facilitate
dynamic memory allocation in C programming.
They are:
 malloc()
 calloc()
 free()
 realloc()
 “malloc” or “memory allocation” method in C
is used to dynamically allocate a single large
block of memory with the specified size.

 “calloc” or “contiguous allocation” method in
C is used to dynamically allocate the specified
number of blocks of memory of the specified
type. It initializes each block with a default
value ‘0’.
 “free” method in C is used to dynamically de-
allocate the memory. The memory allocated
using functions malloc() and calloc() is not de-
allocated on their own. Hence the free() method
is used, whenever the dynamic memory
allocation takes place. It helps to reduce wastage
of memory by freeing it.

 “realloc” or “re-allocation” method in C is used to
dynamically change the memory allocation of a
previously allocated memory. In other words, if
the memory previously allocated with the help of
malloc or calloc is insufficient, realloc can be
used to dynamically re-allocate memory. re-
allocation of memory maintains the already
present value and new blocks will be initialized
with default garbage value.
Malloc
 “malloc” or “memory allocation” method in C
is used to dynamically allocate a single large
block of memory with the specified size. It
returns a pointer of type void which can be
cast into a pointer of any form. It initializes
each block with default garbage value.
Syntax:
 ptr = (cast-type*) malloc(byte-size)
Calloc
 “calloc” or “contiguous allocation” method in
C is used to dynamically allocate the specified
number of blocks of memory of the specified
type. It initializes each block with a default
value ‘0’.

Syntax:
 ptr = (cast-type*)calloc(n, element-size);
free
 “free” method in C is used to dynamically de-
allocate the memory.

 The memory allocated using functions malloc()


and calloc() is not de-allocated on their own.
Hence the free() method is used, whenever the
dynamic memory allocation takes place.

 It helps to reduce wastage of memory by


freeing it.
Syntax:
 free(ptr);
realloc
 “realloc” or “re-allocation” method in C is
used to dynamically change the memory
allocation of a previously allocated memory.
 In other words, if the memory previously

allocated with the help of malloc or calloc is


insufficient, realloc can be used
to dynamically re-allocate memory.
 re-allocation of memory maintains the
already present value and new blocks will be
initialized with default garbage value.
Linear Arrays
 A linear array, is a list of finite numbers of
elements stored in the memory.

 In a linear array, we can store only


homogeneous data elements.

 Elements of the array form a sequence


or linear list, that can have the same type of
data.

 Each element of the array, is referred by an


index set.
 Length of the array can be obtained from the
index set by the formula:

Length= UB-LB+1

Where UB is the largest index(upper bound)


LB is the smallest index(lower bound) of
the array.
Representation of Linear arrays in
memory
 Address of the first element of Linear array,
denoted by:
◦ Base(LA)
Base address of LA

Using this address Base(LA), the computer calculates


the address of any element of LA by using the
formula:

LOC(LA[K]) = Base(LA) + w(K- lower bound)


Array Operations
Following are the basic operations supported
by an array.
 Traversing − print all the array elements one

by one.
 Inserting − Adds an element at the given

index.
 Deleting − Deletes an element at the given

index.
 Searching − Searches an element using the

given index or by the value.


 Sorting- Operation of rearranging the

elements
Traversing
 This operation is to traverse through the
elements of an array.
Inserting
 Insert operation is to insert one or more data
elements into an array. Based on the
requirement, a new element can be added at
the beginning, end, or any given index of
array.
Example
Deleting
 Deletion refers to removing an existing
element from the array and re-organizing all
elements of an array.
Searching
  Search for an array element based on its
value or its index.

 Linear Search Program


 Enter the number of elements
 5
 Enter the elements one by one

 10
 20
 30
 40
 50
 Enter the element to be searched

 20
 The element is present in the array of position
 1
 Binary Search Program
Sorting
 Sorting is the process of rearranging the
elements.
 Eg: Bubble Sort
Multidimensional Arrays
 Multi-Dimensional array means array of
arrays. A 3D array is a multi-dimensional
array. A 3D array is a collection of 2D arrays.

 It is specified by using three subscripts :


Block size, row size and column size.

 More dimensions in an array means more


data can be stored in that array.
Declaraing 3D Array
To declare 3D array:
 Specify data type, array name, block size, row

size and column size.

 Each subscript can be written within its own


separate pair of brackets.

 Syntax:
data_type array_name[block_size][row_size]
[columnsize];
Example
Strings
 A finite sequence S of zero or more
characters is called a string. The difference
between a character array and a string is the
string is terminated with a special character ‘\
0’.

 The number of characters in a string is called


its length.

 The string with zero characters is called the


empty string or the null string.
String operations
 Substring

 Indexing

 Concatenation

 Length
Substring
 A substring is itself a string that is part of a
longer string.

 For example, substrings of string "the" are ""


(empty string), "t", "th", "the", "h", "he" and
"e."

 The header file "string. h" does not contain


any library function to find
a substring directly.
Cont..
Accessing a substring from a given string
requires three information:

 Name of the string or string itself.


 The position of the first character of the

substring in the given string.


 The length of the substring or the position of

the last character of the substring.

 SUBSTRING(string,initial,length)
String : GSSSIETW MYSUR
Position : 2
Length: 9
Indexing
 Indexing refers to finding the position where
a string pattern P first appears in a given
string text T.

 INDEX(text, pattern)

 If the pattern P does not appear in the text T,


then INDEX is assigned the value 0. The
arguments “text” and “pattern” can be either
string constants or string variables.
Hi there, how are yo
O
Concatenation
  String concatenation is the operation of
joining character strings end-to-end.
For example, the concatenation of "snow" and
"ball" is "snowball".
 Basket and ball =“basketball”

 Strcat(S1,S2)
Length
 The number of characters in a string is called
its length.

 Length(string)
for the length of a given string.

Eg: Length(‘COMPUTER’)= 8.
String Compare

 The strcmp() compares two strings character


by character. If the first character of two
strings is equal, the next character of two
strings are compared. This continues until
the corresponding characters of two
strings are different or a null character '\0' is
reached. It is defined in the string.

 Function strcmp is case sensitive and returns


0 if both the strings are same.
Example
 Enter a string str1: Cricket
Enter a string str2: Cricket
Storing Strings
 Strings are stored in three types of structures:

1. Fixed length structures


2. Variable length structures with fixed
maximums
3. Linked Structures
Fixed Length Structures
 In fixed length storage each line of print is
viewed as a record where all records have the
same length.

 Each record accommodates the same number


of characters.
Advantages:
1.The ease of accessing data from any given record.
2.The ease of updating data in any given record.

Disadvantages
1.Time is wasted reading an entire record if most of
the storage consists of inessential blank spaces
2.Certain records may require more space than
available
3.When the correction consists of more or fewer
characters than the original text, changing a
misspelled word requires the entire record to be
changed
Variable-Length Storage with fixed
maximum
 Although strings may be stored in fixed
length memory locations as above, there are
advantages in knowing the actual length of
each string.

 For example, one then does not have t read


the entire record when the string occupies
only the beginning part of memory location.
 The storage of variable length strings in
memory cells with fixed lengths can be done
in two general ways:

1.One can use a marker, such as 2 dollar signs,


to signal the end of the string.
2.One can list the length of the string as an
additional item in the pointer array
Linked Storage
 One way linked list is a linearly ordered sequence of
memory cells called nodes, where each node contains an
item, called a link, which points to the next node in the
list.

 Strings may be stored in linked list as follows. Each


memory cell is assigned one character or a fixed number
of characters and a link contained in a cell gives the
address of the cell containing the next character or
group of characters in the string.

 For example, consider the following quotation


Pattern Matching Algorithms
 Pattern Matching is the problem of deciding
whether or not a given string pattern P
appears in a string text T.
KMP (Knuth Morris Pratt) Pattern
Searching
 KMP Algorithm is one of the most popular patterns matching
algorithms. KMP stands for Knuth Morris Pratt. KMP algorithm was
invented by Donald Knuth and Vaughan Pratt together and
independently by James H Morris in the year 1970. In the year
1977, all the three jointly published KMP Algorithm.

 KMP algorithm is one of the string matching algorithms used to


find a Pattern in a Text.

 Knuth Morris Pratt (KMP) is an algorithm, which checks the


characters from left to right. When a pattern has a sub-pattern
appears more than one in the sub-pattern, it uses that property to
improve the time complexity, also for in the worst case.

 The time complexity of KMP is O(n).

 Let us consider below example to understand this.


 Best case=
 Linear search
 23 63 89 78 56 90 element=23

Average case element =89

Worst case element=90 or 95


Example
Sparse Matrices
 A matrix is a two-dimensional data object made of m
rows and n columns, therefore having total m x n
values. If most of the elements of the matrix have 0
value, then it is called a sparse matrix.

Why to use Sparse Matrix instead of simple matrix ?


 Storage: There are lesser non-zero elements than zeros

and thus lesser memory can be used to store only those


elements.
 Computing time: Computing time can be saved by

logically designing a data structure traversing only


non-zero elements..
C program to check sparse matrix
Transposing a matrix
 Transpose of a matrix is obtained by changing rows to
columns and columns to rows. In other words, transpose of
A[][] is obtained by changing A[i][j] to A[j][i].

 For example, consider the following 3 X 2 matrix:


12
34
56

 Transpose of the matrix:


135
246

When we transpose a matrix then its order changes, but for


a square matrix, it remains the same.
C Program to Find the Transpose of a Matrix
Matrix Multiplication
 Two matrices with a given order can be
multiplied only when number of columns of
first matrix is equal to the number of rows of
the second matrix.
Polynomials
 A polynomial is a mathematical expression
consisting of a sum of terms, each term
including a variable or variables raised to a
power and multiplied by a coefficient.

 The simplest polynomials have one variable.


C program to evaluate a given polynomial by
reading its coefficients in an array.
C Program to add two polynomials

You might also like