Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Lecturenotes Module 1 BCS304 Datastructure

Datastructure notes

Uploaded by

kalidassm.aiml
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lecturenotes Module 1 BCS304 Datastructure

Datastructure notes

Uploaded by

kalidassm.aiml
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

MODULE 1: INTRODUCTION

DATA STRUCTURES
Data may be organized in many different ways. The logical or mathematical model of a
particular organization of data is called a data structure.

The choice of a particular data model depends on the two considerations


1. It must be rich enough in structure to mirror the actual relationships of the data in thereal
world.
2. The structure should be simple enough that one can effectively process the datawhenever
necessary.

Basic Terminology: Elementary Data Organization:

Data: Data are simply values or sets of values.


Data items: Data items refers to a single unit of values.

Data items that are divided into sub-items are called Group items. Ex: An Employee
Namemay be divided into three subitems- first name, middle name, and last name.

Data items that are not able to divide into sub-items are called Elementary items.
Ex: SSN

Entity: An entity is something that has certain attributes or properties which may be
assignedvalues. The values may be either numeric or non-numeric.
Ex: Attributes- Names, Age, Sex, SSN
Values- Rohland Gail, 34, F, 134-34-5533

Entities with similar attributes form an entity set. Each attribute of an entity set has a
range of values, the set of all possible values that could be assigned to the particular
attribute.
The term “information” is sometimes used for data with given attributes of in other words
meaningful or processed data.

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.

R.Tharani, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Each record in a file may contain many field items but 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.

Records may also be classified according to length.


A file can have fixed-length records or variable-length records.
 In fixed-length records, all the records contain the same data items with the same amount
of space assigned to each data item.
 In variable-length records file records may contain different lengths.

Example: Student records have variable lengths, since different students take different
numbersof courses. Variable-length records have a minimum and a maximum length.

The above organization of data into fields, records and files may not be complex enough to
maintain and efficiently process certain collections of data. For this reason, data are also
organized into more complex types of structures.

CLASSIFICATION OF DATA STRUCTURES

Data structures are generally classified into


 Primitive data Structures
 Non-primitive data Structures

1. Primitive data Structures: Primitive data structures are the fundamental data types which
are supported by a programming language. Basic data types such as integer, float, character and
Boolean are known as Primitive data Structures. These data types consist of characters that cannot be
divided and hence they also called simple data types.

2. Non- Primitive data Structures: Non-primitive data structures are those data structures
which are created using primitive data structures. Examples of non-primitive data structures is the
processing of complex numbers, linked lists, stacks, trees, and graphs.

Based on the structure and arrangement of data, non-primitive data structures is further
classified into
1. Linear Data Structure
2. Non-linear Data Structure

R.Tharani, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

1. Linear Data Structure:


A data structure is said to be linear if its elements form a sequence or a linear list. There
arebasically two ways of representing such linear structure in memory.
1. One way is to have the linear relationships between the elements represented by meansof
sequential memory location. These linear structures are called arrays.
2. The other way is to have the linear relationship between the elements represented by
means of pointers or links. These linear structures are called linked lists.

The common examples of linear data structure are Arrays, Queues, Stacks, Linked lists

2. Non-linear Data Structure:


A data structure is said to be non-linear if the data are not arranged in sequence or a linear.
The insertion and deletion of data is not possible in linear fashion. This structure is mainly
used to represent data containing a hierarchical relationship between elements. Trees and
graphs are the examples of non-linear data structure.

Arrays:
The simplest type of data structure is a linear (or one dimensional) array. A list of a finite
number n of similar data referenced respectively by a set of n consecutive numbers,
usually 1,2, 3 ....n. if A is chosen the name for the array, then the elements of A are
denoted by
subscript notation a1, a2, a3 ......... an
or
by the parenthesis notation A (1), A (2), A (3) ........ A (n)
or
by the bracket notation A [1], A [2], A [3] ........ A [n]

Example 1: A linear array STUDENT consisting of the names of six students is pictured
inbelow figure. Here STUDENT [1] denotes John Brown, STUDENT [2] denotes Sandra
Gold, and so on.
Linear arrays are called one-dimensional arrays because each element in such an array is
referenced by one subscript. A two-dimensional array is a collection of similar data
elements where each element is referenced by two subscripts.

Example 2: A chain of 28 stores, each store having 4 departments, may list its weekly
sales as in below fig. Such data can be stored in the computer using a two-dimensional
array in which the first subscript denotes the store and the second subscript the
department. If SALES is the name given to the array, then
SALES [1, 1] = 2872, SALES [1, 2] = 805, SALES [1, 3] = 3211,…., SALES [28, 4] =
982
R.Tharani, Dept. of AIML, Sri Sairam College of Engineering
Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Trees
Data frequently contain a hierarchical relationship between various elements. The data
structurewhich reflects this relationship is called a rooted tree graph or a tree.
Some of the basic properties of tree are explained by means of examples

Example 1: Record Structure


Although a file may be maintained by means of one or more arrays a record, where one
indicates both the group items and the elementary items, can best be described by means of
a tree structure. For example, an employee personnel record may contain the following
data items:
Social Security Number, Name, Address, Age, Salary, Dependents

However, Name may be a group item with the sub-items Last, First and MI (middle
initial). Also Address may be a group item with the subitems Street address and Area
address, where Area itself may be a group item having subitems City, State and ZIP code
number.
This hierarchical structure is pictured below

R.Tharani, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Another way of picturing such a tree structure is in terms of levels, as shown below

Some of the data structures are briefly described below.

1. Stack: A stack, also called a fast-in first-out (FIFO) system or lastin last-out (LILO), is a linear
list in which insertions and deletions can take place only at one end, called the top. This structure is
similar in its operation to a stack of dishes on a spring system as shown in fig.
Note that new 4 dishes are inserted only at the top of the stack and dishes can be deleted
only from the top of the Stack.

R.Tharani, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

2. Queue: A queue, also called a first-in first-out (FIFO) system, is a linear list in which
deletions can take place only at one end of the list, the "from'' of the list, and insertions can
take place only at the other end of the list, the “rear” of the list.
This structure operates in much the same way as a line of people waiting at a bus stop, as
picturedin Fig. the first person in line is the first person to board the bus. Another analogy
is with automobiles waiting to pass through an intersection the first car in line is the first
car through.

3. Graph: Data sometimes contain a relationship between pairs of elements which is not necessarily
hierarchical in nature. For example, suppose an airline flies only between the cities connected by lines
in Fig. The data structure which reflects this type of relationship is called a graph.

R.Tharani, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

DATA STRUCTURES OPERATIONS


The data appearing in data structures are processed by means of certain operations. The
following four operations play a major role in this text:
1. Traversing: accessing each record/node exactly once so that certain items in the record
may be processed. (This accessing and processing is sometimes called “visiting” the record.)
2. Searching: Finding the location of the desired node with a given key value, or finding the
locations of all such nodes which satisfy one or more conditions.
3. Inserting: Adding a new node/record to the structure.
4. Deleting: Removing a node/record from the structure.

The following two operations, which are used in special situations:


1. Sorting: Arranging the records in some logical order (e.g., alphabetically according to
some NAME key, or in numerical order according to some NUMBER key, such as social security
number or account number)
2. Merging: Combining the records in two different sorted files into a single sorted file.

POINTERS
A pointer is a variable which contains the address, the memory of another variable.
The two most important operator used with the pointer type are
& - The unary operator & which gives the address of a variable
* - The indirection or dereference operator * gives the content of the object pointed toby a
pointer.

Declaration
int i, *pi;

Here, i is the integer variable and pi is a pointer to an integer

pi = &i;
Here, &i returns the address of i and assigns it as the value of pi

Null Pointer
The null pointer points to no object or function. The null pointer is represented by the
integer 0.
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

