Data Structure
Data Structure
Data Structure
INTRODUCTION
Anything that can store data can be called as a data structure, hence
Integer, Float, Boolean, Char etc, all are data structures. They are known as
Primitive Data Structures and some complex Data Structures, which are used
to store large and connected data. They are called Non-primitive Data Structures.
Examples:
Array
Stack
Queue
Linked List
Tree
Graph
It can be classified as
Figure 4: Graph
DATA STRUCTURE OPERATIONS
The data in the data structures are processed by certain operations. The
particular data structure chosen largely depends on the frequency of the
operation that needs to be performed on the data structure.
• Traversing
• Searching
• Insertion
• Deletion
• Sorting
• Merging
(1) Traversing: Accessing each record exactly once so that certain items in
the record may be processed.
STRUCTURES DEFINITION
“Structure is a collection of data items of same or dissimilar data type. Each
data item is identified by its name and type. (Or) A Structure is a user defined data
type that can store related information together. (Or) A structure is a collection of
different data items / heterogeneous data items under a single name.” The variable
within a structure are of different data types and each has a name that is used to
select it from the structure.
C arrays allow you to define type of variables that can hold several data items
of the same kind but structure is another user defined data type available in C
programming, which allows you to combine data items of different kinds. Structures
are used to represent a record, Suppose, track of books are kept in a library are
recorded then it is required to track the following attributes about each book:
• Title
• Author
• Subject
• Book ID
STRUCTURE INITIALIZATION
Assigning values to the data members of the structure is called initializing of
structure.
Syntax:
struct struct_name
{
data _type member_name1;
data _type member_name2;
} struct_var={constant1,constant2};
Program to create a struct person , initializes its data member and print their
values
#include<stdio.h>
#include<conio.h>
ARRAY OF STRUCTURE
An array of structure can also be declared. Each element of the array
representing a structure variable.Example : struct employee emp[5];
The above code define an array emp of size 5 elements. Each element of array emp
is of type employee
#include<stdio.h>
#include<conio.h>
struct employee
{
char ename[10];
int sal;
};
struct employee emp[5];
int i,j;
void ask()
{
Here humanbeing is the name of the type defined by structure definition and we
may follow this definition with declarations of variables such as humanbeing
person1, person2;
Structures cannot be directly checked for equality or nor equality. i.e. directly
using person1==person2 is not allowed. If each individual data member is
checked for equality then the entire structure can be checked for equality.
POINTERS TO STRUCTURES
Pointer to a structure is a variable that holds the address of a structure. The
syntax to declare pointer to a structure can be given as:
strcut struct_name *ptr;
eg: struct stud *ptr_stud;
To assign address of stud to the pointer using address operator(&) we would write
ptr_stud=&stud;
To access the members of the structure (->) operator is used.
for example
Ptr_stud->name=Raj;
NESTED STRUCTURE
typedef struct
{
char name[10];
int age;
salary;
date dob;
}humanbeing;
If humanbeing person1 and person2 declares person1 and person2 variables
of type humanbeing. Then consider a person born on feb 11, 1944, can have the
values for the date struct set as:
person1.dob.month=2;
person1.dob.day=11;
person1.dob.year=1944;
Similarly for considering person2, his dob is 3rd December 1956 then
person2.dob.month=12;
person2.dob.day=3;
person2.dob.year=1956;
typedef struct
{
char name[10];
int age;
salary;
date dob;
}humanbeing;
humanbeing person1;
strcpy(person1.name,"james");
person1.age=10;
person1.salary=35000;
person1.dob.month=2;
person1.dob.day=11;
person1.dob.year=1944;
printf(“\n details of the person”);
printf("\n name=%s age=%d salary=%f dob=%d-%d-%d",person1.name,
person1.age,person1.salary,Person1.dob.day,Person1.dob.month,Person1.do
b.year);
getch();
}
UNIONS
Declaring Union
union union-name
{
data_type1 var-name1;
data_type2 var-name2;
.. ..
data_typen var_namen;
};
Example:
Union data
{
char a;
int x;
float f;
}mydata;
The union tag is optional and each member definition is a normal variable
definition, such as int i; or float f; or any other valid variable definition.
At the end of the union's definition, before the final semicolon, you can specify
one or more union variables but it is optional. The memory occupied by a union will
be large enough to hold the largest member of the union. For example, in above
example Data type will occupy 4 bytes of memory space because this is the
maximum space which can be occupied by float data.
Example 1
struct node
{
int val;
struct node*next;
};
Example 2:
typedef struct
{
Char data;
Struct list * link;
}list;
The value 10 can be accessed by either using the variable name a or the
address 80F.Since the memory addresses are simply numbers they can be assigned
to some other variable. The variable that holds memory address are called pointer
variables. A pointer variable is therefore nothing but a variable that contains an
address, which is a location of another variable. Value of pointer variable will be
stored in another memory location.
Dereferencing of Pointer
Once a pointer has been assigned the address of a variable. To access the
value of variable, pointer is dereferenced, using the indirection operator * .
int a,*p;
a = 10;
p = &a;
printf("%d",*p); //this will print the value of a.
printf("%d",*&a); //this will also print the value of a.
printf("%u",&a); //this will print the address of a.
printf("%u",p); //this will also print the address of a.
printf("%u",&p); //this will also print the address of p.
Array of Pointers
Array of pointers can also be declared. Pointers are very helpful in handling
character array with rows of varying length.
char *name[3]={
"Adam",
"chris",
"Deniel"
};
//Now see same array without using pointer
char name[3][20]= {
"Adam", "chris",
"Deniel"
};