Download as pdf or txt
Download as pdf or txt
You are on page 1of 165

Course

Data Structures and Applications


18CS32

Availaible at VTU HUB (Android App)


Text Books and Reference Books
 Text Books

1. Fundamentals of Data Structures in C - Ellis Horowitz and Sartaj Sahni,


2nd edition, Universities Press,2014
2. Data Structures - Seymour Lipschutz, Schaum's Outlines, Revised 1st
edition, McGraw Hill, 2014
 Reference Books
1. Data Structures: A Pseudo-code approach with C –Gilberg &
Forouzan, 2nd edition, Cengage Learning, 2014.
2. Data Structures using c and C++ - Y.Langsam, M.J.Augenstein, A.M.
Tenenbaum

Availaible at VTU HUB (Android App)


Introduction to data structures

MODULE 1

Availaible at VTU HUB (Android App)


Outline
 Introduction to Data Structures
 Classification of Data Structures
 Data structure Operations
 Pointers and Dynamic Memory Management Functions
 Review of Arrays, Structures, Self Referential Structures and
Unions
 Representation of linear Arrays in memory
 Dynamically allocated Arrays
 Array operations and multidimensional Arrays
 Polynomial and sparse matrix
 Strings
 Programming Examples
Availaible at VTU HUB (Android App)
Terminologies Used
 Data : Data are simply values or sets of values.
 Data item : Data items refer to a single unit of values.
Data items may be Group items or Elementary item
 Information: “information” is sometimes used for data with given
attributes
 Collection of data are organized into hierarchy of Field, Record
and Files
 Entity : An entity is something that has certain attributes or
properties which may be assigned values
 Entity set : Entities with similar attributes form an entity set
SSN Name Age Sex Salary
101 Rama 32 M 30000
105 Hari 30 M 20000
106 Geetha 28 F 25000
Availaible at VTU HUB (Android App)
 Field is a single elementary unit of information representing
an attribute of an entity.
 Record is the collection of field values of a given entity.
 File is the collection of records of the entities in a given
entity set.
 Primary Keys and Keys : The value in a certain field may
uniquely determine the record in the file. Such a field K is
called a primary key and the values k1, k2, ….. in such a
field are called keys or key values
 Fixed length Records : All the records contain the data items
with the same amount of space assigned to each data item.
 In variable-length records file records may contain different
lengths Availaible at VTU HUB (Android App)
Introduction
 What is data type?
A data type is a collection of objects (data) and set of
operations that act on those objects
For example : if data is integer, the operations may be add,
subtract, multiply, divide and any other operations
appropriate for the data
 What is data structures?
Data structure is logical or mathematical model of particular
organization of data
 Goal of data structures?
For efficient processing, data are organized into some form of
structures Availaible at VTU HUB (Android App)
Classification of data structures
DATA STRUCTURES

PRIMITIVE NON PRIMITIVE

LINEAR NON LINEAR

Availaible at VTU HUB (Android App)


Primitive and Non primitive DS
 Primitive data structures are the basic data types
 Examples: int, float, char, boolean
 Non primitive data structures are the data structures created
using primitive DS
 Examples: arrays, structures, union, stacks, list, queue etc

Availaible at VTU HUB (Android App)


Linear and Non linear DS
 Non primitive data structure can be classified into linear and
non linear based on how the elements are stored
 If the values or elements are stored in sequential order, then
they are called LINEAR DS
 Examples: arrays, lists, stack, queue
 If the values or elements are stored in non sequential order
then it is called NON LINEAR DS
 Examples: trees, graphs

Availaible at VTU HUB (Android App)


Data structure operations
 Create: declaration and initialization of the data structure
 Insert: adding new records to the structure
 Delete: removing a record from the structure
 Search: finding the location of a particular record or all the
records
 Sort: arranging the records in some logical order
 Merge: combining the records from two structures into a
single structure
 Traversal: accessing each record exactly once

Availaible at VTU HUB (Android App)


Pointers
 Pointer is a derived data type that holds the address of memory
location of another variable
 That is, the actual value of a pointer type is an address of memory
 Pointer Concept
1. Pointer constant : It is memory address with in the computer
2. Pointer value : It is address of variable
3. Pointer variable : It is a variable that holds pointer value.
Representation of variable : Pointer as variable 5000 179
 int Quantity =179; int *p= &Quantity; 5048 5000

Availaible at VTU HUB (Android App)


 Two important operators : & the address operator
* the dereferencing (indirection) operator
 Pointer declaration:
data_type *pointer_variable_name;
 Example : int *p;
 Here * tells the compiler that p is a pointer variable
It can hold the address of (only) an integer variable
p needs a memory location
 Pointer Initilization:
int i=10 , *p; or int i, *p=&i
p = &i;
Here &i returns the address of i and assigns it as the value of p
 Thus p is a pointer to i and *p is value at i
 Print p gives address of i and *p gives value at i
 To assign the value to i we can write, i = 10; Or *p = 10;
Availaible at VTU HUB (Android App)
Accessing variable through pointers
 Two important operators :
 & the address operator used to find address of varible
 *the dereferencing (indirection) operator is to get the
value at address
 *p means value at address p
 &p means address of p
variable value address
int i=10 , *p; i 10 2000
p = &i; p 2000 2048

Availaible at VTU HUB (Android App)


 Null Pointer
We can set a pointer to Null ie, int *p=NULL;
When we dereference a NULL pointer, we are using address zero,
which is a valid address in computer. If we dereference pointer p
we will most likely get run time error which depends on system
that we use.
 The null pointer can be used in relational expression, where it is
interpreted as false.
Ex: if (pi = = NULL) or if (!pi)
 Pointers can be Dangerous:
Pointer can be very dangerous if they are misused. The pointers are
dangerous in following situations:
1. Pointer can be dangerous when an attempt is made to access an
area of memory that is either out of range of program or that does
not contain a pointer reference
Availaible to(Android
at VTU HUB a legitimate
App) object.
 Example:
int main ()
{
int *p;
int pa = 10;
p = &pa;
printf(“%d”, *p); //output = 10;
printf(“%d”, *(p+1)); //accessing memory which is out of range
}

Availaible at VTU HUB (Android App)


 2. It is dangerous when a NULL pointer is de-referenced,
because on some computer it may return 0 and permitting
execution to continue, or it may return the result stored in
location zero, so it may produce a serious error.
 3. In some system, 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 has proven to be a dangerous practice on some
computer and the programmer is made to define explicit
types for functions.

Availaible at VTU HUB (Android App)


Pointer Expressions
 If p1 and p2 are properly declared and initialized pointers,
Then the following statements are valid
Y=*p1* *p2 ; Sum=sum+*p1; Z=5* - *p2/ *p1;
*p2=*p2+10; P1+4; p2-2;
p1-p2; P1++; --p2;
Sum+=*p2
P1>p2 , p1==p2, p1!=p2 are valid expressions

P1/p2 , p1*p2 and p1/3 p1+p2 are not allowed- illegal

Availaible at VTU HUB (Android App)


Pointer increment and Scale factor
 When we increment a pointer, its value is increased by the length of
data type in bytes it points to. This length is called as scale factor
 The number of bytes used to store various data type depends
on the system that can be found by using sizeof operator. In IBM PC
Character 1 byte
Integer 2 bytes
Float 4 bytes
Long int 4 bytes
Double 8 bytes
Hence if p1 is an integer pointer with initial value say 5000, then p1++
or p1=p1+1 will be 5002 not 5001 and p1+2 will be 5004

if p1 is an float pointer with initial value say 5000, then p1++ or


p1=p1+1 will be 5004 not 5001 and p1+2 will be 5008

Availaible at VTU HUB (Android App)


Pointers to Pointers

 A variable which contains address of a pointer variable is


called pointer-to-pointer

Availaible at VTU HUB (Android App)


Pointers and Function
 Pointers as function arguments: We can pass address of variable as