R.Tharani, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

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 to a legitimate object.
Ex: main ()
{
int *p;
int pa = 10;p = &pa;
printf(“%d”, *p); //output = 10;
printf(“%d”, *(p+1)); //accessing memory which is out of range
}
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. Pointer is dangerous when use of explicit type casts in converting between pointer types
Ex: pi = malloc (sizeof (int));pf = (float*) pi;
4. 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.

Pointers to Pointers
A variable which contains address of a pointer variable is called pointer-to-pointer.

It is a form of multiple indirection, or a chain of pointers. Normally, a pointer contains the address
of a variable. When we define a pointer to a pointer, the first pointer contains the address of the
second pointer, which points to the location that contains the actual value as shown below.

A variable that is a pointer to a pointer must be declared as such. This is done by placing an
additional asterisk in front of its name. For example, the following declaration declares a pointer to
a pointer of type int −

int **var;

When a target value is indirectly pointed to by a pointer to a pointer, accessing that value requires
that the asterisk operator be applied twice, as is shown below in the example −

R.Tharani, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

#include <stdio.h>

int main () {

int var;
int *ptr;
int **pptr;

var = 3000;

/* take the address of var */


ptr = &var;

/* take the address of ptr using address of operator & */


pptr = &ptr;

/* take the value using pptr */


printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);

return 0;
}

When the above code is compiled and executed, it produces the following result −

Value of var = 3000


Value available at *ptr = 3000
Value available at **pptr = 3000

DYNAMIC MEMORY ALLOCATION FUNCTIONS

1. malloc( ):
The function malloc allocates a user- specified amount of memory and a pointer to the
startofthe allocated memory is returned.
If there is insufficient memory to make the allocation, the returned value is NULL.Syntax:
data_type *x;
x= (data_type *)
malloc(size);Where,

x is a pointer variable of data_type


size is the number of bytes

Ex: int *ptr;


ptr = (int *) malloc(100*sizeof(int));

Prof.Tharani R , Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

2. calloc( ):
The function calloc allocates a user- specified amount of memory and initializes the
allocatedmemory to 0 and a pointer to the start of the allocated memory is returned.
If there is insufficient memory to make the allocation, the returned value is NULL.
Syntax:
data_type *x;
x= (data_type *) calloc(n, size);

Where,
x is a pointer variable of type int
n is the number of block to be allocated
size is the number of bytes in each block

Ex: int *x
x= calloc (10, sizeof(int));
The above example is used to define a one-dimensional array of integers. The capacity of thisarray is n=10
and x [0: n-1] (x [0, 9]) are initially 0.

3. realloc( ):
 Before using the realloc( ) function, the memory should have been allocated using malloc(
) or calloc( ) functions.
 The function relloc( ) resizes memory previously allocated by either malloc or calloc,
which means, the size of the memory changes by extending or deleting the allocated memory.
 If the existing allocated memory need to extend, the pointer value will not change.
 If the existing allocated memory cannot be extended, the function allocates a new block
and copies the contents of existing memory block into new memory block and then deletes the old
memory block.
 When realloc is able to do the resizing, it returns a pointer to the start of the new block
and when it is unable to do the resizing, the old block is unchanged and the function returns the value
NULL
Syntax:
data_type *x;
x= (data_type *) realloc(p, s );

The size of the memory block pointed at by p changes to s. When s > p the additional s-p
memory block have been extended and when s < p, then p-s bytes of the old block are
freed.

4. free( )
Dynamically allocated memory with either malloc( ) or calloc ( ) does not return on its
own.The programmer must use free( ) explicitly to release space.

Prof.Tharani R , Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Syntax:
free(ptr);
This statement cause the space in memory pointer by ptr to be deallocated

ARRAYS
 An Array is defined as, an ordered set of similar data items. All the data items of anarray
are stored in consecutive memory locations.
 The data items of an array are of same type and each data items can be accessed usingthe
same name but different index value.
 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
Implementation:
< 3 , 17 > list[3]=17
When
< 4 , 35 > the complier encounters
list[4]=35 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
Here, list is the name of array. By using, list [0] to list [4] the data items in list can be
address of list[0] is called the base address.
accessed.
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:
Array in C
Declaration: A one dimensional array in C is declared by adding brackets to the name of avariable.
list[i] = α + i * sizeof (int)
Ex: int list[5], *plist[5];

 Where, α is
The baselist[5],
array address.
defines 5 integers and in C array start at index 0, so list[0], list[1], list[2],
list[3], list[4] are the names of five array elements which contains an integer value.
 The array *plist[5], defines an array of 5 pointers to integers. Where, plist[0], plist[1],
plist[2], plist[3], plist[4] are the five array elements which contains a pointer to an integer.

Difference between int *list1; & int list2[5];


The variables list1 and list2 are both pointers to an int, but in list2[5] five memory locations are
reserved for holding integers. list2 is a pointer to list2[0] and list2+i is a pointer to list2[i].

Prof.Tharani R , Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Note: In C the offset i do not multiply with the size of the type to get to the appropriateelement of the
array. Hence (list2+i) is equal &list2[i] and *(list2+i) is equal to list2[i].

How C treats an array when it is parameter to a function?

 All parameters of a C functions must be declared within the function. As various


parameters are passed to functions, the name of an array can be passed as parameter.
 The range of a one-dimensional array is defined only in the main function since new
storage for an array is not allocated within a function.
 If the size of a one dimensional array is needed, it must be passed into function as a
argument or accessed as a global variable.

Example: Array Program

#define MAX_SIZE 100float sum(float [], int);


float input[MAX_SIZE], answer;void main(void)
{
int i;
for( i=0; i<MAX_SIZE; i++)input[i]= i;
answer = sum(input, MAX_SIZE); printf(“\n The sum is: %f \n”,answer);
}

float sum(float list[], int n)


{
int i;
float tempsum = 0;for(i=0; i<n; i++)
tempsum = tempsum + list[i];return tempsum;
}

Prof.Tharani R , Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

When sum is invoked, input=&input[0] is copied into a temporary location and associatedwith the
formal parameter list
A function that prints out both the address of the ith element of the array and the value
foundat that address can written as shown in below program.

void print1 (int *ptr, int rows)


{
int i;
printf(“ Address contents \n”);for(i=0; i<rows; i++)
printf(“% 8u %5d \n”, ptr+i, *(prt+i));printf(“\n”);
}

Output:
Address Content
12244868 0
12344872 1
12344876 2
12344880 3
12344884 4

DYNAMIC ALLOCATED ARRAYS


Array in C is static in nature, so its size should be known at compile time and we can’t change
the size of the array after its declaration. Due to this, we may encounter situations where our
array doesn’t have enough space left for required elements or we allotted more than the required
memory leading to memory wastage. To solve this problem, dynamic arrays come into the
picture.
A Dynamic Array is allocated memory at runtime and its size can be changed later in the
program.
We can create a dynamic array in C by using the following methods:
1. Using malloc() Function
2. Using calloc() Function
3. Resizing Array Using realloc() Function

Dynamic Array Using malloc() Function

ptr = (int*) malloc(100 * sizeof(int));


// C program to create dynamic array using malloc() function

#include <stdio.h>
#include <stdlib.h>
int main()

Prof.Tharani R , Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

{ // address of the block created hold by this pointer


int* ptr;
int size;

// Size of the array


printf("Enter size of elements:");
scanf("%d", &size);

// Memory allocates dynamically using malloc()


ptr = (int*)malloc(size * sizeof(int));

// Checking for memory allocation


if (ptr == NULL) {
printf("Memory not allocated.\n");
}
else {

// Memory allocated
printf("Memory successfully allocated using "
"malloc.\n");

// Get the elements of the array


for (int j = 0; j < size; ++j) {
ptr[j] = j + 1;
}

// Print the elements of the array


printf("The elements of the array are: ");
for (int k = 0; k < size; ++k) {
printf("%d, ", ptr[k]);
}
}

return 0;
}

