0% found this document useful (0 votes)
9 views36 pages

Ds Module 1 Notes

The document provides an introduction to data structures, detailing the types of data structures, including primitive and non-primitive, as well as operations performed on them such as insertion, deletion, searching, and sorting. It discusses linear and non-linear data structures, arrays, structures, unions, pointers, and dynamic memory allocation in C programming. Additionally, it covers string operations and pattern matching algorithms.

Uploaded by

ravishravi153
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views36 pages

Ds Module 1 Notes

The document provides an introduction to data structures, detailing the types of data structures, including primitive and non-primitive, as well as operations performed on them such as insertion, deletion, searching, and sorting. It discusses linear and non-linear data structures, arrays, structures, unions, pointers, and dynamic memory allocation in C programming. Additionally, it covers string operations and pattern matching algorithms.

Uploaded by

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

Module 1

Introduction to Data Structures


A Data Structures is a data organization, management and storage format that enables efficient access
and modification.

Types of Data Structure:

There are two classes of data structures such as:


Primitive Data Structures
Non- Primitive Data Structures

Primitive Data Structures

Fundamental data type that are supported by a programming language

Basic Data structures that directly operate upon the Machine instructions

Example: integer, float, character, string

Non- Primitive Data Structures

More complicated data structures and derived from primitive data structures

Non primitive data structures are also called Compound data structures

Non- Primitive Data Structures are classified as


Linear data structures
Non-Linear 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

Raksha Puthran, Assistant Professor, SDIT


Data Structure Operation

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

A linear array is a list of a finite number n homogeneous data elements.

The elements of an array are referenced by an index set


The elements of an array are stored in successive memory location
The number n indicates length or size of an array
Declaration of an array:

Raksha Puthran, Assistant Professor, SDIT


Representation of Linear array in Memory:

The memory of computer is a sequence of addressed location as shown below:

Let A be the array in memory,

LOC(A[K]) = Address of the element A[K] of the array A


Elements of A are stored in successive memory cells
The address of the first element of A is denoted by BASE(A), called base address of A
We can calculate the address of any element of A by the formula,
LOC(A[K]) = Base(A) + w (K- lowerbound)

Where, w is the number of words per memory cell for array A

Array Operations

Different operations that can be performed on Array are:


Traversing
Inserting
Deleting
Searching
Sorting
Traversing:

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:

Raksha Puthran, Assistant Professor, SDIT


Consider another example:

Inserting:

Inserting new element at the specified location


Input new element and position to insert in an array
Check for the invalid position
Shift the item from the given insertion position towards right by one position
Finally, insert new item in vacant position
Consider the following function:

Raksha Puthran, Assistant Professor, SDIT


Deleting:

Deleting an item from an unsorted Array based on position

Check whether the given position is valid or not


Remove the item from the array
Move all the item from deleted position towards left by one position and update the no of
items
Consider the following Example:

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

Simple Searching algorithm which is also called sequential 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

Raksha Puthran, Assistant Professor, SDIT


Consider the following Example:

Advantages of Linear Search:

Very simple

Works well when array size is small


Disadvantages of Linear Search:
More number of comparisons is required

Binary Search

The key element is searched with the middle element of the array

If they are equal, position of the middle element is returned

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:

simple searching technique

requires fewer no of comparisons

very efficient than linear search


Disadvantage of Binary search:
Applicable only for the sorted list of elements
Consider the following function:

Raksha Puthran, Assistant Professor, SDIT


Sorting

Rearranging the elements in linear array in ascending / Descending order


Bubble sort:
Simplest Sorting Algorithm that works by repeatedly swapping the adjacent element, if they are
in wrong order

Advantages of Bubble sort:

Simple and easy implementation


Straightforward approach for sorting
Disadvantages of Bubble sort:
More number of comparisons are required (Slow)

Raksha Puthran, Assistant Professor, SDIT


Consider the following Example:

Raksha Puthran, Assistant Professor, SDIT


Consider the following Function:

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:

Raksha Puthran, Assistant Professor, SDIT


Example:

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;

Raksha Puthran, Assistant Professor, SDIT


int y;
};
main()
{
struct Point p1;
p1.x = 1;
p1.y = 3;
printf("%d \n", p1.x); // prints 1
printf("%d \n", p1.y); // prints 3
}

Type defined Structure

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;

Consider the following Example:

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;

Consider the following Program:

#include<stdio.h>
typedef struct
{

int x;
int y;
} Point;

Raksha Puthran, Assistant Professor, SDIT


main()

Point p1;
p1.x = 1;
p1.y = 3;

printf("%d \n", p1.x); // prints 1


printf("%d \n", p1.y); // prints 3
}