an argument to function. Then the parameter receiving the
address should be pointer. This process of calling function to pass
address of variable and receiving as pointer is known as call by
reference
 Example: void change(int*);
int main()
{
int x=10;
change(&x);
printf(“%d\n”,x);
}
void change(int* p)
{
*p=*P+10;
} Availaible at VTU HUB (Android App)
Pointer to function
 A pointer to function is declared as data type (*fptr)();
 Example: float mul(float,float), (*fptr)();
fptr =mul;
We can call function mul as (*fptr)(x,y);
or mul(x,y);

Availaible at VTU HUB (Android App)


Memory Allocation Procedures
 Static memory allocation : Allocation of memory during
compile time Example: int x, float y;
 Dynamic memory allocation : Allocation of memory during
runtime
 There are four memory management function in C to
support dynamic memory management defined in the
header file <stdlib.h>
 1. malloc ( )
 2. calloc ( )
 3. realloc ( )
 4.free ( )
Availaible at VTU HUB (Android App)
Dynamic memory allocation Process
 Conceptual view of storage of C program in memory is shown
below
Local Variable - Stack
Free memory --Heap
Global variable -Permanent storage area
C- Program Instructions –Permanent storage area
 when ever you need a new area of memory, you may call a function
malloc () request the amount of memory you needed
 If the memory is available in the heap, a pointer to the start of an area of
memory of the requested size is returned .If the requested memory is not
available the pointer NULL is returned.
 If memory allocated is no longer required you can throw back to heap
using a function free().
 Thus size of heap shrinks and grows during program execution.
Availaible at VTU HUB (Android App)
malloc(size)
 This function allows the program to allocate single block of
memory of requested size as and when required and the exact
amount needed during execution.
 Syntax
 Declaration :
 void * malloc (size_t size);

 Function call
 ptr=(data_type *)malloc(size);

ptr->is a pointer variable of type data type.


data_type->can be any basic data type or user defined data type
size->no: of bytes required.

Availaible at VTU HUB (Android App)


Example
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *pi;
float *pf;
pi=(int *)malloc(sizeof(int));
pf=(float *)malloc(sizeof(float));
*pi=1024;
*pf=3.14;
printf(“an integer=%d,a float=%f\n”,*pi,*pf);
free(pi);
free(pf);
} Availaible at VTU HUB (Android App)
Macro to invoke malloc
 Macro substitution is a process where an identifier in a
program is replaced by a predefined string composed of
one or more tokens
 # define MALLOC(P,S)\
If((P=malloc(S))==NULL){\
printf(“Insufficient memory”);\
Exit(1);\
} also May be written as
 # define MALLOC(P,S)\
If(!(P=malloc(S))){\
printf(“Insufficient memory”);\
Exit(1);\
}

Then malloc can be invoked using code


MALLOC(pi,sizeof(int));
Availaible at VTU HUB (Android App)
calloc(n,size)
• Function is used to allocate multiple blocks of memory.
Calloc stands for contiguous allocation of multiple blocks is mainly used to allocate
memory for arrays.
The number of blocks is determined by first parameter n.
 syntax:
Prototype Declaration:
void * calloc(size_t elementcount, size_t element_size ) ;
 Function call
ptr=(data_type *)calloc(n,size);
ptr->is a pointer variable of type data_type.
data_type->basic/user defined data type .
n->no:of blocks to be allocated.
size->is the no: of bytes in each block.
 Example: ptr=(int *)calloc(5,sizeof(int)).
Availaible at VTU HUB (Android App)
calloc vs malloc

calloc is a function for dynamic memory allocation in C language stdlib.h header malloc is a function for dynamic memory allocation in C language stdlib.h header
file that allocates a specific number of bytes and initializes them to zero. file that allocates a specific number of bytes.

Meaning

calloc stands for contiguous allocation. malloc stands for memory allocation.

Syntax

calloc follows a syntax similar to void *calloc(size_t_num, size_t size); malloc follows a syntax similar to void *malloc(size_t_size);.

Number of Arguments

calloc takes two arguments. They are a number of blocks and size of each block. malloc takes one argument. It is a number of bytes.

Speed

calloc takes little longer than malloc. That is because of the extra step of
malloc is faster than calloc.
initializing the allocated memory by zero.

Availaible at VTU HUB (Android App)


realloc(ptr,size)
 Before using this function,the memory should have been allocated using
malloc(),calloc().
 Sometimes,the allocated memory may not be sufficient and we may requires
additional memory space.
 Sometimes allocated memory may be much larger and we want to reduce the size
of allocated memory.
 In both of the above situations,the size of allocated memory can be changed using
realloc() and the process is called reallocation of memory.
 Syntax:
 Prototype Declaration : void * realloc( void * ptr, size_t newsize);
 Function call : ptr=(data_type *)realloc(ptr,size)
Example:
ptr=(int *)realloc(ptr,1*sizeof(int))
ptr=(int *)realloc(ptr,4);
Availaible at VTU HUB (Android App)
free(ptr)
This function is used to de-allocate or free the allocated block
of memory which is allocated by using the functions
calloc(),malloc() and realloc()
 Prototype declaration
 void free(void * ptr);
 Example:
#include<stdlib.h>
-------------------------
-------------------------
free(ptr);
ptr=NULL;

Availaible at VTU HUB (Android App)


Arrays
 Array is a collection of items of the same data types that share a common
name.
 Each data items can be accessed using the same name but different index
value.
 General form : data_type array_name[size];
 An array is a set of pairs, <index, value >, such that each index has a value
associated with it. It can be called as corresponding or a mapping
 Ex: <index, value>
 < 0 , 25 > list[0]=25
 < 1 , 15 > list[1]=15
 < 2 , 20 > list[2]=20
 < 3 , 17 > list[3]=17
 < 4 , 35 > list[4]=35
 Here, list is the name of array. By using, list [0] to list [4] the data items in
list can be accessed. Availaible at VTU HUB (Android App)
Arrays in C
 A one dimensional array in C is declared by adding brackets
to the name of a variable.

Availaible at VTU HUB (Android App)


Examples
int list[5], *plist[5];

int list[5]: Means list is an array of five integers


list[0], list[1], list[2], list[3], list[4]

int *plist[5]: Means plist is an array of five pointers to integers


plist[0], plist[1], plist[2], plist[3], plist[4]

int (*plist)[5] : Means plist is pointer to array of 5 integers.

implementation of 1-D array


list[0] base address = 
list[1]  +1* sizeof(int)
list[2]  + 2*sizeof(int)
list[3]  + 3*sizeof(int)
list[4]  + 4*size(int)
Availaible at VTU HUB (Android App)
 Implementation:
 When the complier encounters an array declaration, list[5], it
allocates five consecutive memory locations. Each memory is enough
large to hold a single integer.
  The address of first element of an array is called Base Address.
Ex: For list[5] the address of list[0] is called the base address.
  If the memory address of list[i] need to compute by the compiler,
then the size of the int would get by sizeof (int), then memory
address of list[i] is as follows:
 list[i] = α + i * sizeof (int) Where, α is base address.
 For example, an array of 10 integer variables, with indices 0 through 9,
may be stored as 10 words at memory addresses 2000, 2004, 2008,
2036, so that the element with index i has the address 2000 + 4× i.

Availaible at VTU HUB (Android App)


Compare int *list1 and int list2[5] in C.

list1 and list2 are pointers.


Difference: list2 reserves five locations.

Notations:
list2 is a pointer to list2[0]
(list2 + i) is a pointer to list2[i], that is &list2[i]
*(list2 + i) is nothing but list2[i]

Availaible at VTU HUB (Android App)


Example: 1-dimension array addressing
Let int one[ ] = {0, 1, 2, 3, 4};
To print address and value
Function call is print1(one,5);

void print1(int *ptr, int size)


{
/* print out a one-dimensional array using a pointer */
int i;
printf(“Address Contents\n”);
for (i=0; i < size; i++)
printf(“%u%d\n”, ptr+i, *(ptr+i));
printf(“\n”);
}

Availaible at VTU HUB (Android App)