Output:
Enter size of elements:5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,

2. Dynamic Array Using calloc() Function


ptr = (int*) calloc(5, sizeof(float));
// C program to create dynamic array using calloc() function
#include <stdio.h>
#include <stdlib.h>

Prof.Tharani R , Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

int main()
{
// address of the block created hold by this pointer
int* ptr;
int size;

// Size of the array


printf("Enter size of elements:");
scanf("%d", &size);

// Memory allocates dynamically using calloc()


ptr = (int*)calloc(size, sizeof(int));

// Checking for memory allocation


if (ptr == NULL) {
printf("Memory not allocated.\n");
}
else {

// Memory allocated
printf("Memory successfully allocated using " "malloc.\n");

// Get the elements of the array


for (int j = 0; j < size; ++j) {
ptr[j] = j + 1;
}

// Print the elements of the array


printf("The elements of the array are: ");
for (int k = 0; k < size; ++k) {
printf("%d, ", ptr[k]);
}
}

return 0;
}

Output:
Enter size of elements:6
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5, 6,
3. Dynamically Resizing Array Using realloc() Function

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

ptr = realloc(ptr, newSize);


// C program to resize dynamic array using realloc() function
#include <stdio.h>

#include <stdlib.h>
int main()
{
// address of the block created hold by this pointer
int* ptr;
int size = 5;
// Memory allocates dynamically using calloc()
ptr = (int*)calloc(size, sizeof(int));

if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using "
"calloc.\n");
}

// inserting elements
for (int j = 0; j < size; ++j) {
ptr[j] = j + 1;
}

printf("The elements of the array are: ");


for (int k = 0; k < size; ++k) {
printf("%d, ", ptr[k]);
}

printf("\n");

size = 10;
int *temp = ptr;
// using realloc
ptr = realloc(ptr, size * sizeof(int));
if (!ptr) {
printf("Memory Re-allocation failed.");
ptr = temp;

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

}
else {
printf("Memory successfully re-allocated using "
"realloc.\n");
}

// inserting new elements


for (int j = 5; j < size; ++j) {
ptr[j] = j + 10;
}

printf("The new elements of the array are: ");


for (int k = 0; k < size; ++k) {
printf("%d, ", ptr[k]);
}
return 0;
}
Output
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,
Memory successfully re-allocated using realloc.
The new elements of the array are: 1, 2, 3, 4, 5, 15, 16, 17, 18, 19,

STRUCTURES

The structure in C is a user-defined data type that can be used to group items of possibly different
types into a single type. The struct keyword is used to define the structure in the C programming
language.

Ex: struct {
char name[10];int age;
float salary;
} Person;
The above example creates a structure and variable name is Person and that has three fields:
name = a name that is a character array
age = an integer value representing the age of the person salary = a float value representing
the salary of the individual

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304
Assign values to fields
To assign values to the fields, use . (dot) as the structure member operator. This operator is
used to select a particular member of the structure

Ex: Person.name=james;Person.age =10;


Person.salary = 35000;

Type-Defined Structure
The structure definition associated with keyword typedef is called Type-Defined Structure.
Syntax 1: typedef struct
{
data_type member 1;
data_type member 2;
………………………
………………………
data_type member n;
}Type_name;

Where,
 typedef is the keyword used at the beginning of the definition and by using typedef user
defined data type can be obtained.
 struct is the keyword which tells structure is defined to the complier
 The members are declare with their data_type
 Type_name is not a variable, it is user defined data_type.

Syntax 2: struct struct_name


{
data_type member 1;
data_type member 2;
………………………
………………………
data_type member n;
};
typedef struct struct_name Type_name;

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Ex: typedef struct{


char name[10];int age;
float salary;
}humanBeing;

In above example, humanBeing is the name of the type and it is a user defined data type.

Declarations of structure variables:

humanBeing person1, person2;

This statement declares the variable person1 and person2 are of type humanBeing.
Structure Operation
The various operations can be performed on structures and structure members.
1. Structure Equality Check:
Here, the equality or inequality check of two structure variable of same type or dissimilar
typeis not allowed
typedef struct{
char name[10];int age;
float salary;
}humanBeing; humanBeing person1, person2;

if (person1 = = person2) is invalid.

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

The valid function is shown below#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 human being otherwisereturn 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;
}
Program: Function to check equality of structures

2. Assignment operation on Structure variables:


person1 = person2
The above statement means that the value of every field of the structure of person 2 is
assigned as the value of the corresponding field of person 1, but this is invalid statement.

Valid Statements is given below:


strcpy(person1.name, person2.name); person1.age = person2.age; person1.salary =
person2.salary;

Structure within a structure:


There is possibility to embed a structure within a structure. There are 2 ways to embed
structure.

1. The structures are defined separately and a variable of structure type is declared inside the
definition of another structure. The accessing of the variable of a structure type that are nested inside
another structure in the same way as accessing other member of that structure

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Example: The following example shows two structures, where both the structure are defined
separately.
typedef struct {

int month;int day; int year;


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

int month;int day; int year;

} date;
} humanBeing;
} humanBeing;
humanBeing person1;

A person born on February 11, 1944, would have the values for the date struct set as:
person1.dob.month = 2;
person1.dob.day = 11;
person1.dob.year = 1944;

