You are on page 1of 43

DATA STRUCTURES AND

ALGORITHMS
INTRODUCTION TO DATA STRUCTURE
 Data structure is the structural representation of
logical relationships between elements of data. In
other words a data structure is a way of
organizing data items by considering its
relationship to each other.
 Data structure mainly specifies the structured
organization of data, by providing accessing
methods with correct degree of associativity.
Data structure affects the design of both the
structural and functional aspects of a program.
Algorithm + Data Structure = Program

 Data structures are the building blocks of a


program; here the selection of a particular data
structure will help the programmer to design
more efficient programs as the complexity and
volume of the problems solved by the computer is
steadily increasing day by day. The programmers
have to strive hard to solve these problems. If the
problem is analyzed and divided into sub
problems, the task will be much easier i.e.,
divide, conquer and combine.
ALGORITHM
 In the first stage, modeling, we try to represent
the problem using an appropriate mathematical
model such as a graph, tree etc. At this stage, the
solution to the problem is an algorithm expressed
very informally.
 At the next stage, the algorithm is written in
pseudo-language (or formal algorithm) that is, a
mixture of any programming language constructs
and less formal English statements. The
operations to be performed on the various types
of data become fixed.
 In the final stage we choose an implementation
for each abstract data type and write the
procedures for the various operations on that
type. The remaining informal statements in the
pseudo-language algorithm are replaced by (or
anyprogramming language) C/C++ code.
TYPES OF PROGRAMMING METHODOLOGIES
MODULAR PROGRAMMING
 Modular Programming is heavily procedural. The
focus is entirely on writing code (functions). Data
is passive in Modular Programming. Any code
may access the contents of any data structure
passed to it. (There is no concept of
encapsulation.) Modular Programming is the act
of designing and writing programs as functions,
that each one performs a single well-defined
function, and which have minimal interaction
between them. That is, the content of each
function is cohesive, and there is low coupling
between functions.
 Modular Programming discourages the use of
control variables and flags in parameters; their
presence tends to indicate that the caller needs to
know too much about how the function is
implemented. It encourages splitting of
functionality into two types: “Master” functions
controls the program flow and primarily contain
calls to “Slave” functions that handle low-level
details, like moving data between structures.
TWO METHODS OF MODULAR
PROGRAMMING
TOP-DOWN ALGORITHM DESIGN
 The principles of top-down design dictates that a
program should be divided into a main module
and its related modules. Each module should also
be divided into sub modules according to software
engineering and programming style. The division
of modules processes until the module consists
only of elementary process that are intrinsically
understood and cannot be further subdivided.
 Top-down algorithm design is a technique for
organizing and coding programs in which a
hierarchy of modules is used, and breaking the
specification down into simpler and simpler
pieces, each having a single entry and a single
exit point, and in which control is passed
downward through the structure without
unconditional branches to higher levels of the
structure. That is top-down programming tends
to generate modules that arebased on
functionality, usually in the form of functions or
procedures or methods.
 In C, the idea of top-down design is done using
functions. A C program is made of one or more
functions, one and only one of which must be
named main. The execution of the program
always starts and ends with main, but it can call
other functions to do special tasks.
BOTTOM-UP ALGORITHM DESIGN
 Bottom-up algorithm design is the opposite of
top-down design. It refers to a style of
programming where an application is constructed
starting with existing primitives of the
programming language, and constructing
gradually more and more complicated features,
until the all of the application has been written.
That is, starting the design with specific modules
and build them into more complex structures,
ending at the top.
 The bottom-up method is widely used for testing,
because each of the lowest-level functions is
written and tested first. This testing is done by
special test functions that call the low-level
functions, providing them with different
parameters and examining the results for
correctness. Once lowest-level functions have
been tested and verified to be correct, the next
level of functions may be tested. Since the lowest-
level functions already have been tested, any
detected errors are probably due to the higher-
level functions. This process continues, moving
up the levels, until finally the main function is
tested.
STRUCTURED PROGRAMMING
 It is a programming style; and this style of
programming is known by several names:
Procedural decomposition, Structured
programming, etc. Structured programming is
not programming with structures but by using
following types of code structures to write
programs:
 1. Sequence of sequentially executed statements
 2. Conditional execution of statements (i.e., “if”
statements)
 3. Looping or iteration (i.e., “for, do...while, and while”
statements)
 4. Structured subroutine calls (i.e., functions) In
particular, the following language usage is forbidden:
 • “GoTo” statements
 • “Break” or “continue” out of the middle of loops
 • Multiple exit points to a
function/procedure/subroutine (i.e., multiple
“return”statements)
 • Multiple entry points to a