Self-Referential Structure

Consider the following Example:


#include <stdio.h>
struct node {
int data1;
char data2;
struct node* link;

};

int main()

struct node ob1;

ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;

struct node ob2;


ob2.link =
NULL;ob2.data1 = 30;

ob2.data2 = 40;
ob1.link = &ob2;

printf("%d", ob1.link data1);


printf("\n%d", ob1.link data2);

Raksha Puthran, Assistant Professor, SDIT


}

Unions

Unions are declared, created and used same as structures except,

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.

Raksha Puthran, Assistant Professor, SDIT


Consider the following Program:
#include <stdio.h>
union Job {

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

Number of workers = 100

Difference between Structure and union

Raksha Puthran, Assistant Professor, SDIT


Pointers

Pointer is a variable that holds the address of another variable


Syntax:
datatype *ptr;
where ptr is the pointer name
Consider the following Example:

Consider the following Example:

#include<stdio.h>
main()
{

int number=50, *p;


p=&number;
printf("Address of p variable is %x \n",p); // prints fff4
printf("Value of p variable is %d \n",*p); // prints 50

Pointers can be dangerous

 Pointers have the same size as type int.

 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.

 This is proven to be a dangerous practice on some computers

Dynamic memory Allocation

Generally, if we declare any primitive variable as


int a;
The memory allocation will be done at compile time is called “Static memory allocation”
(fixed memory).
In case of array and structures also size is fixed
int a[10];
These limitations are avoided by using “Dynamic memory allocation” in which memory is
more explicitly managed.
The C dynamic memory allocations are defined in “stdlib.h”

Raksha Puthran, Assistant Professor, SDIT


malloc():

Does not initialize the allocated memory


Malloc takes the single argument(amount of memory to allocate in bytes)

Syntax:

ptr = (type_cast*) malloc(byte-size)


Consider the following example:

calloc():

Calloc() initializes the allocated memory to zero


Calloc() takes two argument(no of blocks to be allocated and sizeof each block)

Synatx:

ptr = (type_cast*) calloc(number_of_blocks, byte-size)


Consider the following program:
int main()

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:

ptr1 points to value 1840480


ptr2 points to value 0
realloc():
Reallocation is done by either expanding or reducing the existing area pointer by a pointer

syntax:

ptr= (datatype*)realloc(ptr, new_size);

free():

deallocates the memory allocated by calloc(), malloc() or realloc()


Syntax:
void free(*ptr)

Raksha Puthran, Assistant Professor, SDIT


Dynamically allocated arrays:
One dimensional arrays:
Following is the code for dynamic allocation of one dimensional arrays
int i, n, *list;
printf(“ enter the number of elements in the array”);
scanf(“%d”, &n);
if(n<1)
{
fprintf(stderr, “improper value of n”);
exit(EXIT_FAILURE);
}
MALLOC(list, n*sizeof(int));

Two dimensional arrays:


C uses the so- called arrays-of arrays representation to represent a multidimensional array.

Consider the following Example:

Raksha Puthran, Assistant Professor, SDIT


How to Declare a Multidimensional Array in C:
A multidimensional array is declared using the following syntax:

Data_type array_name[d1][d2][d3][d4]………[dn];

Where each d is a dimension, and dn is the size of final dimension.


Examples:
1. int table[5][5][20];
2. float arr[5][6][5][6][5];

Following is the function to create two dimensional array dynamically:

Strings

Strings are defined as an array of characters terminated with a special character ‘\0’.
Declaring a string:
char str_name[size];

Consider the following Example:

Raksha Puthran, Assistant Professor, SDIT


Example 2:

C program to read a string and print

/ C program to read string from user


#include<stdio.h>
main()

char str[50];
printf(“Enter a string”);
scanf("%s",str);
printf(“Entered string is:”);
printf("%s",str);
}

Following is the Abstract data type strings:

Raksha Puthran, Assistant Professor, SDIT


Following are the C string functions:

String Operations

1. strcat():

Consider the following program

#include<string.h>
main()
{

char src[20]= “ Science”;


char dest[20]= “Computer ”;
strcat(dest, src);
puts(dest);

Output:

Computer Science

Raksha Puthran, Assistant Professor, SDIT


2. strlen()

Consider the following program


#include<string.h>
main()

int length;
char s[20] = “data structure”;
length=strlen(s);
printf(“\Length of the string is = %d \n”, length);

Output:

Length of the string is = 14

3. strcpy()
#include<string.h>
main()
{

char src[20]= “ Destination”;


char dest[20]= “”;
printf(“\n source string is = %s”, src);
printf(“\n destination string is = %s”, dest);
strcpy(dest, src);
printf (“\n target string after strcpy() = %s”, dest);

Output:

Source string is = Destination


Destination string is =
target string after strcpy() = Destination

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

Accessing substring from a given string requires:

1)Name of the string

2) Position of the first character of the substring in the given string

3) Length of the substring