2. The complete definition of a structure is placed inside the definition of another structure.
Example:
typedef struct {
char name[10];int age;
float salary;struct {

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

SELF-REFERENTIAL STRUCTURES
A self-referential structure is one in which one or more of its components is a pointer to itself. Self-
referential structures usually require dynamic storage management routines (malloc and free) to
explicitly obtain and release memory.

Consider as an example:
typedef struct {

} list;
char data;
struct list *link ;

Each instance of the structure list will have two components data and link.
 Data: is a single character,
 Link: link is a pointer to a list structure. The value of link is either the address inmemory of
an instance of list or the null pointer.

Consider these statements, which create three structures and assign values to their respective

fields:list item1, item2, item3;


item1.data = 'a';
item2.data = 'b';item3.data = 'c';
item1.link = item2.1ink = item3.link = NULL;

Structures item1, item2 and item3 each contain the data item a, b, and c respectively, and the
null pointer. These structures can be attached together by replacing the null link field in
item 2 withone that points to item 3 and by replacing the null link field in item 1 with one
that points to item2.

item1.link = &item2;item2.1ink = &item3;

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Unions:

A union is similar to a structure, it is collection of data, similar data type or dissimilar.

Syntax: union{

data_type member 1;
data_type member 2;
………………………
………………………
data_type member n;
}variable_name;
Example:

union{
int children;
int beard;
} u;

Union Declaration:
A union declaration is similar to a structure, but the fields of a union must share their memory
space. This means that only one field of the union is "active" at any given time.

union{
char name;
int age;
float salary;
}u;

The major difference between a union and a structure is that unlike structure members which

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

are stored in separate memory locations, all the members of union must share the same
memory space. This means that only one field of the union is "active" at any given time.

Example: #include <stdio.h>union job {


char name[32];float salary;
int worker_no;
}u;

int main( ){
printf("Enter name:\n"); scanf("%s", &u.name); printf("Enter salary: \n");
scanf("%f", &u.salary);
printf("Displaying\n Name :%s\n",u.name);printf("Salary: %.1f",u.salary); return 0;
}

Output:
Enter name: Albert Enter salary: 45678.90

Displaying
Name: f%gupad (Garbage Value)Salary: 45678
.90

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

REPRESENTATION OF LINEAR ARRAYS IN MEMORY

Linear Array
A linear array is a list of a finite number ‘n’ of homogeneous data element such that
a. The elements of the array are reference respectively by an index set consisting of n
consecutive numbers.
b. The element of the array are respectively in successive memory locations.

The number n of elements is called the length or size of the array. The length or the numbers
of elements of the array can be obtained from the index set by the formula
When LB = 0,
Length = UB – LB + 1Length = UB
When LB = 1,

Where,

UB is the largest index called the Upper Bound LB is the smallest index, called the Lower
Bound

Representation of linear arrays in memory

Let LA be a linear array in the memory of the computer. The memory of the computer is
simply a sequence of address location as shown below,

1000
1001
1002
1003
1004

LOC (LA [K]) = address of the element LA [K] of the array LA The elements of LA are

stored in successive memory cells.


The computer does not keep track of the address of every element of LA, but needs to keep
track only the address of the first element of LA denoted by,
Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering
Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304
Base (LA)
and called the base address of LA.

Using the base address of LA, the computer calculates the address of any element of LA by
the formula

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

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

DYNAMICALLY ALLOCATED ARRAYS

One Dimensional Array

While writing computer programs, if finds ourselves in a situation where we cannot determine
how large an array to use, then a good solution to this problem is to defer this decision to run
time and allocate the array when we have a good estimate of the required array size.
Example:

int i, n, *list;
printf(“Enter the number of numbers to generate:”);scanf(“%d”, &n);
if(n<1)
{
fprintf (stderr, “Improper value of n \n”);exit(EXIT_FAILURE);
}
MALLOC (list, n*sizeof(int));

The programs fails only when n<1 or insufficient memory to hold the list of numbers that
areto be sorted.

Two DimensionalArrays
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
aone-dimensional array.

Example: int x[3][5];

C find element x[i][j] by first accessing the pointer in x[i].


Where x[i] = α+ i* sizeof(int), which give the address of the zeroth element of row i of thearray.
Then adding j*sizeof(int) to this pointer ( x[i] ) , the address of the [j]th element of row i is determined.

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304
x[i] = α+ i* sizeof(int)
x[j]= α+ j* sizeof(int)
x[i][j] = x[i]+ j* sizeof(int)

Creation of Two-Dimensional Array Dynamically

int **myArray;
myArray = make2dArray(5,10);myArray[2][4]=6;

int ** make2dArray(int rows, int cols)


{ /* create a two dimensional rows X cols array */ int **x, i;
MALLOC(x, rows * sizeof (*x)); /*get memory for row pointers*/
for (i= 0;i<rows; i++) /* get memory for each row */
MALLOC(x[i], cols * sizeof(**x));

return x;
}

The second line allocates memory for a 5 by 10 two-dimensional array of integers and the third line
assigns the value 6 to the [2][4] element of this array.

ARRAY OPERATIONS

1. Traversing
 Let A be a collection of data elements stored in the memory of the computer. Supposeif the
contents of the each elements of array A needs to be printed or to count the numbers of elements of A
with a given property can be accomplished by Traversing.
 Traversing is a accessing and processing each element in the array exactly once.

Algorithm 1: (Traversing a Linear Array)

Hear LA is a linear array with the lower bound LB and upper bound UB. This algorithm
traverses LA applying an operation PROCESS to each element of LA using while loop.
1. [Initialize Counter] set K:= LB
2. Repeat step 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

Algorithm 2: (Traversing a Linear Array)

Hear LA is a linear array with the lower bound LB and upper bound UB. This algorithm
traverses LA applying an operation PROCESS to each element of LA using repeat – for loop.

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

1. Repeat for K = LB to UB
Apply PROCESS to LA [K][End of loop]
2. Exit.

Example:
Consider the array AUTO which records the number of automobiles sold each year from
1932through 1984.

To find the number NUM of years during which more than 300 automobiles were sold,
involves traversing AUTO.
1. [Initialization step.] Set NUM := 0
2. Repeat for K = 1932 to 1984:
If AUTO [K] > 300, then: Set NUM: = NUM + 1.
[End of loop.]
3. Return.

2. Inserting
 Let A be a collection of data elements stored in the memory of the computer. Inserting
refers to the operation of adding another element to the collection A.
 Inserting an element at the “end” of the linear array can be easily done provided the memory
space allocated for the array is large enough to accommodate the additional element.
 Inserting an element in the middle of the array, then on average, half of the elements must
be moved downwards to new locations to accommodate the new element and keep the order of the other
elements.

Algorithm:
INSERT (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer such that K ≤ N. This
algorithm inserts an element ITEM into the Kth position in LA.

1. [Initialize counter] set J:= N


2. Repeat step 3 and 4 while J ≥ K
3. [Move Jth element downward] 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

3. Deleting
 Deleting refers to the operation of removing one element to the collection A.

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304
 Deleting an element at the “end” of the linear array can be easily done with difficulties.
 If element at the middle of the array needs to be deleted, then each subsequentelements be
moved one location upward to fill up the array.

Algorithm
DELETE (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer such that K ≤ N. this
algorithm deletes the Kth element from LA

1. Set ITEM:= LA[K]


2. Repeat for J = K to N – 1
[Move J + 1 element upward] set LA[J]:= LA[J+1][End of loop]
3. [Reset the number N of elements in LA] set N:= N – 1
4. Exit

Example: Inserting and Deleting


Suppose NAME is an 8-element linear array, and suppose five names are in the array, as in
Fig.(a). Observe that the names are listed alphabetically, and suppose we want to keep the
array names alphabetical at all times. Suppose Ford is added to the array. Then Johnson,
Smith and Wagner musteach be moved downward one location, as in Fig.(b). Next suppose
Taylor is added to the array; then Wagner must be moved, as in Fig.(c). Last, suppose Davis is
removed from the array. Then thefive names Ford, Johnson, Smith, Taylor and Wagner must
each be moved upward one location, as in Fig.(d).

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304
4. Sorting
Sorting refers to the operation of rearranging the elements of a list. Here list be a set of n
elements. The elements are arranged in increasing or decreasing order.

Ex: suppose A is the list of n numbers. Sorting A refers to the operation of rearranging the
elements of A so they are in increasing order, i.e., so that,
A[I] < A[2] < A[3] < ... < A[N]

For example, suppose A originally is the list


8, 4, 19, 2, 7, 13, 5, 16
After sorting, A is the list
2, 4, 5, 7, 8, 13, 16, 19
Data Structures & Applications [BCS304]

MULTIDIMENSIONAL ARRAY

A multi-dimensional array can be termed as an array of arrays that stores homogeneous data in
tabular form. Data in multidimensional arrays is generally stored in row-major order in the memory.
The general form of declaring N-dimensional arrays is shown below.
Syntax:
data_type array_name[size1][size2] .... [sizeN];
 data_type: Type of data to be stored in the array.
 array_name: Name of the array.
 size1, size2,…, sizeN: Size of each dimension.
Examples:
Two dimensional array: int two_d[10][20];

Three dimensional array: int three_d[10][20][30];

Size of Multidimensional Arrays:


The total number of elements that can be stored in a multidimensional array can be calculated by
multiplying the size of all the dimensions.
For example:
 The array int x[10][20] can store total (10*20) = 200 elements.
 Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.
To get the size of the array in bytes, we multiply the size of a single element with the total number
of elements in the array.
For example:
 Size of array int x[10][20] = 10 * 20 * 4 = 800 bytes. (where int = 4 bytes)
 Similarly, size of int x[5][10][20] = 5 * 10 * 20 * 4 = 4000 bytes. (where int = 4
Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering
Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304
bytes)
The most commonly used forms of the multidimensional array are:

1. Two Dimensional Array


2. Three Dimensional Array
Two-Dimensional Array in C
A two-dimensional array or 2D array in C is the simplest form of the multidimensional array. We
can visualize a two-dimensional array as an array of one-dimensional arrays arranged one over

another forming a table with ‘x’ rows and ‘y’ columns where the row number ranges from 0 to (x-1)
and the column number ranges from 0 to (y-1).

Graphical Representation of Two-Dimensional Array of Size 3 x 3

Declaration of Two-Dimensional Array in C

The basic form of declaring a 2D array with x rows and y columns in C is shown below.
Syntax:
data_type array_name[x][y];

where,

 data_type: Type of data to be stored in each element.


 array_name: name of the array
 x: Number of rows.
 y: Number of columns.
We can declare a two-dimensional integer array say ‘x’ with 10 rows and 20 columns as:

Example:
int x[10][20];

Initialization of Two-Dimensional Arrays in C

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304
The various ways in which a 2D array can be initialized are as follows:

1. Using Initializer List


2. Using Loops
1. Initialization of 2D array using Initializer List
We can initialize a 2D array in C by using an initializer list as shown in the example below.
First Method:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}

The above array has 3 rows and 4 columns. The elements in the braces from left to right are stored
in the table also from left to right. The elements will be filled in the array in order: the first 4
elements from the left will be filled in the first row, the next 4 elements in the second row, and so
on.

Second Method (better):


int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};

This type of initialization makes use of nested braces. Each set of inner braces represents one row.
In the above example, there is a total of three rows so there are three sets of inner braces. The
advantage of this method is that it is easier to understand.

Note: The number of elements in initializer list should always be less than or equal to the total
number of elements in the array.

2. Initialization of 2D array using Loops


We can use any C loop to initialize each member of a 2D array one by one as shown in the below
example.

Example:
int x[3][4];

for(int i = 0; i < 3; i++){

for(int j = 0; j < 4; j++){

x[i][j] = i + j;

Accessing Elements of Two-Dimensional Arrays in C


Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering
Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304
Elements in 2D arrays are accessed using row indexes and column indexes. Each element in a 2D
array can be referred to by:

Syntax:
array_name[i][j]

where,

 i: The row index.


 j: The column index.

// C Program to print the elements of a


// Two-Dimensional array
#include <stdio.h>
int main(void)
{
// an array with 3 rows and 2 columns.
int x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
// output each array element's value
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
printf("Element at x[%i][%i]: ", i, j);
printf("%d\n", x[i][j]);
}
}
return (0);
}
Output
Element at x[0][0]: 0

Element at x[0][1]: 1

Element at x[1][0]: 2

Element at x[1][1]: 3

Element at x[2][0]: 4

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Element at x[2][1]: 5

Three-Dimensional Array in C
A Three Dimensional Array or 3D array in C is a collection of two-dimensional arrays. It can be
visualized as multiple 2D arrays stacked on top of each other.

Declaration of Three-Dimensional Array in C


We can declare a 3D array with x 2D arrays each having y rows and z columns using the syntax
shown below.
Syntax:
data_type array_name[x][y][z];

 data_type: Type of data to be stored in each element.


 array_name: name of the array
 x: Number of 2D arrays.
 y: Number of rows in each 2D array.
 z: Number of columns in each 2D array.
Example:
int array[3][3][3];

Initialization of Three-Dimensional Array in C

Initialization in a 3D array is the same as that of 2D arrays. The difference is as the number of
dimensions increases so the number of nested braces will also increase.

A 3D array in C can be initialized by using:

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

1. Initializer List
2. Loops
Initialization of 3D Array using Initializer List
Method 1:
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

11, 12, 13, 14, 15, 16, 17, 18, 19,

20, 21, 22, 23};

