Professional Documents
Culture Documents
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
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>
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>
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