function/procedure/subroutine/method
ABSTRACT DATA TYPE - ADT
ADT consists of a set a set of
definitions that allow
programmers to use the functions
while hiding the implementation.
 ADT based on the concepts :
 Yourknowledge of data structures is
one your tools.=> to allow us to not
only read the structure but convert it
into different data structures.
CONT….
 We need to be concerned only with what it can do,
how it is done is hidden.
 ADT is a data declaration packaged together with
the operation that are meaningful on the data type
 ADT Class = ADT + Encapsulation (encapsulate the
data and the operations on data and we hide them
from the user).
CONT…
Figure 1-3
SYNTAX
struct name {
member1;
member2;
.
.
};
-------- Inside function -------
struct name *ptr;
EXAMPLE
 struct date {
int month;
int day;
int year;
};
struct date todays_date;
CONT….
 #include <iostream>

 using namespace std;

 struct date { /* global definition of type date */


 int month;
 int day;
 int year;
 };
 int main(){
 struct date today;
 today.month = 10;
 today.day = 14;
 today.year = 1995;
 cout<<"Todays date is,"<<today.month<<
today.day<<today.year;
 }
ARRAYS OF STRUCTURES
Consider the following,
struct date {
int month, day, year;
};
 Lets now create an array called birthdays of the
same data type as the structure date
struct date birthdays[5];
 This creates an array of 5 elements which have
the structure of date.
birthdays[1].month = 12;
CONT….
 #include <iostream>
 #include<string.h>

 using namespace std;

 struct month {
 int number_of_days;
 char name[4];
 };
 int main(){
 static struct month this_month = { 31, "Jan" };
 this_month.number_of_days = 31;
 strcpy( this_month.name, "Jan" );

 cout<<"The month:"<<this_month.name;
 }
VARIATIONS IN DECLARING
STRUCTURES
 Consider the following,
struct date {
int month, day, year;
} todays_date, purchase_date;
 or another way is,
struct date {
int month, day, year;
} todays_date = { 9,25,1985 };
 or, how about an array of structures similar to date,
struct date {
int month, day, year;
} dates[100];
EXAMPLE
#include <iostream>
#include<string.h>

using namespace std;

struct kid{
char name[100];
int age;
int grade;
}kids,kiddie;

int main(){
cout<<"Enter Name:";
cin>>kids.name;
cout<<"Enter Grade:";
cin>>kiddie.grade;
cout<<kids.name<<endl;
cout<<kiddie.grade<<endl;
}
CONT…
#include <iostream>
#include<string.h>

using namespace std;

struct kid{
char initial;
int age;
int grade;
}kids[100];

int main(){
kids[0].age=3;
cout<<kids[0].age;
}
CONT…
struct kid {
char initial;
int age;
int grade;
} kids[12];
void main(){
int index;
for (index = 0; index < 12; index++) {
kids[index].initial = 'A' + index;
kids[index].age = 16;
kids[index].grade = 84;
}
kids[3].age = kids[5].age = 17;
kids[2].grade = kids[6].grade = 92;
kids[4].grade = 57;
kids[10] = kids[4]; /* Structure assignment */
for (index = 0; index < 12; index++)
printf("%c is %d years old and got a grade of %d\n",
kids[index].initial, kids[index].age,
kids[index].grade);
}
CONT….
struct month {
int number_of_days;
char name[4];
}this_month = { 31, "Jan" };

void main(){
clrscr();
this_month.number_of_days = 31;
strcpy( this_month.name, "Jan" );
printf("The month is %s\n", this_month.name );
getch();
}
STRUCTURE AND POINTER
 #include<stdio.h>
#include<stdio.h>
 #include<conio.h>
#include<conio.h>

 struct st{
struct st{
 int id;
int id;
 char *name;
char *name;
 char *address;
char *address;

 };
};
 struct st employee, *stptr;
struct st employee, *stptr;
 void main(){
void main(){
 clrscr();
clrscr();
 stptr=&employee;
*stptr=employee;
 stptr->id=1;
stptr->id=1;
 printf("%d",stptr->id);
printf("%d",stptr->id);
 getch();
getch();
 }
}
CONT….
 #include<stdio.h>
 #include<conio.h>

 struct st{
 int id;
 char *name;
 char *address;

 };
 struct st employee, *stptr[100];
 void main(){
 clrscr();
 *stptr[100]=employee;
 stptr[0]->id=10;
 printf("%d",stptr[0]->id);
 getch();
 }