Method 2(Better):
int x[2][3][4] =

{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },

{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }

};

Initialization of 3D Array using Loops


It is also similar to that of 2D array with one more nested loop for accessing one more dimension.

int x[2][3][4];

for (int i=0; i<2; i++) {

for (int j=0; j<3; j++) {

for (int k=0; k<4; k++) {

x[i][j][k] = (some_value);

Accessing elements in Three-Dimensional Array in C

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Accessing elements in 3D Arrays is also similar to that of 3D Arrays. The difference is we have to
use three loops instead of two loops for one additional dimension in 3D Arrays.

Syntax:
array_name[x][y][z]

where,

 x: Index of 2D array.
 y: Index of that 2D array row.
 z: Index of that 2D array column.

// C program to print elements of Three-Dimensional Array


#include <stdio.h>
int main(void)
{ // initializing the 3-dimensional array
int x[2][3][2] = { { { 0, 1 }, { 2, 3 }, { 4, 5 } },
{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };
// output each element's value
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 2; ++k) {
printf("Element at x[%i][%i][%i] = %d\n", i, j, k,
x[i][j][k]);
}
}
}
return (0);
}
Output
Element at x[0][0][0] = 0

Element at x[0][0][1] = 1

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Element at x[0][1][0] = 2

Element at x[0][1][1] = 3

Element at x[0][2][0] = 4

Element at x[0][2][1] = 5

Element at x[1][0][0] = 6

Element at x[1][0][1] = 7

Element at x[1][1][0] = 8

Element at x[1][1][1] = 9

Element at x[1][2][0] = 10

Element at x[1][2][1] = 11

POLYNOMIALS

What is a polynomial?
“A polynomial is a sum of terms, where each term has a form axe , where x is the variable, a
isthe coefficient and e is the exponent.”

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
sincex raised to a power of zero is 1.

Assume there are two polynomials, A(x) = Σ ai xi and B (x) =Σ bi xi


then:
A(x) + B(x) = Σ (ai + bi) xi A(x).B(x)
= Σ (ai xi. Σ (bj xj))

Polynomial Representation
One way to represent polynomials in C is to use typedef to create the type polynomial as

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

below:

#define MAX-DEGREE 101 /*Max degree of polynomial+1*/typedef struct{


int degree;
float coef[MAX-DEGREE];
} polynomial;

Now if a is a variable and is of type polynomial and n < MAX_DEGREE, the polynomialA(x)
= Σai xi would be represented as:
a.degree = n
a.coef[i] = an-i , 0 ≤ i ≤ n

In this representation, the coefficients is stored in order of decreasing exponents, such that
a.coef [i] is the coefficient of xn-i provided a term with exponent n-i exists;
Otherwise, a.coef [i] =0. This representation leads to very simple algorithms for most of the
operations, it wastes a lot of space.

To preserve space an alternate representation that uses only one global array, terms to storeall
polynomials.
The C declarations needed are:

MAX_TERMS 100
typedef struct{
float coef;int expon;
} polynomial; polynomial terms[MAX-TERMS];int avail = 0;

Consider the two polynomials


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

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

 The above figure shows how these polynomials are stored in the array terms. The indexof the
first term of A and B is given by startA and startB, while finishA and finishB give the index of the last
term of A and B.
 The index of the next free location in the array is given by avail.
 For above example, startA=0, finishA=1, startB=2, finishB=5, & avail=6.

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 atposition
avail, attach( ) which places the terms of D into the array, terms.
 If there is not enough space in terms to accommodate D, an error message is printed tothe
standard error device & exits the program with an error condition

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: /* a expon < b expon */
attach (terms [startB].coef, terms[startB].expon);startB++;
break;

case 0: /* equal exponents */


coefficient = terms[startA].coef + terms[startB].coef;

if (coefficient)
attach (coefficient, terms[startA].expon);startA++;
startB++;break;

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

case 1: /* a expon > b expon */


attach (terms [startA].coef, terms[startA].expon);startA++;
}