Address Contents
1228 0
1230 1
1232 2
1234 3
1236 4

Availaible at VTU HUB (Android App)


Address Calculation in single (one) Dimension Array:

An element of an array say ―A[ i ]‖ is calculated using the following formula:


Address of A [i ] = B +W * ( i – LB )
Where,
B = Base address
W = Storage Size of one element stored in the array (in byte)
i = Subscript of element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)
Availaible at VTU HUB (Android App)
Example:
Given the base address of an array B[1300…..1900] as 1020 and size of each element is 2
bytes in the
memory. Find the address of B[1700].
Solution:
The given values are: B = 1020, LB = 1300, W = 2, I = 1700
Address of A [ I ] = B +W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820 [Ans]

Availaible at VTU HUB (Android App)


Address Calculation in Double (Two) Dimensional Array:
 While storing the elements of a 2-D array in memory, these
are allocated contiguous memory locations. Therefore, a 2-D
array must be linearized so as to enable their storage. There
are two alternatives to achieve linearization: Row-Major and
Column-Major.

Availaible at VTU HUB (Android App)


• C allows for arrays of two or more dimensions. A two-dimensional (2D) array is an array of arrays. A three-
dimensional (3D) array is an array of arrays of arrays.
In C programming an array can have two, three, or even ten or more dimensions. The maximum dimensions a
C program can have depends on which compiler is being used.
More dimensions in an array means more data be held, but also means greater difficulty in managing and
understanding arrays.
Availaible at VTU HUB (Android App)
 How to Declare a Multidimensional Array in C
 A multidimensional array is declared using the following syntax:
 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];
 In Example 1:
  int designates the array type integer.
  table is the name of our 3D array.
  Our array can hold 500 integer-type elements. This number is
reached by multiplying the value of each dimension. In this case:
5x5x20=500.
Availaible at VTU HUB (Android App)
 In Example 2:
  Array arr is a five-dimensional array.
  It can hold 4500 floating-point elements
(5x6x5x6x5=4500).
 When it comes to holding multiple values, we would need to
declare several variables. But a single array can hold
thousands of values.
 Explanation of a 3D Array
 A 3D array is essentially an array of arrays of arrays: it's an
array or collection of 2D arrays, and a 2D array is an array of
1D array.
Availaible at VTU HUB (Android App)
Diagram below shows a 3D array representation:
3D Array Conceptual View

3D array memory map.

