Ds Module 1 Notes
Ds Module 1 Notes
Basic Data structures that directly operate upon the Machine instructions
More complicated data structures and derived from primitive data structures
Non primitive data structures are also called Compound data structures
Linear data structures are data structure where its values or elements are stored in sequential or linear
order is called linear data structure
Example: Array, stack, Queue, Linked list
Non-Linear Data Structures is a data structures where its values or elements are not stored in
sequential or linear order is called Non-linear data structures
Example: Trees and Graphs
The basic operations that are performed on data structures are as follows:
Insertion: Addition of new data element in a data structure
Deletion: Removal of data element from the data structure if it is found
Searching: Searching for the specified data element in a data structure
Traversal: Processing (Visiting) all the data elements present in the data structure
Sorting: Arranging data elements of a data structure in a specified order
Merging: Combining elements of two similar data structure to form a new data structure of
the same type
Review of Arrays
Array Operations
Each element of an array is accessed exactly once for processing. This is also called visiting
of an array
Each element is accessed in linear order either from left-to-right or from right-to-leftConsider
the following example:
Inserting:
Searching:
Find whether given element is present in the array and if present what location it occurs
The two searching techniques are:
* Linear Search
* Binary Search
Linear search
Start from the left most element of array and compare key element with each elements of
the array
if key matches with any of the elements, return the index, otherwise return -1
Very simple
Binary Search
The key element is searched with the middle element of the array
if the key element is greater than the mid element then the searching is applied on the
second half of the array otherwise on the first half of the array
Advantages of Binary Search:
Structures
Structures are user-defined data type and using structure we can define a data type which holds more
than element of different type.
Syntax of Structure:
In Arrays, only homogeneous elements are stored so using arrays we cannot process info like
Employee data, Student data etc. So, we make use of structures.
Structure variable is declared in main function as:
void main()
{
int a;
struct Employee e;
}
Based on different data members present inside the structure the number of bytes of memory
will be allocated
In the above example, memory allocation is as shown below.
Once the memory is allocated, we can store/ retrieve the info in the structure using (.) operator
also called as “Accessor” such as e.id, e.name, e.salary to access the member of structure
using
Member operator(.)
Structure pointer operator()
Consider the following example:
#include<stdio.h>
struct Point
{
int x;
Typedef is a powerful tool that allows programmer to define and then use their own data type.
The general syntax to declare typedef structure:
typedef struct
type1 member1;
type2 member2;
.
}type_id;
typedef struct
char name[10];
int roll_no;
float avg_marks;
}Student;
Student is the type created by the user using the keyword typedef.
We can use Student as the data type and declare the variables as:
Student CSE, ISE;
#include<stdio.h>
typedef struct
{
int x;
int y;
} Point;
Point p1;
p1.x = 1;
p1.y = 3;
Self-Referential Structure
};
int main()
ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;
ob2.data2 = 40;
ob1.link = &ob2;
Unions
Structure allocates enough space to store all the fields in the struct.
Union only allocated enough space to store the largest field listed and all the fields are stored
at the same space.
Syntax:
union Union_name
data_type name1;
data_type name2;
data_type name3;
.
};
Example:
union Student
int rollno;
float marks;
char name[10];
}x;
Max size is only 10 bytes which will be shared by rollno, marks and name.
float salary;
int workerNo;
} j;
main()
{
j.salary = 12.3;
j.workerNo = 100; //when j.workerNo is assigned a value j.salary will no longer hold 12.3
printf("Salary = %f\n", j.salary);
printf("Number of workers = %d", j.workerNo);
Output:
Salary = 0.0
#include<stdio.h>
main()
{
Since int is the default type specifier, some programmers omit the return type when defining a
function
The return type defaults to int which can later be interpreted as a pointer.
Syntax:
calloc():
Synatx:
int *ptr1,*ptr2;
ptr1=(int*)malloc(sizeof(int));
ptr2=(int*)calloc(1,sizeof(int));
printf("ptr1 points to value %d\n", *ptr1);
printf("ptr2 points to value %d\n", *ptr2);
}
Raksha Puthran, Assistant Professor, SDIT
Output:
syntax:
free():
Data_type array_name[d1][d2][d3][d4]………[dn];
Strings
Strings are defined as an array of characters terminated with a special character ‘\0’.
Declaring a string:
char str_name[size];
char str[50];
printf(“Enter a string”);
scanf("%s",str);
printf(“Entered string is:”);
printf("%s",str);
}
String Operations
1. strcat():
#include<string.h>
main()
{
Output:
Computer Science
int length;
char s[20] = “data structure”;
length=strlen(s);
printf(“\Length of the string is = %d \n”, length);
Output:
3. strcpy()
#include<string.h>
main()
{
Output:
4. strcmp()
#include<string.h>
main()
{
char str1[]=”computer”;
char str2[]=”science”;
int i,j,k;
i=strcmp(str1, “computer”);
j=strcmp(str1, str2);
k=strcmp(str2, “Information”);
printf(“\n %d %d %d”,i,j,k);
}
Output:
Raksha Puthran, Assistant Professor, SDIT
0 -1 1
5. Substring
6. Indexing
Indexing also called pattern matching 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 value 0.
String insertion
Assume that we have two strings say string1 and string2 and to insert string2 into string 1 starting at
ith position of string1 as follows:
Pattern matching
Pattern matching is the problem of deciding whether or not a given pattern P appears in a text T or
not
we assume that length of P does not exceed the length of T
In this technique we compare a given pattern P with each of the substring of T, moving from left
to right, until we get a match.
Consider the following Example where pattern is matched with the given text with the window
size=4
If there is no match, then pattern matched with the next set of characters with the window size=4
again
int m=strlen(txt);
int n=strlen(pat);
for(i=0; i<m-n; i++)
if(j==m)
2. nfind
nfind compares the characters from pat with those of strings
The end of the string and pat arrays are held by lasts and lastp respectively
First, nfind compares string[endmatch] and pat[lastp]
If they match, nfind uses i and j to move through the two strings until a mismatch occurs or
until all of pat is matched
Variable start is used to reset i if a mismatch occurs
Following is the simulation of nfind:
int i,j,start=0;
int lasts=strlen(str)-1;
int lastp=strlen(pat)-1;
int endmatch=lastp;
if ( str[endmatch] == pat[lastp] )
if( j == lastp )
return start;
return -1;
Pattern: a b a c a b
Failure function is as follows:
A polynomial object is a homogeneous ordered list of pairs < exponent, coefficient>, where each
coefficient is unique.
Operations include returning the degree, extracting the coefficient for a given exponent, addition,
multiplication, evaluation for a given input.
Representation of a Polynomial:
A polynomial is an expression that contains more than two terms.
A term is made up of coefficient and exponent. An example of polynomial is
P(x) = 4x3+6x2+7x+9
A polynomial thus may be represented using arrays or linked lists. Array representation assumes
that the exponents of the given expression are arranged from 0 to the highest value (degree), which
is represented by the subscript of the array beginning with 0. The coefficients of the respective
exponent are placed at an appropriate index in the array.
0 1 2 3 4 5
To preserve space, we have an alternative representation, that uses only one global array, terms, to
store all polynomial.
C Representation is as follows:
SPARSE MATRICES
By contrast, if most of the elements are nonzero, then the matrix is considered dense. When storing
and manipulating sparse matrices on a computer, it is beneficial and often necessary to use
specialized algorithms and data structures that take advantage of the sparse structure of the matrix.
Operations using standard dense matrix structures and algorithms are slow and inefficient when
applied to large sparse matrices as processing and memory are wasted on the zeroes.
Sparse data is by nature more easily compressed and thus require significantly less storage. Some
very large sparse matrices are infeasible to manipulate using standard dense-matrix algorithms.
C Representation:
Each element a[i][j] in the original matrix becomes element b[j][i] in the transpose matrix