/* 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-i;

Function to add two polynomials

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;
}

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

SPARSE MATRICES

A matrix contains m rows and n columns of elements as illustrated in below figures. In this
figure, the elements are numbers. The first matrix has five rows and three columns and the
second has six rows and six columns. 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 mn. If m
equals n, the matrix is square.

What is Sparse Matrix?


A matrix which contains many zero entries or very few non-zero entries is called as Sparse
matrix.
In the figure B contains only 8 of 36 elements are nonzero and that is sparse.

Important Note:
A sparse matrix can be represented in 1-Dimension, 2- Dimension and 3- Dimensional array.
When a sparse matrix is represented as a two-dimensional array as shown in
Figure B, more space is wasted.

Example: consider the space requirements necessary to store a 1000 x 1000 matrix that has
only 2000 non-zero elements. The corresponding two-dimensional array requires space for
1,000,000 elements. The better choice is by using a representation in which only the nonzero
elements are stored.

Sparse Matrix Representation


 An element within a matrix can characterize by using the triple <row,col,value> Thismeans
that, an array of triples is used to represent a sparse matrix.
 Organize the triples so that the row indices are in ascending order.
 The operations should terminate, so we must know the number of rows and columns,and the
number of nonzero elements in the matrix.

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Implementation of the Create operation as below:


SparseMatrix Create(maxRow, maxCol) ::=

#define MAX_TERMS 101 /* maximum number of terms +1*/typedef struct {


int col;
int row;int value;
} term;term a[MAX_TERMS];

 The below figure shows the representation of matrix in the array “a” 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. 
 Positions 1 through 8 store the triples representing the nonzero entries. The row index is in
the field row, the column index is in the field col, and the value is in the field value. The triples are
ordered by row and within rows by columns.

a[0] 6 6 8 b[0] 6 6 8
[1] 0 0 15 [1] 0 0 15
[2] 0 3 22 [2] 0 4 91
[3] 0 5 -15 [3] 1 1 11
[4] 1 1 11 [4] 2 1 3
[5] 1 2 3 [5] 2 5 28
[6] 2 3 -6 [6] 3 0 22
[7] 4 0 91 [7] 3 2 -6
[8] 5 2 28 [8] 5 0 -15
Fig (a): Sparse matrix stored as triple Fig (b): Transpose matrix stored as triple

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

Prof.Tharani R , Dept. of AIML , Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

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> inelement <j, i, value>

The columns within each row of the transpose matrix will be arranged in ascending order.
Transpose of a sparse matrix:
void transpose (term a[], termb[])
{ /* 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++;
}
}
}

Prof.Tharani R , Dept. of AIML ,Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

STRING

BASIC TERMINOLOGY:

Each programming languages contains a character set that is used to communicate with the
computer. The character set include the following:
Alphabet: A B C D E F G H I J K L M N O P Q R S T U V W X Y ZDigits:
012345678 9
Special characters: + - / * ( ) , . $ = ‘ _ (Blank space)

String: A finite sequence of zero or more Characters is called string. Length: The number of

characters in a string is called length of 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.
Ex: ‘THE’ // ‘END’ = ‘THEEND’ ‘THE’ // ‘ ’ // ‘END’ = ‘THE END’

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
thenY 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 of ‘THE END’

STRINGS IN C
In C, the strings are represented as character arrays terminated with 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[4]
d o g \0 h o u s e \0

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304
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.

STORING STRINGS

Strings are stored in three types of structures


1. Fixed length structures
2. Variable length structures with fixed maximum
3. Linked structures

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.
Exasm
toprlaeg:eSm
upepdoiusm
e the input consists of the program. Using a record oriented, fixed length
, the input data will appear in memory as pictured below.

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Suppose, if new record needs to be inserted, then it requires that all succeeding records be
movedto new memory location. This disadvantages can be easily remedied as shown in below
figure.

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 advantages 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 datadoes not
exceed the record length)

The main disadvantages are


1. Time is wasted reading an entire record if most of the storage consists of unessential blank
spaces.
2. Certain records may require more space than available
3. When the correction consists of more or fewer characters than the original text, changing a
misspelled word requires record to be changed.

Variable length structures with fixed maximum


The storage of variable-length strings in memory cells with fixed lengths can be done in two
general ways
1. One can use a marker, such as 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

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Example:

The other method to store strings one after another by using some separation marker, such as
the two dollar sign ($$) or by using a pointer giving the location of the string.

These ways of storing strings will save space and are sometimes used in secondary memory
when records are relatively permanent and require little changes.
These types of methods of storage are usually inefficient when the strings and their lengths

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

are frequently being changed.

Linked Storage
 Most extensive word processing applications, strings are stored by means of linked lists.
 In a one way linked list, a linearly ordered sequence of memory cells called nodes, where
each node contains an item called a link, which points to the next node in the list, i.e., which consists the
address of the next node.

Strings may be Stored in linked list as follows:


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

Ex: TO BE OR NOT TO BE

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

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

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

STRING OPERATION

Substring
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’, 3, 7) = 'BE OR N’


SUBSTRING ('THE END', 4, 4) = ' END'

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.

Concatenation
Let S1 and S2 be string. The concatenation of S1 and S2 which is denoted by S1 // S2, is the
stringconsisting of the characters of S1 followed by the character of S2.
Ex:
(a) Suppose S1 = 'MARK' and S2= ‘TWAIN' thenS1 // S2 = ‘MARKTWAIN’

Concatenation is performed in C language using strcat function as shown below

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

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 at the time of
pre- processing

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.

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

PATTERN MATCHING ALGORITHMS

Pattern matching is the problem of deciding whether or not a given string pattern P appears
in astring 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 patternP
with each of the substrings of T, moving from left to right, until a match is found.
WK = SUBSTRING (T, K, LENGTH (P))
 Where, WK denote the substring of T having the same length as P and beginning with theKth
character of T.
 First compare P, character by character, with the first substring, W1. If all the characters are
the same, then P = W1 and so P appears in T and INDEX (T, P) = 1.
 Suppose it is found that some character of P is not the same as the corresponding characterof
W1. Then P ≠ W1
 Immediately move on to the next substring, W2 That is, compare P with W2. If P ≠ W2 then
compare P with W3 and so on.
 The process stops, When P is matched with some substring WK and so P appears in T and
INDEX(T,P) = K or When all the WK'S with no match and hence P does not appear in T.
 The maximum value MAX of the subscript K is equal to LENGTH(T) -LENGTH(P) +1.

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

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Observation of algorithms
 P is an r-character string and T is an s-character string
 Algorithm contains two loops, one inside the other. The outer loop runs through each
successive R-character substring WK = T[K] T[K + 1] ... T[K+R-l] of T.
 The inner loop compares P with WK, character by character. If any character does not match,
then control transfers to Step 5, which increases K and then leads to the next substring ofT.
 If all the R characters of P do match those of some WK then P appears in T and K is the
INDEX of P in T.
 If the outer loop completes all of its cycles, then P does not appear in T and so INDEX
= 0.

Complexity
The complexity of this pattern matching algorithm is equal to O(n2)

STACKS
DEFINITION
“A stack is an ordered list in which insertions (pushes) and deletions (pops) are made at one
end called the top.”
Given a stack S= (a0, ... ,an-1), where a0 is the bottom element, an-1 is the top element, and ai is on top of
element ai-1, 0 < i < n.

Figure: Inserting and deleting elements in a stack

As shown in above figure, the elements are added in the stack in the order A, B, C, D, E,
then E is the first element that is deleted from the stack and the last element is deleted
from stack is A. Figure illustrates this sequence of operations.
Since the last element inserted into a stack is the first element removed, a stack is also
known as a Last-In-First-Out (LIFO) list.

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

ARRAY REPRESENTATION OF STACKS


 Stacks may be represented in the computer in various ways such as one-way linked list (Singly
linked list) or linear array.
 Stacks are maintained by the two variables such as TOP and MAX_STACK_SIZE.
 TOP which contains the location of the top element in the stack. If TOP= -1, then it indicates
stack is empty.
 MAX_STACK_SIZE which gives maximum number of elements that can be stored in stack.

Stack can represented using linear array as shown below

STACK OPERATIONS

Implementation of the stack operations as follows.

1. Stack Create
Stack CreateS(maxStackSize )::=
#define MAX_STACK_ SIZE 100 /* maximum stack size*/ typedef struct
int key;
} element;