Availaible at VTU HUB (Android App)


 struct Student
{
int Roll_No;
char name[10];
char department[5[;
float GPA;
};
struct student s1,s2,s3,s4,s5;

Availaible at VTU HUB (Android App)


Structures
 Structure is a collection of data items, where each item can
be of same or different data type.
 Consider a Student data
Roll_No Name Department GPA
1 Ravi CSE 8.5

 Data are of not same type.


 In such case we define data type called structure

Availaible at VTU HUB (Android App)


Three ways of defining structure and Declaring variables

 Structure with tag _name


 Structure with out tag _name
 Structure with typedef statement

Availaible at VTU HUB (Android App)


Structure with tag _name -Syntax with Example
 Syntax : struct tag_name
{
data-type member_1;
data-type member_2;
.
data-type member_n;
};
Struct tag_name variable name/s;

 Example : struct Employee


{
char name[10];
int age;
float salary;
};
struct Employee E1,E2;
Availaiblevariables
Note: E1 and E2 are structure at VTU HUB (Android App)
Structure without tag _name - Syntax with Example
 Syntax : struct
{
data-type member_1;
data-type member_2;
.
data-type member_n;
} variable name/s;

 Example : struct
{
char name[10];
int age;
float salary;
} E1,E2;
Note: E1 and E2 are structure variables

Availaible at VTU HUB (Android App)


Structure with typedef statement - Syntax with Example
 Syntax : typedef struct
{
data-type member_1;
data-type member_2;
.
data-type member_n;
} NAME;
NAME variable name/s;

 Example : typedef struct


{
char name[10];
int age;
float salary;
} EMPLOYEE;
EMPLOYEE E1,E2;
Note: E1 and E2 are structure variables
Availaible at VTU HUB (Android App)
Accessing members of structures
 To access he members of the structure dot (.) operator is
used.
 Syntax: variable name . member_name
 For the employee structure shown above E1 and E2 are
structure variables and values to the members may be
assigned as given below
 strcpy(E1.name, “ravi”);
 E1.age = 30;
 E1.salary = 50000;
 These values may be accessed in the program as and when
required using the same syntax in the RHS of expression

Availaible at VTU HUB (Android App)


 #include<stdio.h>
struct student
{
int Rollno;
char name[10];
char dept[5];
float GPA;
};
int main()
{
int i;
struct student s1;
printf("enter student details\n");
scanf("%d%s%s%f",&s1.Rollno,s1.name,s1.dept,&s1.GPA);
printf("Student information\n");
printf("Roll No->%d\nName->%s\ndept->%s\nGPA->% f", s1.Rollno, s1.name,
s1.dept,s1.GPA);
} Availaible at VTU HUB (Android App)
Array of structures
 It is also possible to define an array of structures that is an array
in which each element is a structure.
The procedure is shown in the following example:
struct student
{
char name [80];
int roll_no ;
float marks ;
} s [100];
 In this declaration s is a 100- element array of structures.
 It means each element of s represents an individual student
record.
Availaible at VTU HUB (Android App)
Structure Operation
 Comparison
if (strcmp(E1.name,E2.name))
printf(“Employees names are not same”);
Else
printf(“Employees names are same”);
 Assignment
 ANSI C permits assignment of structure variables of same
structures, ie, E1=E2; is valid operation if both E1 and E2
are variables of same structure
 Checking for Equality
If(E1==E2) is not supported in C

Availaible at VTU HUB (Android App)


Structure Operation
 1. Structure Equality Check:
 Here, the equality or inequality check of two structure variable of
same type
typedef struct
{
char name[10];
int age;
float salary;
}humanBeing;
humanBeing person1, person2;
if (person1 = = person2) is invalid.
The valid function is shown next
Availaible at VTU HUB (Android App)
#define FALSE 0
#define TRUE 1
if (humansEqual(person1,person2))
printf("The two human beings are the same\n");
else
printf("The two human beings are not the same\n");
 int humansEqual(humanBeing person1, humanBeing person2)
{ /* return TRUE if person1 and person2 are the same otherwise return FALSE */
if (strcmp(person1.name, person2.name))
return FALSE;
if (person1.age != person2.age)
return FALSE;
if (person1.salary != person2.salary)
return FALSE;
return TRUE;
Availaible at VTU HUB (Android App)
Structure within structure
There is possibility to embed a structure within a structure. There are 2 ways to embed structure.

typedef struct {
int month;
int day;
int year;
} DATE;

typedef struct {
char name[10];
int age;
float salary;
DATE dob;
} HUMAN_BEING;

HUMAN_BEING person1;
person1.dob.month = 2;
person1.dob.day = 11;
person1.dob.year = 1944;
Availaible at VTU HUB (Android App)
Self Referential Structures
One or more of its components is a pointer to itself.

typedef struct {
char data; Construct a list with three nodes
struct list *link; item1.link=&item2;
} list ; item2.link=&item3;
malloc: obtain a node
list item1, item2, item3;
item1.data=„a‟;
a b c
item2.data=„b‟;
item3.data=„c‟;
item1.link = item2.link = item3.link = NULL;

Availaible at VTU HUB (Android App)


Union
 Union is the concept borrowed from structure.
 Union declaration, initialization and accessing the members is
same as that of structure.
 The difference is only the keyword is union and the fields of
the union share the memory location.
 That is only one field of union is active at any given time.
 The memory allocation for union variable is the amount of
storage necessary to represent the largest component.
 Example : union code {
int m;
char c;
float f; }c1;
Allocates only 4 bytes to hold the float f value. The same location is used by the
other members in different instances. It is not possible to use all the members at
Availaible at VTU HUB (Android App)
the same time.
Memory allocation procedure in union

4 bytes

Availaible at VTU HUB (Android App)


Application of union for saving the memory
typedef struct {
enum tagField {female, male} sex;
union {
int children;
int beard;
}u;
}sextype;
typedef struct {
char name[10];
int age;
sextype sexinfo;
}humanBeing;
humanBeing person1, person2;

Availaible at VTU HUB (Android App)


We could assign values to person1 and person2 as follows:
person1.sexinfo.sex = male;
person1.sexinfo.u.beard = FALSE;
And
person2.sexinfo.sex = female;
person2.sexinfo.u.children = 4;

Availaible at VTU HUB (Android App)


Internal implementation of structures
 Consider the structure
 struct { int i; int j; float a; float b;}code1,code2;
 Values will be stored in the same way using increasing
addresses in the order specified in the structure notation. But
structures must begin and end on the same type of memory
boundary.
 For an even byte boundary address is multiple of 4, 8or16.
Since variable code1 takes only 12 bytes the remaining 4
bytes are padded with zeros ( holes ) so that the next variable
code2 value starts from the 16th byte.

Availaible at VTU HUB (Android App)


Pointer to Structure
 Example : struct Employee
{
char name[10];
int age;
float salary;
};
struct Employee E1, *ptr ;
ptr=&E1; Then ptr will point to E1.
Members can be accessed using arrow operator ie, ptr->age is same as E1.age
ptr->age same as that (*ptr).age
Note: Operators ->, . And [] enjoys highest priority among operators.

Availaible at VTU HUB (Android App)


Availaible at VTU HUB (Android App)
Linear Arrays
 A linear Array is a list of a finite number n of homogeneous
data elements which are referenced by an index set consisting
of n consecutive numbers.
 The number of data elements of the array is known as length
or size of array that can be obtained from the index set by the
formula
Length = UB-LB+1 where UB is largest index and LB is smallest index.
The elements of array A may be denoted by
A1,A2,A3,……,An in subscript notation,
A(1),A(2),A(3),……..A(N) in parenthesis notation used in
FORTRAN,PL/1 and BASIC,
A[1],A[2],A[3],……..A[N] in bracket notation used in PASCAL
,C and C++ where A[K] is called as subscripted variable and
K is subscript or index.
Availaible at VTU HUB (Android App)
Examples to Linear Arrays
 Each Programming Language has its own rules for declaring
the arrays. Suppose DATA is a 6-element linear array
containing real values.
Programming Language Declaration
FORTRAN REAL DATA(6)
PL/1 DECLARE DATA(6) FLOAT
PASCAL VAR DATA: ARRAY[1…6]OF REAL
C Float DATA[6]

 Suppose AUTO is an Integer Array with LB=1932 and UB=1984


Programming Language Declaration

FORTRAN 77 INTEGER AUTO(1932:1984)


PL/1 DECLARE AUTO(1932:1984) FIXED
PASCAL VAR AUTO: ARRAY[1932…1984] of
INTEGER
C Not possible directly
Availaible at VTU HUB (Android App)
Representation of Linear Arrays in memory
 Let LA be the linear arrays in memory of Computer.
 LOC(LA[K]) is address of element LA[K] of Array LA
 Address of first element of LA is known as Base address
Base(LA) and computer calculates the address of any element of
LA by the formula
LOC(LA[K])=Base(LA)+w(K- lower bound) where w is
number of bytes per word.
For example : In an array declared in C as
int average[5] = {8,3,10,2,4} Array index starts at zero ,The
base address is & average[0] and let it be 2000
Then address of average[3]is 2000+2*(3-0) =2006
Note: Time to access LOC(LA[K]) is same for any value of K
Availaible at VTU HUB (Android App)
Array - Operations
 Traverse − print all the array elements one by one.
 Insertion − add an element at given index.
 Deletion − delete an element at given index.
 Search − search an element using given index or by value.
 Update − update an element at given index.
 Merge – combining the elements from two arrays into a
single structure
 Sort – arranging the records in some logical order

Availaible at VTU HUB (Android App)


Traversing
 Algorithm: TRAVERSING(LA, N,LB,UB)
Here LA is linear Array with N elements, UB is largest index
and LB is smallest index and PROCESS is an operation
we apply as indication of visited.
 1.[Initialize counter] set K:=LB
 2.Repeat steps 3 and 4 while K<=UB
 3. [Visit element] Apply PROCESS to LA[K]
 4. [Increase counter] Set K:=K+1
 [End of Step 2 loop]
5.EXIT

Availaible at VTU HUB (Android App)


Alternate form of Traversal Algorithm
 Algorithm: TRAVERSING(LA, N,LB,UB)
 Here LA is linear Array with N elements, UB is largest index
and LB is smallest index and PROCESS is an operation we apply as
indication of visited.
 1. Repeat for K=LB to UB
 Apply PROCESS to LA[K]
 [End of loop]
2.EXIT

Availaible at VTU HUB (Android App)


Insertion
 Insert operation is to insert one or more data elements into
an array.
 New element can be added –
 At the beginning
 At the end or
 At any given index of array.

Availaible at VTU HUB (Android App)


Insertion at the beginning
199

10 20 30 40 50

0 1 2 3 4 5 …..?????

Availaible at VTU HUB (Android App)


Insertion at the end
199

10 20 30 40 50

0 1 2 3 4 5 …..?????

Availaible at VTU HUB (Android App)


Insertion at a given index
let index be 2
199

10 20 30 40 50

0 1 2 3 4 5

…..?????

Availaible at VTU HUB (Android App)


Algorithm
INSERT(LA,N,K,ITEM)
Here LA is linear Array[0…N-1] with N elements and K is the
position to insert the element such that K<=N and ITEM
is the element to be inserted in Kth position
1. [Initialize counter] Set J:= N-1
2. Repeat steps 3 and 4 while J >=K
3. [Move Jth element Downwards]Set LA[ J+1 ]=LA[ J ]
4. [Decrease counter] Set J = J-1
[End of step 2 loop]
5. [Insert Element] Set LA[ K ] = ITEM
6. [Reset N] Set N = N+1
7. EXIT
Availaible at VTU HUB (Android App)
Deletion
 Element can be deleted –
 From the beginning
 At the end or
 At any given index of array.

Availaible at VTU HUB (Android App)


Deletion : front of the array

10 20 30 40 50

0 1 2 3 4 5
…..?????

Availaible at VTU HUB (Android App)


Deletion : end of the array

10 20 30 40 50

0 1 2 3 4 5

…..?????

Availaible at VTU HUB (Android App)


Deletion : at given index

10 20 30 40 50

0 1 2 3 4 5

…..?????

Availaible at VTU HUB (Android App)


Algorithm
DELETE(LA,N,K,ITEM)
Here LA is linear Array[0…N-1] with N elements and K is the
position to delete the element such that K<=N-1 and
ITEM is the element deleted from Kth position
1. Set ITEM:=LA[K]
2. Repeat for J =K to N-2
3. [Move J + 1st element Upwards]Set LA[ J ]=LA[ J +1 ]
[End of loop]
4. [Reset N] Set N = N-1
5. EXIT

Availaible at VTU HUB (Android App)


bubble sort
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6

77 42 35 12 101 5

CS 307 Fundamentals of Computer


83 Science
Availaible at VTU HUB (Android App)
bubble sort
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6
42Swap
77 77
42 35 12 101 5

CS 307 Fundamentals of Computer


84 Science
Availaible at VTU HUB (Android App)
bubble sort
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6

42 35Swap35
77 77 12 101 5

CS 307 Fundamentals of Computer


85 Science
Availaible at VTU HUB (Android App)
bubble sort
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6

42 35 12Swap12
77 77 101 5

CS 307 Fundamentals of Computer


86 Science
Availaible at VTU HUB (Android App)
bubble sort
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6

42 35 12 77 101 5

No need to swap
CS 307 Fundamentals of Computer
87 Science
Availaible at VTU HUB (Android App)
bubble sort
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 Swap101
101 5

CS 307 Fundamentals of Computer


88 Science
Availaible at VTU HUB (Android App)
bubble sort
 Traverse a collection of elements
 Move from the front to the end
 “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed


CS 307 Fundamentals of Computer
89 Science
Availaible at VTU HUB (Android App)
Sorting algorithm
 BUBBLE ( DATA, N)
 DATA is an array with N elements. This algorithm sorts the
elements in DATA
1. Repeat step 2 and 3 for K=1 to N-1
2. Set PTR:=1 [initialize pass pointer PTR]
3. Repeat while PTR <= N-K [ execute pass]
If DATA [PTR] > DATA[PTR +1], then
interchange DATA[PTR] and DATA[PTR +1]
[end of if structure]
set PTR := PTR+1
[End of inner loop]
[End of step 1 outer loop]
4. Exit Availaible at VTU HUB (Android App)
Sort
Initial

20 10 40 5

Availaible at VTU HUB (Android App)


Sort
First pass

20 > 10 40 5

Availaible at VTU HUB (Android App)


Sort
Second pass

10 > 20 5 40

Availaible at VTU HUB (Android App)


Sort
Third pass

10 > 5 20 40

Availaible at VTU HUB (Android App)


 Complexity of Bubble sort algorithm can be found by
counting number of comparison made =
 (n-1)+(n-2)+(n-3)+……..+2+1=n(n-1)/2 =n*n= n2

Availaible at VTU HUB (Android App)


Searching
 Let DATA be a collection of data elements in memory.
 Suppose a specific ITEM of information is given.
 Searching refers to the operation of finding the location LOC of
ITEM in DATA, or printing some message that ITEM present/ does
not appear there.
 The search is said to be successful if ITEM does appear in DATA and
unsuccessful otherwise. Two approaches of searching are
1. Linear search
2. Binary search
 Linear Search : Suppose DATA is a linear array with n elements.
One of the way to search for a given ITEM in DATA is to compare
ITEM with each element of DATA one by one. That is, first test
whether DATA [l] = ITEM, and then test whether DATA[2] = ITEM,
and so on. This method, which traverses DATA sequentially to locate
ITEM, is called linear search
Availaible or HUB
at VTU sequential search.
(Android App)
Search
KEY to be searched is 30

30

10 20 30 40 50

0 1 2 3 4 5

How to
search…..?????

Availaible at VTU HUB (Android App)


Linear Search
 LINEAR ( DATA, N, ITEM, LOC)
 DATA is an array with N elements. ITEM is the key to be searched. Algorithm
finds location LOC of ITEM in DATA or sets LOC = -1 if the search is
unsuccessful
1. [Insert ITEM at the end of DATA] Set DATA[N+1]:= ITEM
2. [initialize counter] Set LOC := 1
3. [search for ITEM]
Repeat while DATA [ LOC ] ≠ ITEM
Set LOC:=LOC+1
[End of loop]
4. If LOC = N + 1 Set LOC := -1
5. Return LOC

Availaible at VTU HUB (Android App)


Complexity of the Linear Search Algorithm
 Worst Case: The worst case occurs when one must search
through the entire array DATA, i.e., when ITEM does not
appear in DATA or ITEM is last element in the given list. In
this case, the algorithm requires comparisons f(n) = n + 1 or
f(n)=n respectively. Thus, in the worst case, the running time
is proportional to n.
 Average Case: The average number of comparisons required to
find the location of ITEM is approximately equal to half the
number of elements in the array. f(n)= (𝑛+1)/2
 Best Case : The Best case Complexity=1

Availaible at VTU HUB (Android App)


Binary Search- Procedure
 Suppose DATA Array is sorted, then to find Location of ITEM in
DATA array binary search is an efficient searching algorithm
 List DATA[LB:UB] is single sorted segment , ITEM is to be searched ,
Set BEG=LB and END=UB
 Step1: While BEG<=END find mid=(BEG+END)/2,
Compare DATA[mid] with ITEM to be searched
There are 3 possible cases
1. If DATA[mid] equals to ITEM then set LOC=mid and display message-
Item found at LOC position and return LOC
2. If ITEM<DATA[mid] set END=mid-1 repeat step 1
3. If ITEM>DATA[mid] set BEG=mid+1 repeat step 1
Step 2: LOC=-1,Display-Item not found and return LOC

Availaible at VTU HUB (Android App)


Binary Search
 Let ITEM=31,
 BEG=0,END= 9

Availaible at VTU HUB (Android App)


ITEM=31, BEG=0,END= 9
 mid=(BEG+END)/2=4 ,

Availaible at VTU HUB (Android App)


ITEM=31, mid=4
 ITEM>a[mid] Hence BEG=mid+1=5, END= 9

Availaible at VTU HUB (Android App)


ITEM=31, BEG=5,END= 9
 mid=(BEG+END)/2=7

Availaible at VTU HUB (Android App)


ITEM=31, mid=7
 ITEM<a[mid] Hence END=mid-1=6, BEG= 5,

Availaible at VTU HUB (Android App)


ITEM=31, BEG=5,END= 6
 mid=(BEG+END)/2=5

Availaible at VTU HUB (Android App)


ITEM=31,mid=5
 ITEM=a[mid], Key found at location 5

Availaible at VTU HUB (Android App)


BINARY SEARCH
 BINARY ( DATA, LB,UB, ITEM, LOC)
 DATA is an sorted array with lower bound LB and upper bound UB and ITEM
is the key to be searched. The variable BEG and END and MID denotes the
beginning, end and middle location of segment of elements of DATA
respectively. Algorithm finds location LOC of ITEM in DATA or sets LOC =
NULL if the search is unsuccessful
1. [initialize segment variables] Set BEG := LB, END := UB
2. Repeat steps 3 to 5 while BEG <= END
3. Set MID := INT((BEG +END)/2)
4. if DATA[MID]= =ITEM then Set LOC=MID return LOC
[End of if structure]
5. If ITEM < DATA[MID] then Set END := MID-1
else Set BEG := MID +1
[End of if structure]
[End of step 2loop]
6. Set LOC:= NULL
7. Return LOC Availaible at VTU HUB (Android App)
Complexity of Binary search
 The running time for the worst case is Log2n

 One can also show that the running time for the average case
is approximately equal to the running time for the worst case
that is Log2n

 For Best case running time=1

Availaible at VTU HUB (Android App)


Merge
10 30 20 40

Availaible at VTU HUB (Android App)


DYNAMICALLY ALLOCATED ARRAYS
 One Dimensional Array
If we cannot determine how large an array to use, we may defer this
decision to run time and allocate the array dynamically.
 Example:
int n, *list;
printf(“Enter the number of numbers in array”);
scanf(“%d”, &n);
if((list=(int*)malloc(n*size of(int))==Null)
{
printf (“No memory to allocate”);
exit(0);
} //The programs fails only when insufficient memory to hold the
list of numbers . Availaible at VTU HUB (Android App)
Two Dimensional Arrays
 C uses array-of-arrays representation to represent a multidimensional array.
The two dimensional arrays is represented as a one-dimensional array in
which each element is itself a one-dimensional array.
 Example: int x[3][5];

 Consider Array-of-arrays representation of mXn matrix


 C find element x[i][j] by first accessing the pointer in x[i].
 Where x[i] = α+w*n*(i-LB), which give the address of the zeroth
element of row i of the array.
 Then adding w*(j-LB) to this pointer ( x[i] ) , the address of the [j]th
element of row i is determined.
 x[i][j] = x[i]+ w*(j-LB)
Availaible at VTU HUB (Android App)
Creation of Two-Dimensional Array Dynamically
int **myArray;
myArray = make2dArray(3,5);
myArray[2][3]=6;
int ** make2dArray(int rows, int cols)
{ /* create a two dimensional rows X cols array */
int **x, i;
x=malloc( rows * sizeof (*x)); /*get memory for row pointers*/
for (i= 0;i<rows; i++) /* get memory for each row */
x[i]=malloc( cols * sizeof(**x));
return x;
}
The second line allocates memory for a 3 by 5 two-dimensional array of
integers and the third line assigns the value 6 to the [2][3] element of
this array. Availaible at VTU HUB (Android App)
Multidimensional arrays
 Two-dimensional arrays
 A two dimensional m x n array A is a collection of m * n data
elements such that each element is specified by a pair of
integers, called subscripts, with the property that,
 1<= J<=m and 1<=K<=n
 Two dimensional arrays are called matrices or tables or
matrix arrays

Availaible at VTU HUB (Android App)


 Suppose A is a two dimensional m x n array, the first
dimension of A contains the index set 1…….m, with lower
bound 1 and upper bound m
 The second dimension of A contains the index set 1…..n,
with lower index 1 and upper index n
 m X n is called the size of the array
 Some programming languages allow to define
multidimensional arrays in which lower bound is not 1 ( non-
regular arrays)
 Length of the dimension can be obtained from the formula
 Length = upper bound – lower bound +1
Availaible at VTU HUB (Android App)
Representation of 2D arrays in
memory
 Array will be represented in memory by a block of m.n
sequential memory locations
 The programming language will store the array A either
column-major order or row major order
 Storing the elements column by column is column major,
 Storing the elements row by row is row major order

Availaible at VTU HUB (Android App)


Availaible at VTU HUB (Android App)
 Address of any element A[ J,K ] can be calculated as follows
 Row-major
LOC( A[ J, K ]) = base(A) + w ( N( J-LB ) + ( K-LB ) )
 Column major
LOC( A[ J, K ]) = base(A) + w ( M( K-LB ) + ( J-LB ))

Availaible at VTU HUB (Android App)


General multidimensional arrays
 A n- dimensional array B is m1Xm2Xm3X……Xmn is a
collection of m1.m2.m3…..mn data elements.
 For example B[2][4][3] is a 3 dimensional array with
2*4*3=24 data elements is shown below

Availaible at VTU HUB (Android App)


Availaible at VTU HUB (Android App)
POLYNOMIALS
 “A polynomial is a sum of terms, where each term has a form aixei,
where x is the variable, ai is the coefficient and ei is the exponent an
integer >=0.”
 Two example polynomials are:
A(x) =3x20 + 2x5 + 4
B(x) =x4 + 10x3 + 3x2 +1
 The largest (or leading) exponent of a polynomial is called its degree.
Coefficients that are zero are not displayed. The term with exponent
equal to zero does not show the variable since x raised to a power of
zero is 1.
 Assume there are two polynomials,
A(x) = Σ ai xi
B (x) =Σ bi xi
 then: A(x) + B(x) = Σ (ai + bi) xi
A(x).B(x) = Σ (ai xi. ΣAvailaible
(bi xi)) at VTU HUB (Android App)
Polynomial Representation
 A Polynomial may be sparse or dense polynomial
 If number of non zero coefficient is small relative to degree of Polynomial, it is called as
sparse polynomial.
Example : A(x) =3x20 + 2x5 + 4
 If number of non zero coefficient is large relative to degree of Polynomial, it is called as
dense polynomial.
Example : B(x) =x4 + 10x3 + 3x2 +1
Polynomial may be represented using structures. There are two approaches
1. #define MAX-DEGREE 101 /*Max degree of polynomial+1*/
typedef struct
{
int degree;
float coef [MAX-DEGREE];
} polynomial;
polynomial a,b;
 Now a and b are two polynomials of degree n < MAX_DEGREE. This representation is
suitable for dense polynomial and will waste lot of space for sparse polynomial .
Availaible at VTU HUB (Android App)
Polynomial Addition
 Let A(x) =3x6 + 2x5 +2x2+ 4
B(x) =x4 + 10x3 + 3x2 +1
Then D(x)=A(x)+B(x)= 3x6 + 2x5 + x4 + 10x3 + 5x2+ 5
data structure :
#define MAX-DEGREE 101 /*Max degree of polynomial+1*/
typedef struct
{
int degree;
float coef [MAX-DEGREE];
} polynomial;
polynomial a,b;

Availaible at VTU HUB (Android App)


Function to add two Polynomials
 Function IsZero(poly) return TRUE if Polynomial has no terms.
 Function Coef(poly, Lead_exp(Poly)) return coefficient of Polynomial of its largest expon
 Function Lead_Exp(poly) return degree of polynomial
 Function Attach(d, coef(b,Lead_Exp(b)), Lead_exp(b)) append term of b to d
 Function remove(b,Lead_Exp(b)) remove specified term from poly b
 /* d =a + b, where a, b, and d are polynomials */
d = Zero( )
while (! IsZero(a) && ! IsZero(b)) do {
switch COMPARE (Lead_Exp(a), Lead_Exp(b)) {
case -1: d =
Attach(d, Coef (b, Lead_Exp(b)), Lead_Exp(b));
b = Remove(b, Lead_Exp(b));
break;
case 0: sum = Coef (a, Lead_Exp (a)) + Coef ( b, Lead_Exp(b));
if (sum) {
Attach (d, sum, Lead_Exp(a));
a = Remove(a , Lead_Exp(a));
b = Remove(b , Lead_Exp(b));
Availaible at VTU HUB (Android App)
 case 1: d =
Attach(d, Coef (a, Lead_Exp(a)), Lead_Exp(a));
a = Remove(a, Lead_Exp(a));
}
}
insert any remaining terms of a or b into d
 advantage: easy implementation
 disadvantage: waste space when sparse

Availaible at VTU HUB (Android App)


Sparse Polynomial Representation
 An alternate representation that uses only one global array, terms to store all polynomials
That is, it uses array of structures .The C declarations needed are:
#define MAX_TERMS 100 /*size of terms array*/
typedef struct
{
float coef;
int expon;
} polynomial;
polynomial terms[MAX-TERMS]; int avail = 0;
 Consider the two polynomials : A(x) = 2xl000+ 1 and B(x) = x4 + 10x3 + 3x2 + 1
These Polynomials are stored in array as given below

Availaible at VTU HUB (Android App)


A(x) = 2xl000+ 1
B(x) = x4 + 10x3 + 3x2 + 1
D(x) =

Start A Finish A Start B


Finish B Avail

Coef 2 1 1 10 3 1
Expon 1000 0 4 3 2

0 1 2 3 4 5 6 7 8 9 10 11 12 13

Availaible at VTU HUB (Android App)


Polynomial Addition
 C function is written that adds two polynomials, A and B to obtain D =A + B.
 To produce D (x), padd( ) is used to add A (x) and B (x) term by term.
 Starting at position avail, attach( ) which places the terms of D into the array, terms.
 void padd(int startA, int finishA, int startB, int finishB, int *startD,int *finishD)
{ /* add A(x) and B(x) to obtain D(x) */
float coefficient;
*startD = avail;
while (startA <= finishA && startB <= finishB)
switch(COMPARE(terms[startA].expon, terms[startB].expon))
{
case -1: attach (terms [startB].coef, terms[startB].expon); // a expon < b expon
start B++;
break;
 case 1: attach (terms [startA].coef, terms[startA].expon); // a expon > b expon
start A++;
break; Availaible at VTU HUB (Android App)
case 0: coefficient = terms[startA].coef + terms[startB].coef;
if (coefficient)
attach (coefficient, terms[startA].expon);
startA++;
startB++;
}
/* add in remaining terms of A(x) */
for(; startA <= finishA; startA++)
attach (terms[startA].coef, terms[startA].expon);
/* add in remaining terms of B(x) */
for( ; startB <= finishB; startB++)
attach (terms[startB].coef, terms[startB].expon);
*finishD = avail-1;
}

Availaible at VTU HUB (Android App)


void attach(float coefficient, int exponent)
{ /* add a new term to the polynomial */
if (avail >= MAX-TERMS)
{
fprintf(stderr,"Too many terms in the polynomial\n");
exit(EXIT_FAILURE);
}
terms[avail].coef = coefficient;
terms[avail++].expon = exponent;
}

Availaible at VTU HUB (Android App)


Analysis of padd( ):
 The number of non-zero terms in A and B is the most important factors in
analyzing the time complexity.
 Let m and n be the number of non-zero terms in A and B, If m >0 and n > 0,
the while loop is entered. Each iteration of the loop requires O(1) time. At
each iteration, the value of startA or startB or both is incremented. The
iteration terminates when either startA or startB exceeds finishA or finishB.
 The number of iterations is bounded by m + n -1
 The time for the remaining two for loops is bounded by O(n + m) because we
cannot iterate the first loop more than m times and the second more than n
times. So, the asymptotic computing time of this algorithm is O(n +m).

Availaible at VTU HUB (Android App)


Matrix-Representation
 A matrix contains m rows and n columns of elements
 We write m x n (read "m by n") to designate a matrix with m
rows and n columns.
 The total number of elements in such a matrix is m.n. If m
equals n, the matrix is square.
 Sparse matrix :A matrix in which number of non-zero
elements is relatively less in a matrix.
 Dense matrix : A matrix in which number of zero elements is
relatively less in a matrix.

Availaible at VTU HUB (Android App)


Dense and Sparse Matrix

col1 col2 col3 col4 col5 col6


row0 15 0 0 22 0  15
 0 11 3 0 0 0 
row1  
row2  0 0 0 6 0 0
 
row3  0 0 0 0 0 0 
91 0 0 0 0 0
row4
 
row5
 0 0 28 0 0 0

Dense matrix
sparse matrix

Availaible at VTU HUB (Android App)


Matrix Representation
 In general Matrices are represented by a 2D array.
 But there is wastage of space if 2D array representation is used for
sparse matrix. Hence sparse matrix is represented as array of triples.
 In triples representation, each element is characterized by
<row,col,value>
 The triples should be in ascending order of rows and column in the
row should be in ascending order within that row.
 To know the number of rows, number of columns and number of non-
zero elements in the matrix, this information is also stored in triple
representation
 That is if “a” is array of triples a[0].row contains the number of
rows, a[0].col contains the number of columns and a[0].value
contains the total number of nonzero entries.
Availaible at VTU HUB (Android App)
Sparse Matrix Representation- Data structures
 #define MAX_TERMS 101 /* maximum number of terms +1*/
typedef struct {
int col;
int row;
int value;
} term;
term a[MAX_TERMS];

Availaible at VTU HUB (Android App)


Triple representation of sparse matrix

col1 col2 col3 col4 col5 col6 < row column Value>
15 0 0 22 0  15
row0

row1  0 11 3 0 0 0 
 
row2  0 0 0 6 0 0
 
row3  0 0 0 0 0 0 
row4
91 0 0 0 0 0
 
row5
 0 0 28 0 0 0

Availaible at VTU HUB (Android App)


Transposing a Matrix
 To transpose a matrix, interchange the rows and columns. This means that each element
a[i][j] in the original matrix becomes element a[j][i] in the transpose matrix.
 A good algorithm for transposing a matrix:
for each row i
take element <i, j, value> and store it as
element <j, i, value> of the transpose;
 If we process the original matrix by the row indices it is difficult to know exactly where
to place element <j, i, value> in the transpose matrix until we processed all the elements
that precede it.
 This can be avoided by using the column indices to determine the placement of
elements in the transpose matrix. This suggests the following algorithm:
for all elements in column j
place element <i, j, value> in
element <j, i, value>
The columns within each row of the transpose matrix will be arranged in ascending order.

Availaible at VTU HUB (Android App)


Transpose of sparse matrix
< row column Value> < row column Value>

Sparse matrix stored as triple Transpose matrix stored as triple

Availaible at VTU HUB (Android App)


void transpose (term a[], term b[])
{ /* b is set to the transpose of a */
int n, i, j, currentb;
n = a[0].value; /* total number of elements */
b[0].row = a[0].col; /* rows in b = columns in a */
b[0].col = a[0].row; /* columns in b = rows in a */
b[0].value = n;
if (n > 0)
{ currentb = 1;
for (i = 0; i < a[0].col; i++)
for (j= 1; j<=n; j++)
if (a[j].col == i)
{
b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++;
}
} Availaible at VTU HUB (Android App)
 Complexity is O(Columns. Elements)
 =(column.row.column)
 Where as if we use 2D representation
Complexity is O(row.columns)
Hence we go for another algorithm called as fast trnspose
algorithm in case of triple representation of matrix.

Availaible at VTU HUB (Android App)


STRING
 String: A finite sequence of zero or more Characters
 Length: The number of characters in a string
 Empty or Null String: The string with zero characters.
 Concatenation: Let S1 and S2 be the strings. The string consisting of the
characters of S1 followed by the character S2 is called Concatenation of S1 and
S2, denoted by S1//S2
Ex: „THE‟ // „END‟ = „THEEND‟
„THE‟ // „ ‟ „// „END‟ = „THE END‟
The length of S1//S2 is sum of the lengths of strings S1 and S2.
 Substring: A string Y is called substring of a string S if there exist string X and
Z such that S = X // Y // Z
If X is an empty string, then Y is called an Initial substring of S, and Z is an
empty string then Y is called a terminal substring of S.
Ex: „BE OR NOT‟ is a substring of „TO BE OR NOT TO BE‟
„THE‟ is an initial substring and END is of a terminal substring of String „THE
END‟ Availaible at VTU HUB (Android App)
STRINGS IN C
 In C, the strings are represented as character arrays terminated by the null character
\0.
 Declaration 1:
#define MAX_SIZE 100 /* maximum size of string */
char s[MAX_SIZE] = {“dog”};
char t[MAX_SIZE] = {“house”};
 s[0] s[1] s[2] s[3] t[0] t[1] t[2] t[3] t[4] t[5]
d o g \0 h o u s e \0
The above figure shows how these strings would be represented internally in memory.
 Declaration 2:
char s[ ] = {“dog”};
char t[ ] = {“house”};
Using these declarations, the C compiler will allocate just enough space to hold each
word including the null character.
Availaible at VTU HUB (Android App)
STORING STRINGS
 Strings are stored in three types of structures
1. Fixed length structures
2.Variable length structures with fixed maximum
3. Linked structures
 1. Record Oriented Fixed length storage:
In fixed length structures each line of print is viewed as a
record, where all have the same length i.e., where each record
accommodates the same number of characters.
 Example: Suppose the input consists of the program. Using a
record oriented, fixed length storage medium, the input data
will appear in memory as pictured below.

Availaible at VTU HUB (Android App)


Availaible at VTU HUB (Android App)
To insert a record then it requires that all succeeding records be
moved to new memory location. This disadvantage can be easily
solved as shown in below figure.

Availaible at VTU HUB (Android App)


 That is, one can use a linear array POINT which gives the address of
successive record, so that the records need not be stored in consecutive
locations in memory. Inserting a new record will require only an
updating of the array POINT.
 The main advantage of this method are
1. The ease of accessing data from any given record
2. The ease of updating data in any given record (as long as the length
of the new data does not exceed the record length)
 The main disadvantages are
1. Time is wasted reading an entire record if most of the storage
consists of inessential blank spaces.
2. When the correction consists of more or fewer characters than the
original text, changing a misspelled word requires record to be
changed.
Availaible at VTU HUB (Android App)
2. Variable length structures:

 The storage of variable-length strings in memory cells can be done in


two general ways
1. One can use a marker, such as two 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.

Availaible at VTU HUB (Android App)


Example:

Availaible at VTU HUB (Android App)


3.Linked storage:
 Strings are stored by means of linked lists.

 A linked list is a data structure where each element (called a node) is


made up of two items:
a) the data and
b) a reference (or pointer), which points to the next node.

 A linked list is a collection of nodes where each node is connected to


the next node through a pointer.

Availaible at VTU HUB (Android App)


CHARACTER DATA TYPE
 The various programming languages handles character data type in different ways.
 Constants
Many programming languages denotes string constants by placing the string in either
single or double quotation marks.
Ex: „THE END‟
“THE BEGINNING”
The string constants of length 7 and 13 characters respectively.
 Variables
Each programming languages has its own rules for forming character variables.
These variables fall into one of three categories
1. Static: In static character variable, whose length is defined before the program is
executed and cannot change throughout the program
2. Semi-static: The length of the variable may vary during the execution of the
program as long as the length does not exceed a maximum value determined by the
program before the program is executed.
3. Dynamic: The length of the variable can change during the execution of the program.
Availaible at VTU HUB (Android App)
STRING OPERATION
 1.Substring: Group of consecutive elements in string is known as
substring that are basic units to access in a string.
Accessing a substring from a given string requires three pieces of
information:
(1) The name of the string or the string itself
(2) The position of the first character of the substring in the given string
(3) The length of the substring or the position of the last character of the
substring.
 Syntax: SUBSTRING (string, initial, length)
The syntax denote the substring of a string S beginning in a position K
and having a length L.
Ex: SUBSTRING ('TO BE OR NOT TO BE‟, 4, 7) = 'BE OR N‟
SUBSTRING ('THE END', 4, 4) = „ END'
Availaible at VTU HUB (Android App)
STRING OPERATION
 2.Indexing : Indexing also called pattern matching, refers to finding the position
where a string pattern P first appears in a given string text T. This operation is
called INDEX
Syntax: INDEX (text, pattern)
If the pattern P does not appears in the text T, then INDEX is assigned the value 0.
The arguments “text” and “pattern” can be either string constant or string variable.

 Suppose T contains the text “ HIS FATHER IS THE PROFESSOR”


Then, INDEX(T,‟THE‟) has the value 7
INDEX(T,‟ THE‟) has the value 14
INDEX(T,‟THEN‟) has the value 0

Availaible at VTU HUB (Android App)


 3.Concatenation
Let S1 and S2 be string. The concatenation of S1 and S2 which is
denoted by S1 // S2, is the string consisting of the characters of
S1 followed by the character of S2.
 Ex: (a) Suppose S1 = 'MARK' and S2= „TWAIN' then
S1 // S2 = „MARKTWAIN‟
 Concatenation is performed in C language using strcat function as
shown below
strcat (S1, S2); Concatenates string S1 and S2 and stores the result
in S1
 strcat ( ) function is part of the string.h header file; hence it
must be included in link section
Availaible at VTU HUB (Android App)
STRING OPERATION
4.Length : The number of characters in a string is called its length.
 Syntax: LENGTH (string)
Ex: LENGTH („computer‟) = 8
 String length is determined in C language using the strlen( )
function, as shown below:
X = strlen ("sunrise");
strlen function returns an integer value 7 and assigns it to the
variable X
Similar to strcat, strlen is also a part of string.h, hence the header
file must be included at the time of pre-processing.

Availaible at VTU HUB (Android App)


String handling Functions
 strlen(str) ; Returns the no. of characters in the given string
 strcpy(dest,src) ; Copies the given source string to the destination string.
 strncpy(dest,source,n); Copies the n character from source string to the
destination string.
 strcmp(s1,s2) : Compares two strings and returns
0 if two strings are equal, 1 if s1>s2, -1 if s1<s2
 strncmp(s1,s2,n) : Compares n characters from s1 and s2 returns
0 if they are equal, 1 if n charecters of s1> n charecters of s2,
-1 if n charecters of s1< n charecters of s2
 strcat(s1,s2) : Joins two string into single string, s2 appended to s1
 Strncat(s1,s2,n); Concatenates n characters from s2 to s1
 strupr(str) : Converts characters of str into uppercase overwrites on str
 strlwr(str) : Converts characters of str into lowercase overwrites on str
 strrev(str) : reverse a given string str and overwrites on str
 Strstr(s1,s2); Checks whether substring s2 is present in s1
Availaible at VTU HUB (Android App)
String handling Functions in c

Availaible at VTU HUB (Android App)


String insertion Example i=1

Availaible at VTU HUB (Android App)


Word/Text Processing
 Word Processing Operations
1.Insertion: To insert a string S in a text T, so that S begins in position K
We denote INSERT(text,position,string)
Example: INSERT(„ABCDEFG‟,3,‟XYZ‟)=„ABXYZCDEFG‟

This INSERT function can be implemented using string operation as given below

INSERT(T,K,S)= SUBSTRING(T,1,K-1)//S// SUBSTRING(T,K,LENGTH(T)-K+1)

ie, Initial substring of T before position K,which has length K-1 is concatenated with string S
and result is concatenated with remaining part of T which begins in position K and has
LENGTH(T)-(K-1)= Length(T)-K+1

Availaible at VTU HUB (Android App)


Deletion : To delete the sub string from a text T, that begins at position K and has
length L
We denote DELETE(text,position,Length)
Example: DELETE(„ABCDEFG‟,4,2)=„ABCFG‟
DELETE(„ABCDEFG‟,0,2)=„ABCDEFG‟
This DELETE function can be implemented using string operation as given below
DELETE(T,K,L)= SUBSTRING(T,1,K-1)// SUBSTRING(T,K+L,LENGTH(T)-K-L+1)
ie, Initial substring of T before position K,which has length K-1 is concatenated with terminal
substring of T beginning at position K+L Which has length
LENGTH(T)-(K+L-1) = LENGTH(T)-K-L+1
Note: DELETE(T,K,L)=T when K=0

Availaible at VTU HUB (Android App)


 Replacement:
 To replace the first occurrence of pattern P1 by a pattern P2 in a given text T,
 We Write REPLACE(text,Pattern1,Pattern2)
For example REPLACE(„XABYABZ‟, „AB‟,‟C‟)=„XCYABZ‟
REPLACE(„XABYABZ‟, „BA‟,‟C‟)=„XABYABZ‟
 REPLACE function can be expressed as deletion followed by an insertion as given below
K: = INDEX(T,P1)
T:= DELETE(T,K,LENGTH(P1))
INSERT(T,K,P2)
 To replace every occurrence of pattern P1 in T by pattern P2 apply
repeatedly REPLACE(T,P1,P2) until INDEX(T,P)=0

Availaible at VTU HUB (Android App)


Algorithm

Availaible at VTU HUB (Android App)


PATTERN MATCHING ALGORITHMS
 Pattern matching is the problem of deciding whether or not a
given string pattern P appears in a string text T. The length of
P does not exceed the length of T.
 First Pattern Matching Algorithm
  The first pattern matching algorithm is one in which
comparison is done by a given pattern P with each of the
substrings of T, moving from left to right, until a match is
found.

Availaible at VTU HUB (Android App)


 Let pattern P is 4 character string and T is 20 character string
appearing as linear array ie,
 P=P[1],P[2],P[3],P[4] and T=T[1],T[2],T[3],……….T[20]
 P is compared with each of following 4- character substring
of T ie Wk=SUBSTRING(T,K,Length(P))
 W1= T[1],T[2],T[3],T[4], W2= T[2],T[3],T[4],T[5] …….
W17= T[17],T[18],T[19],T[20]
 Here MAX=20-4+1=17

Availaible at VTU HUB (Android App)


Algorithm: (Pattern Matching)
 P and T are strings with lengths R and S, and are stored as arrays
with one character per element. This algorithm finds the INDEX of
P in T.
1. [Initialize.] Set K: = 1 and MAX: = S - R + 1
2. Repeat Steps 3 to 5 while K ≤ MAX
3. Repeat for L = 1 to R: [Tests each character of P]
If P[L] ≠ T[K + L – l], then: Go to Step 5
[End of inner loop.]
4. [Success.] Set INDEX = K, and Exit
5. Set K := K + 1
[End of Step 2 outer loop]
6. [Failure.] Set INDEX = O
7. Exit Availaible at VTU HUB (Android App)
Example

Availaible at VTU HUB (Android App)

You might also like