SUBSTRING( String, Initial, Length)

Example: SUBSTRING(“TO BE OR NOT TO BE”, 4, 7)= “BE OR N”

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.

Example: T= “ HIS FATHER IS THE PROFESSOR”

INDEX(T, “THE”) has the value 7

INDEX(T, “ THE ”) has the value 14

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:

Raksha Puthran, Assistant Professor, SDIT


Consider the following program for insertion:

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

There are three pattern matching algorithm


1. Simple pattern matching (Brute force method)

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

Raksha Puthran, Assistant Professor, SDIT


When the match is found, index is returned

Consider the following program:

void pattern_match(char *pat, char *txt)

int m=strlen(txt);
int n=strlen(pat);
for(i=0; i<m-n; i++)

for(j=0; j<m; j++)

if( txt[i+j] != pat[j] )


break;
}

if(j==m)

print(“Pattern found at %d location”, i);

print(“pattern not found”);

Raksha Puthran, Assistant Professor, SDIT


}

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:

Raksha Puthran, Assistant Professor, SDIT


Consider the following program:

int nfind(char *str, char *pat)

int i,j,start=0;

int lasts=strlen(str)-1;

int lastp=strlen(pat)-1;

int endmatch=lastp;

for(i=0; endmatch<=lasts; endmatch++, start++)

if ( str[endmatch] == pat[lastp] )

for(j=0, i=start; j<lastp && str[i]==pat[j];i++,j++)

if( j == lastp )

return start;

return -1;

3. Knuth, Morris, Pratt pattern matching


Knuth-Morris-Pratt (KMP) string matching algorithm is used to find all occurrences of
pattern P in S.
In KMP algorithm, a pre-processing is done in pattern string P and an array of length m is
calculated.
The basic idea behind KMP’s algorithm is: whenever we detect a mismatch (after some
matches), we
already know some of the characters in the text of the next window.
We take advantage of this information to avoid matching the characters that we know
will anyway match.
Unlike Brute-Force/Naïve algorithm, where we slide the pattern by one and compare all
characters at each shift, we use a value from failure function to decide the next
characters to be matched.
The idea is to not match a character that we know will anyway match.

Following is the pattern matching function:

Raksha Puthran, Assistant Professor, SDIT


Consider the following example:
Text: a b a c a a b a c c a b a c a b a a b b

Pattern: a b a c a b
Failure function is as follows:

Following the function for failure function

Raksha Puthran, Assistant Professor, SDIT


Polynomials

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.

Polynomials C representation is:

#define MAX_DEGREE 101 /* max degree of polynomial + 1 */


typedef struct
{
int degree;
float coef[MAX_DEGREE];
}polynomial;

Array representation of polynomial:

0 1 2 3 4 5

Raksha Puthran, Assistant Professor, SDIT


The abstract data type:

Consider the following C function (initial version) of padd function:

Raksha Puthran, Assistant Professor, SDIT


The above algorithm for finding the sum of two polynomial is although very simple algoritm, it
wastes a lot of space.

To preserve space, we have an alternative representation, that uses only one global array, terms, to
store all polynomial.

C Representation is as follows:

Consider two polynomials:


A(x)=2𝑥1000 + 1
B(x)= 𝑥 4 + 10𝑥 3 + 3𝑥 2 + 1

Raksha Puthran, Assistant Professor, SDIT


Following is the C function for polynomial addition:

Raksha Puthran, Assistant Professor, SDIT


Following is the attach function at add a new term:

SPARSE MATRICES

A sparse matrix is a matrix in which most of the elements are zero.

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.

The abstract data type:

Raksha Puthran, Assistant Professor, SDIT


Representation of sparse matrix

Sparse matrix can be represented by using array of triples<row,col,value>

C Representation:

Consider the following example:

Raksha Puthran, Assistant Professor, SDIT


Following is the sparse matrix stored as triples:

Following is the transpose sparse matrix stored as triples:

Transpose of a sparse matrix

Following function is the transpose of the sparse matrix.

To transpose a matrix, we must interchange the rows and columns

Each element a[i][j] in the original matrix becomes element b[j][i] in the transpose matrix

Raksha Puthran, Assistant Professor, SDIT


Since above transpose function takes more time, we have an alternative transpose function,
fastTranspose

Following is the function for fast Transpose of a sparse matrix:

Raksha Puthran, Assistant Professor, SDIT

You might also like