{
element stack[MAX_STACK_SIZE]; int top = -1;

The element which is used to insert or delete is specified as a structure that consists of only a
key field.

2. Boolean IsEmpty(Stack)::= top < 0;

3. Boolean IsFull(Stack)::= top >= MAX_STACK_SIZE-1;

The IsEmpty and IsFull operations are simple, and is implemented directly in the program
push and pop functions. Each of these functions assumes that the variables stack and top are
global.

4. Push( )
Function push checks whether stack is full. If it is, it calls stackFull( ), which prints an error
message and terminates execution. When the stack is not full, increment top and assign item
to stack [top].

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304
void push(element item)
{ /* add an item to the global stack */
if (top >= MAX_STACK_SIZE-1)
stackFull(); stack[++top] = item;
}

5. Pop( )
Deleting an element from the stack is called pop operation. The element is deleted only from
the top of the stack and only one element is deleted at a time.

element pop ( )
{ /*delete and return the top element from the stack */ if (top == -1)
return stackEmpty(); /*returns an error key */ return stack[top--];
}

6. stackFull( )
The stackFull which prints an error message and terminates execution.

void stackFull()
{
fprintf(stderr, "Stack is full, cannot add element");
exit(EXIT_FAILURE);
}

STACKS USING DYNAMIC ARRAYS

The array is used to implement stack, but the bound (MAX_STACK_ SIZE) should be known
during compile time. The size of bound is impossible to alter during compilation hence this
can be overcome by using dynamically allocated array for the elements and then increasing
the size of array as needed.

Stack Operations using dynamic array

1. Stack CreateS( )::= typedef struct


{
int key; /* other fields */

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

} element;
element *stack;
MALLOC(stack, sizeof(*stack));
int capacity= 1;
int top= -1;

2. Boolean IsEmpty(Stack)::= top < 0;

3. Boolean IsFull(Stack)::= top >= capacity-1;

4. push()
Here the MAX_STACK_SIZE is replaced with capacity

void push(element item)


{ /* add an item to the global stack */ if (top >= capacity-1)
stackFull(); stack[++top] = item;
}

5. pop( )
In this function, no changes are made.

element pop ( )
{ /* delete and return the top element from the stack */ if (top == -1)
return stackEmpty(); /* returns an error key */ return stack[top--];
}

6. stackFull( )

The new code shown below, attempts to increase the capacity of the array stack so that new
element can be added into the stack. Before increasing the capacity of an array, decide what
the new capacity should be.
In array doubling, array capacity is doubled whenever it becomes necessary to increase the
capacity of an array.

void stackFull()
{
REALLOC (stack, 2*capacity*sizeof(*stack)); capacity *= 2;
}
Stack full with array

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

STACK APPLICATIONS: POLISH NOTATION

Expressions: It is sequence of operators and operands that reduces to a single value after
evaluation is called an expression.
X=a/b–c+d*e–a*c
In above expression contains operators (+, –, /, *) operands (a, b, c, d, e).

Expression can be represented in in different format such as


 Prefix Expression or Polish notation
 Infix Expression
 Postfix Expression or Reverse Polish notation

Infix Expression: In this expression, the binary operator is placed in-between the operand.
The expression can be parenthesized or un- parenthesized.
Example: A + B
Here, A & B are operands and + is operand

Prefix or Polish Expression: In this expression, the operator appears before its operand.
Example: + A B
Here, A & B are operands and + is operand

Postfix or Reverse Polish Expression: In this expression, the operator appears after its operand.
Example: A B +
Here, A & B are operands and + is operand

Precedence of the operators


The first problem with understanding the meaning of expressions and statements is finding out
the order in which the operations are performed.
Example: assume that a =4, b =c =2, d =e =3 in below expression
X=a/b–c+d*e–a*c
((4/2)-2) + (3*3)-(4*2) (4/ (2-2 +3)) *(3-4)*2

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

=0+9-8 OR = (4/3) * (-1) * 2


=1 = -2.66666

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

The first answer is picked most because division is carried out before subtraction, and
multiplication before addition. If we wanted the second answer, write expression differently
using parentheses to change the order of evaluation
X= ((a / ( b – c + d ) ) * ( e – a ) * c

In C, there is a precedence hierarchy that determines the order in which operators are
evaluated. Below figure contains the precedence hierarchy for C.

Post

Pre

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

 The operators are arranged from highest precedence to lowest. Operators with highest
precedence are evaluated first.
 The associativity column indicates how to evaluate operators with the same precedence. For
example, the multiplicative operators have left-to-right associativity. This means that the expression a * b
/ c % d / e is equivalent to ( ( ( ( a * b ) / c ) % d ) / e )
 Parentheses are used to override precedence, and expressions are always evaluated from the
innermost parenthesized expression first

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

INFIX TO POSTFIX CONVERSION

An algorithm to convert infix to a postfix expression as follows:


1. Fully parenthesize the expression.
2. Move all binary operators so that they replace their corresponding right parentheses.
3. Delete all parentheses.

Example: Infix expression: a/b -c +d*e -a*c Fully parenthesized : ((((a/b)-c) + (d*e))-a*c))
:ab/c – d e* + a c*-

Example [Parenthesized expression]: Parentheses make the translation process more difficult because
the equivalent postfix expression will be parenthesis-free.
The expression a*(b +c)*d which results abc +*d* in postfix. Figure shows the translation process.

 The analysis of the examples suggests a precedence-based scheme for stacking and unstacking
operators.
 The left parenthesis complicates matters because it behaves like a low-precedence operator when
it is on the stack and a high-precedence one when it is not. It is placed in the stack whenever it is found in
the expression, but it is unstacked only when its matching right parenthesis is found.
 There are two types of precedence, in-stack precedence (isp) and incoming precedence (icp).

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

The declarations that establish the precedence’s are:


/* isp and icp arrays-index is value of precedence lparen rparen, plus, minus, times, divide,
mod, eos */
int isp[] = {0,19,12,12,13,13,13,0};
int icp[] = {20,19,12,12,13,13,13,0};

void postfix(void)
{
char symbol; precedence token;
int n = 0,top = 0; /* place eos on stack */ stack[0] = eos;
for (token = getToken(&symbol, &n); token != eos; token = getToken(&symbol,& n ))
{
if (token == operand)
printf("%c", symbol); else if (token == rparen)
{
while (stack[top] != lparen) printToken(pop( ));
pop( );
}
else{
while(isp[stack[top]] >= icp[token]) printToken(pop());
push(token);
}
}
while((token = pop ())!= eos) printToken(token);
printf("\n");
}
Program: Function to convert from infix to postfix

Analysis of postfix: Let n be the number of tokens in the expression. Ө (n) time is spent
extracting tokens and outputting them. Time is spent in the two while loops, is Ө (n) as the
number of tokens that get stacked and unstacked is linear in n. So, the complexity of function
postfix is Ө (n).

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

EVALUATION OF POSTFIX EXPRESSION


 The evaluation process of postfix expression is simpler than the evaluation of infix expressions
because there are no parentheses to consider.
 To evaluate an expression, make a single left-to-right scan of it. Place the operands on a stack
until an operator is found. Then remove from the stack, the correct number of operands for the operator,
perform the operation, and place the result back on the stack and continue this fashion until the end of the
expression. We then remove the answer from the top of the stack.

int eval(void)
{
precedence token; char symbol;
int opl,op2, n=0; int top= -1;
token = getToken(&symbol, &n); while(token! = eos)
{
if (token == operand)
push(symbol-'0'); /* stack insert */
else {
op2 = pop(); /* stack delete */ opl = pop();

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

switch(token) {
case plus: push(opl+op2);
break;
case minus: push(opl-op2);
break;
case times: push(opl*op2);
break;
case divide: push(opl/op2);
break;
case mod: push(opl%op2);
}

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

}
token = getToken(&symbol, &n);
}
return pop(); /* return result */
}
Program: Function to evaluate a postfix expression

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

precedence getToken(char *symbol, int *n)


{
*symbol = expr[(*n)++]; switch (*symbol)
{
case '(' : return lparen; case ')' : return rparen; case '+' : return plus; case '-' : return minus; case
'/' : return divide; case '*' : return times; case '%' : return mod; case ' ' : return eos; default:
return operand;
}
}
Program: Function to get a token from the input string
 The function eval ( ) contains the code to evaluate a postfix expression. Since an operand
(symbol) is initially a character, convert it into a single digit integer.
 To convert use the statement, symbol-'0'. The statement takes the ASCII value of symbol and
subtracts the ASCII value of '0', which is 48, from it. For example, suppose symbol = '1. The character '1'
has an ASCII value of 49. Therefore, the statement symbol-'0' produces as result the number 1.
 The function getToken( ), obtain tokens from the expression string. If the token is an operand,
convert it to a number and add it to the stack. Otherwise remove two operands from the stack, perform the
specified operation, and place the result back on the stack. When the end of expression is reached, remove
the result from the stack.

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

RECURSION

A recursive procedure
Suppose P is a procedure containing either a Call statement to itself or a Call statement to a
second procedure that may eventually result in a Call statement back to the original procedure
P. Then P is called a recursive procedure. So that the program will not continue to run
indefinitely, a recursive procedure must have the following two properties:
1. There must be certain criteria, called base criteria, for which the procedure does not call itself.
2. Each time the procedure does call itself (directly or indirectly), it must be closer to the base
criteria.

Recursive procedure with these two properties is said to be well-defined.

A recursive function

A function is said to be recursively defined if the function definition refers to itself. A


recursive function must have the following two properties:
1. There must be certain arguments, called base values, for which the function does not refer to
itself.
2. Each time the function does refer to itself, the argument of the function must be closer to a base
value
A recursive function with these two properties is also said to be well-defined.

Factorial Function
“Theproduct of the positive integers from 1 to n, is called "n factorial" and is denoted by n!”
n! = 1*2 * 3 ... (n - 2)*(n - 1)*n

It is also convenient to define 0! = 1, so that the function is defined for all nonnegative
integers.

Definition: (Factorial Function)


a) If n = 0, then n! = 1.
b) If n > 0, then n! = n*(n - 1)!