CONT….
 struct st{
 int id[10];
 char name[20];
 char address[20];

 };
 struct st employee, *stptr[100];
 void main(){
 int input;
 clrscr();
 *stptr[100]=employee;
 printf("How many person you want to registere?::");
 scanf("%d",&input);
 for(int i=0;i<=input;i++){
 cprintf("\n Enter age::");
 fflush(stdin);scanf("%d",&stptr[i]->id[i]);
 printf("\n Enter Name::");
 fflush(stdin);scanf("%s",stptr[i]->name[i]);

 //clrscr();
 }
 for(int i1=0;i1<=1;i1++){

 printf("%d %s \n",stptr[i1]->id[i1],stptr[i1]->name[i1]);
 }
 getch();
 }
STRUCTURE WHICH CONTAIN STRUCTURE
 struct person {
 char name[25];
 int age;
 char status; /* M = married, S = single */
 };
 struct alldat {
 int grade;
 struct person descrip;
 char lunch[25];
 } student[53];
 struct alldat teacher,sub;
 teacher.grade = 94;
 teacher.descrip.age = 34;
 teacher.descrip.status = 'M';
 strcpy(teacher.descrip.name,"Mary Smith");
 strcpy(teacher.lunch,"Baloney sandwich");
 sub.descrip.age = 87;
 sub.descrip.status = 'M';
 strcpy(sub.descrip.name,"Old Lady Brown");
 sub.grade = 73;
 strcpy(sub.lunch,"Yogurt and toast");
 student[1].descrip.age = 15;
 student[1].descrip.status = 'S';
 strcpy(student[1].descrip.name,"Billy Boston");
 strcpy(student[1].lunch,"Peanut Butter");
 student[1].grade = 77;
CONT….
struct date {
int month, day, year;
};
struct time {
int hours, mins, secs;
};
struct date_time {
struct date sdate;
struct time stime;
};
struct date_time today;
THE HEAP
 Every compiler has a set of limitations on it that define how big the
executable file can be, how many variables can be used, how long the
source file can be, etc. One limitation placed on users by the C compiler
is a limit of 64K for the executable code if you happen to be in the small
memory model. This is because the IBM-PC uses a microprocessor with
a 64K segment size, and it requires special calls to use data outside of a
single segment. In order to keep the program small and efficient, these
calls are not used, and the memory space is limited but still adequate
for most programs.
 The data and variables are put on the heap by the system as calls to
malloc are made. The system keeps track of where the data is stored.
Data and variables can be deallocated as desired leading to holes in
the heap. The system knows where the holes are and will use them for
additional data storage as more malloc calls are made. The structure of
the heap is therefore a very dynamic entity, changing constantly.
MALLOC
• malloc() allocates a block of memory on the heap
and returns a pointer to the block.
 The function malloc is used to allocate a certain
amount of memory during the execution of a
program. The malloc function will request a block
of memory from the heap. If the request is
granted, the operating system will reserve the
requested amount of memory.
 When the amount of memory is not needed
anymore, you must return it to the operating
system by calling the function free.
EXAMPLE
#include<stdio.h>
struct rec {
int i;
float PI;
char A;
};
int main()
{
struct rec *ptr_one;
ptr_one =(struct rec *) malloc (sizeof(struct rec));
ptr_one->i = 10;
ptr_one->PI = 3.14;
ptr_one->A = 'a';
printf("First value: %d\n", ptr_one->i);
printf("Second value: %f\n", ptr_one->PI);
printf("Third value: %c\n", ptr_one->A);
free(ptr_one);
return 0; }
CONT….
#include<string.h>
#include<stdlib.h>

struct st{
int id[10];
char name[20];
char address[20];

};
struct st employee, *stptr[100];
void main(){
int input;
clrscr();
//*stptr[100]=employee;
stptr[100]=(struct st *)malloc (sizeof(struct st));
printf("How many person you want to registere?::");
scanf("%d",&input);
for(int i=0;i<=input;i++){
cprintf("\n Enter age::");
fflush(stdin);scanf("%d",&stptr[i]->id[i]);
printf("\n Enter Name::");
fflush(stdin);scanf("%s",stptr[i]->name[i]);

}
for(int i1=0;i1<=1;i1++){

printf("%d %s \n",stptr[i1]->id[i1],stptr[i1]->name[i1]);
}
free(stptr[100]);
getch();
}
STRUCTURE AND FUNCTION

 In C, structure can be passed to functions by two


methods:
 Passing by value (passing actual value as
argument)
 Passing by reference (passing address of
an argument)
PASSING STRUCTURE BY VALUE

 A structure variable can be passed to the function


as an argument as normal variable. If structure
is passed by value, change made in structure
variable in function definition does not reflect in
original structure variable in calling function.

You might also like