Observe that this definition of n! is recursive, since it refers to itself when it uses (n - 1)!
(a) The value of n! is explicitly given when n = 0 (thus 0 is the base value )
(b) The value of n! for arbitrary n is defined in terms of a smaller value of n which is closer to the base
value 0.

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

The following are two procedures that each calculate n factorial .

1. Using for loop: This procedure evaluates N! using an iterative loop process

Procedure: FACTORIAL (FACT, N)


This procedure calculates N! and returns the value in the variable FACT.
1. If N = 0, then: Set FACT: = 1, and Return.
2. Set FACT: = 1. [Initializes FACT for loop.]
3. Repeat for K = 1 to N.
Set FACT: = K*FACT.
[End of loop.]
4. Return.

2. Using recursive function: This is a recursive procedure, since it contains a call to itself

Procedure: FACTORIAL (FACT, N)


This procedure calculates N! and r turns the value in the variable FACT.
1. If N = 0, then: Set FACT: = 1, and Return.
2. Call FACTORIAL (FACT, N - 1).
3. Set FACT: = N*FACT.
4. Return.

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

GCD
The greatest common divisor (G botD
h ) of two integers m and n is the greatest integer that
divides
m and n with no remainder.
Procedure: GCD (M, N)
1. If (M % N) = 0, then set GCD=N and RETURN
2. Call GCD (N, M % N)
3. Return

Fibonacci Sequence

The Fibonacci sequence (usually denoted by F0, F1, F2.) is as


follows:0, 1, 1, 2,3,5,8, 13, 21, 34, 55

That is, F0 = 0 and F1 = 1 and each succeeding term is the sum of the two preceding
terms.

The rules of the game are as follows:


1. Only one disk may be moved at a time. Only the top disk on any peg may be moved to
any otherpeg.
2. At no time can a larger disk be placed on a smaller disk.

Definition: (Fibonacci
Sequence) If n = 0
or n = 1, then Fn = n
If n > 1, then Fn= Fn-2+
Fn-1

Here
Thebase values are 0 and 1
The value of Fn is defined in terms of smaller values of n which are closer to the base values.

A procedure for finding the nth term Fn of the Fibonacci sequence follows.

Procedure: FIBONACCI (FIB, N)


This procedure calculates FN and returns the value in the first
parameter FIB.If N = 0 or N = 1, then: Set FIB: = N, and
Return.
Call FIBONACCI
(FIBA, N - 2). Call
Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering
Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

FIBONACCI (FIBB, N -
I). Set FIB: = FIBA +
FIBB.
Return.

Tower of Hanoi

Problem description

Suppose three pegs, labeled A, Band C, are given, and suppose on peg A a finite number
n ofdisks with decreasing size are placed.

The objective of the game is to move the disks from peg A to peg C using peg B as an auxiliary.

We write A→B to denote the instruction "Move top disk from peg A to peg B"

Example: Towers of Hanoi problem for n = 3.

Solution: Observe that it consists of the following seven moves

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

1. Move top disk from peg A to peg C.


2. Move top disk from peg A to peg B.
3. Move top disk from peg C to peg B.
4. Move top disk from peg A to peg C.
5. Move top disk from peg B to peg A.
6. Move top disk from peg B to peg C.
7. Move top disk from peg A to peg C.

In other words,
n=3: A→C, A→B, C→B, A→C, B→A, B→C, A→C

For completeness, the solution to the Towers of Hanoi problem for n = 1 and n = 2 n=l:
A→Cn=2: A→B, A→C, B→C
The Towers of Hanoi problem for n > 1 disks may be reduced to the following sub-
problems:
(1) Move the top n - 1 disks from peg A to peg B
(2) Move the top disk from peg A to peg C: A→C.
(3) Move the top n - 1 disks from peg B to peg C.

The general notation


 TOWER (N, BEG, AUX, END) to denote a procedure which moves the top n disks from the
initial peg BEG to the final peg END using the peg AUX as an auxiliary.

 When n = 1, the solution:


TOWER (1, BEG, AUX, END) consists of the single instruction BEG→END

 When n > 1, the solution may be reduced to the solution of the following three sub-
problems:
(a) TOWER (N - I, BEG, END, AUX)
(b) TOWER (l, BEG, AUX, END) or BEG → END
(c) TOWER (N - I, AUX, BEG, END)

Procedure: TOWER (N, BEG, AUX, END)


This procedure gives a recursive solution to the Towers of Hanoi problem for N disks.
1. If N=l, then:
(a) Write: BEG →END.
(b) Return.
[End of If structure.]
2. [Move N - 1 disks from peg BEG to peg AUX.] Call TOWER (N - 1, BEG, END, AUX).
3. Write: BEG →END.
4. [Move N - 1 disks from peg AUX to peg END.] Call TOWER (N - 1, AUX, BEG, END).
5. Return.

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering


Regulation – 2022(CBCS Scheme) Data Structures and Applications BCS304

Example: Towers of Hanoi problem for n = 4

Ackermann function
The Ackermann function is a function with two arguments each of which can be assigned
any nonnegative integer: 0, 1, 2, ....

Definition: (Ackermann Function)


(a) If m = 0, then A (m, n) = n + 1.
(b) If m ≠ 0 but n = 0, then A(m, n)= A(m - 1, 1)
(c) If m ≠ 0 and n ≠ 0, then A(m, n) = A(m - 1, A(m, n - 1))

Prof.Tharani R, Dept. of AIML, Sri Sairam College of Engineering

You might also like