Professional Documents
Culture Documents
Unit 1
Introduction
Data Structure can be defined as the group of data elements which provides an efficient way of
storing and organising data in the computer so that it can be used efficiently. Some examples of
Data Structures are arrays, Linked List, Stack, Queue, etc. Data Structures are widely used in
almost every aspect of Computer Science i.e. Operating System, Compiler Design, Artifical
intelligence, Graphics and many more.
Data Structures are the main part of many computer science algorithms as they enable the
programmers to handle the data in an efficient way. It plays a vital role in enhancing the
performance of a software or a program as the main function of the software is to store and
retrieve the user's data as fast as possible
Basic Terminology
Data structures are the building blocks of any program or the software. Choosing the appropriate
data structure for a program is the most difficult task for a programmer. Following terminology
is used as far as data structures are concerned
Data: Data can be defined as an elementary value or the collection of values, for example,
student's name and its id are the data about the student.
Group Items: Data items which have subordinate data items are called Group item, for example,
name of a student can have first name and the last name.
Record: Record can be defined as the collection of various data items, for example, if we talk
about the student entity, then its name, address, course and marks can be grouped together to
form the record for the student.
File: A File is a collection of various records of one type of entity, for example, if there are 60
employees in the class, then there will be 20 records in the related file where each record
contains the data about each employee.
Attribute and Entity: An entity represents the class of certain objects. it contains various
attributes. Each attribute represents the particular property of that entity.
As applications are getting complexed and amount of data is increasing day by day, there may
arrise the following problems:
Processor speed: To handle very large amout of data, high speed processing is required, but as
the data is growing day by day to the billions of files per entity, processor may fail to deal with
that much amount of data.
Data Search: Consider an inventory size of 106 items in a store, If our application needs to
search for a particular item, it needs to traverse 106 items every time, results in slowing down the
search process.
Multiple requests: If thousands of users are searching the data simultaneously on a web server,
then there are the chances that a very large server can be failed during that process
in order to solve the above problems, data structures are used. Data is organized to form a data
structure in such a way that all items are not required to be searched and required data can be
searched instantly.
Efficiency: Efficiency of a program depends upon the choice of data structures. For example:
suppose, we have some data and we need to perform the search for a particular record. In that
case, if we organize our data in an array, we will have to search sequentially element by element.
hence, using array may not be very efficient here. There are better data structures which can
make the search process efficient like ordered array, binary search tree or hash tables.
Reusability: Data structures are reusable, i.e. once we have implemented a particular data
structure, we can use it at any other place. Implementation of data structures can be compiled
into libraries which can be used by different clients.
Abstraction: Data structure is specified by the ADT which provides a level of abstraction. The
client program uses the data structure through interface only, without getting into the
implementation details.
Data Structure Classification
Linear Data Structures: A data structure is called linear if all of its elements are arranged in the
linear order. In linear data structures, the elements are stored in non-hierarchical way where each
element has the successors and predecessors except the first and last element.
The elements of array share the same variable name but each one carries a different index
number known as subscript. The array can be one dimensional, two dimensional or
multidimensional.
Linked List: Linked list is a linear data structure which is used to maintain a list in the memory.
It can be seen as the collection of nodes stored at non-contiguous memory locations. Each node
of the list contains a pointer to its adjacent node.
Value of Reference
element part
Stack: Stack is a linear list in which insertion and deletions are allowed only at one end,
called top.
A stack is an abstract data type (ADT), can be implemented in most of the programming
languages. It is named as stack because it behaves like a real-world stack, for example: - piles of
plates or deck of cards etc.
Queue: Queue is a linear list in which elements can be inserted only at one end called rear and
deleted only at the other end called front.
It is an abstract data structure, similar to stack. Queue is opened at both end therefore it follows
First-In-First-Out (FIFO) methodology for storing the data items.
Non Linear Data Structures: This data structure does not form a sequence i.e. each item or
element is connected with two or more other items in a non-linear arrangement. The data
elements are not arranged in sequential structure.
Trees: Trees are multilevel data structures with a hierarchical relationship among its elements
known as nodes. The bottommost nodes in the hierarchy are called leaf node while the topmost
node is called root node. Each node contains pointers to point adjacent nodes.
Tree data structure is based on the parent-child relationship among the nodes. Each node in the
tree can have more than one children except the leaf nodes whereas each node can have atmost
one parent except the root node. Trees can be classfied into many categories which will be
discussed later in this tutorial.
Graphs: Graphs can be defined as the pictorial representation of the set of elements (represented
by vertices) connected by the links known as edges. A graph is different from tree in the sense
that a graph can have cycle while the tree can not have the one.
1) Traversing: Every data structure contains the set of data elements. Traversing the data
structure means visiting each element of the data structure in order to perform some specific
operation like searching or sorting.
Example: If we need to calculate the average of the marks obtained by a student in 6 different
subject, we need to traverse the complete array of marks and calculate the total sum, then we will
devide that sum by the number of subjects i.e. 6, in order to find the average.
2) Insertion: Insertion can be defined as the process of adding the elements to the data structure
at any location.
If the size of data structure is n then we can only insert n-1 data elements into it.
3) Deletion:The process of removing an element from the data structure is called Deletion. We
can delete an element from the data structure at any random location.
4) Searching: The process of finding the location of an element within the data structure is
called Searching. There are two algorithms to perform searching, Linear Search and Binary
Search. We will discuss each one of them later in this tutorial.
5) Sorting: The process of arranging the data structure in a specific order is known as Sorting.
There are many algorithms that can be used to perform sorting, for example, insertion sort,
selection sort, bubble sort, etc.
6) Merging: When two lists List A and List B of size M and N respectively, of similar type of
elements, clubbed or joined to produce the third list, List C of size (M+N), then this process is
called merging
What is an Algorithm?
Characteristics of an Algorithm
o Input: An algorithm has some input values. We can pass 0 or some input value to an
algorithm.
o Output: We will get 1 or more output at the end of an algorithm.
o Unambiguity: An algorithm should be unambiguous which means that the instructions in
an algorithm should be clear and simple.
o Finiteness: An algorithm should have finiteness. Here, finiteness means that the
algorithm should contain a limited number of instructions, i.e., the instructions should be
countable.
o Effectiveness: An algorithm should be effective as each instruction in an algorithm
affects the overall process.
o Language independent: An algorithm must be language-independent so that the
instructions in an algorithm can be implemented in any of the languages with the same
output.
Dataflow of an Algorithm
o Problem: A problem can be a real-world problem or any instance from the real-world
problem for which we need to create a program or the set of instructions. The set of
instructions is known as an algorithm.
o Algorithm: An algorithm will be designed for a problem which is a step by step
procedure.
o Input: After designing an algorithm, the required and the desired inputs are provided to
the algorithm.
o Processing unit: The input will be given to the processing unit, and the processing unit
will produce the desired output.
o Output: The output is the outcome or the result of the program.
Let's understand the algorithm through a real-world example. Suppose we want to make a lemon
juice, so following are the steps required to make a lemon juice:
Step 2: Squeeze the lemon as much you can and take out its juice in a container.
Step 5: When sugar gets dissolved, add some water and ice in it.
The above real-world can be directly compared to the definition of the algorithm. We cannot
perform the step 3 before the step 2, we need to follow the specific order to make lemon juice.
An algorithm also says that each and every instruction should be followed in a specific order to
perform a specific task.
The following are the steps required to add two numbers entered by the user:
Step 1: Start
Step 4: Add the values of a and b and store the result in the sum variable, i.e., sum=a+b.
Step 6: Stop
Factors of an Algorithm
The following are the factors that we need to consider for designing an algorithm:
o Modularity: If any problem is given and we can break that problem into small-small
modules or small-small steps, which is a basic definition of an algorithm, it means that
this feature has been perfectly designed for the algorithm.
o Correctness: The correctness of an algorithm is defined as when the given inputs
produce the desired output, which means that the algorithm has been designed algorithm.
The analysis of an algorithm has been done correctly.
o Maintainability: Here, maintainability means that the algorithm should be designed in a
very simple structured way so that when we redefine the algorithm, no major change will
be done in the algorithm.
o Functionality: It considers various logical steps to solve the real-world problem.
o Robustness: Robustness means that how an algorithm can clearly define our problem.
o User-friendly: If the algorithm is not user-friendly, then the designer will not be able to
explain it to the programmer.
o Simplicity: If the algorithm is simple then it is easy to understand.
o Extensibility: If any other algorithm designer or programmer wants to use your
algorithm then it should be extensible.
Importance of Algorithms
Issues of Algorithms
The following are the issues that come while designing an algorithm:
ALGORITHM SPECIFICATION
PSEUDOCODECONVENTIONS
1. Comments begin with // and continue until the end of the line./* */
3.An identifier (= data type) begins with a letter. Simple types are assumed: Integer, float, char,
boolean, and so on. Compound data types can be formed with records:
node = record
{ datatype_1 data_1;
…
datatype_ndata_n;
node *link;
}
In the example, link is a pointer to the record type node.
5. There are two Boolean values true and false. In order to produce these values, the logical
operators and, or, and not and the relational operators <, ≤ , ≠ , =, ≥, > are provided.
6. Elements of multidimensional arrays are accessed using [ and ]. For example, if A is a two-
dimensional array, the (i,j)th element of the array is denoted A[i,j].
7. The following looping statements are involved: for, while, repeat, until.
1 The while loop takes the following form:
while<condition> do
{
<statement 1>
…
<statement n>
}
As long as <condition> is true, the statements get executed. When <condition> becomes false,
the loop is exited.
The instruction break; can be used within any looping instruction to force exit. In case of
nested loops, break; results in the exit of the innermost loop that is part of it. A return
statement within any of the above loops also will result in exiting the loops. A return statement
results in the exit of the function itself.
EXAMPLE: SELECTIONSORT
We must devise an algorithm that sorts a collection of n≥1 elements of arbitrary type.
Solution: From those elements that are currently unsorted, find the smallest and place it next in
the sorted list.
Although the statement adequately describes the sorting problem, it is not an algorithm
because it leaves several questions unanswered. For example, it does not tell us where and how
the elements are initially stored or where we should place the result. We assume that the
elements are stored in an array a, such that i th integer is stored in the i th position a[i], 1 ≤ i≤ n.
This is our first attempt:
1.for i:= 1 to ndo
2.{
3.Examine a[i] to a[n] and suppose
4.the smallest element is a[j];
5.Interchange a[i] with a[j];
6.}
To turn the algorithm into a pseudocode program, two subtasks remain: Finding the
smallest element.
This task is solved by assuming that a[i] is the minimum, and then comparing a[i] with
a[i+1], a[i+2],..., and whenever a smaller element is found, regarding it as a new minimum.
Eventually a[n] is compared with current minimum and we are done.
Defining “interchange”:
Define t:= a[i]; a[i] = a[j]; a[j]:= t.
1.Algorithm SelectionSort(a,n)
2.// Sort the array a[1:n] into non decreasing order
3.{
4.for i:= 1 to n do
5.{
6. j := i;
7.for k := i+1 to n do
8.if k:= (a[k] < a[j]) then j := k;
9. t:=a[i]; a[i]:=a[j]; a[j]:= t;
10.}
11.}
Approaches of Algorithm
The following are the approaches used after considering both the theoretical and practical
importance of designing an algorithm:
Algorithm Analysis
The algorithm can be analyzed in two levels, i.e., first is before creating the algorithm, and
second is after creating the algorithm. The following are the two analysis of an algorithm:
o Priori Analysis: Here, priori analysis is the theoretical analysis of an algorithm which is
done before implementing the algorithm. Various factors can be considered before
implementing the algorithm like processor speed, which has no effect on the
implementation part.
o Posterior Analysis: Here, posterior analysis is a practical analysis of an algorithm. The
practical analysis is achieved by implementing the algorithm using any programming
language. This analysis basically evaluate that how much running time and space taken
by the algorithm.
Algorithm Complexity
1. sum=0;
2. // Suppose we have to calculate the sum of n numbers.
3. for i=1 to n
4. sum=sum+i;
5. // when the loop ends then sum holds the sum of the n numbers
6. return sum;
In the above code, the time complexity of the loop statement will be atleast n, and if the value of
n increases, then the time complexity also increases. While the complexity of the code, i.e.,
return sum will be constant as its value is not dependent on the value of n and will provide the
result in one step only. We generally consider the worst-time complexity as it is the maximum
time taken for any given input size.
Auxiliary space: The extra space required by the algorithm, excluding the input size, is known as
an auxiliary space. The space complexity considers both the spaces, i.e., auxiliary space, and
space used by the input.
So,
Types of Algorithms
o Search Algorithm
o Sort Algorithm
Search Algorithm
On each day, we search for something in our day to day life. Similarly, with the case of
computer, huge data is stored in a computer that whenever the user asks for any data then the
computer searches for that data in the memory and provides that data to the user. There are
mainly two techniques available to search the data in an array:
o Linear search
o Binary search
Linear Search
Linear search is a very simple algorithm that starts searching for an element or a value from the
beginning of an array until the required element is not found. It compares the element to be
searched with all the elements in an array, if the match is found, then it returns the index of the
element else it returns -1. This algorithm can be implemented on the unsorted list.
Binary Search
A Binary algorithm is the simplest algorithm that searches the element very quickly. It is used to
search the element from the sorted list. The elements must be stored in sequential order or the
sorted manner to implement the binary algorithm. Binary search cannot be implemented if the
elements are stored in a random manner. It is used to find the middle element of the list.
Sorting Algorithms
Sorting algorithms are used to rearrange the elements in an array or a given data structure either
in an ascending or descending order. The comparison operator decides the new order of the
elements.
Array
Array is a container which can hold a fix number of items and these items should be of the same
type. Most of the data structures make use of arrays to implement their algorithms. Following
are the important terms to understand the concept of Array.
Element − Each item stored in an array is called an element.
Index − Each location of an element in an array has a numerical index, which is used to
identify the element.
Array Representation
Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
As per the above illustration, following are the important points to be considered.
Index starts with 0.
Array length is 10 which means it can store 10 elements.
Each element can be accessed via its index. For example, we can fetch an element at
index 6 as 9.
bool false
char 0
int 0
float 0.0
double 0.0f
void
Traverse Operation
Example
Output
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement, a
new element can be added at the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the end of
the array −
Example
n = n + 1;
while( j >= k) {
LA[j+1] = LA[j];
j = j - 1;
}
LA[k] = item;
Output
Deletion refers to removing an existing element from the array and re-organizing all elements of
an array.
Algorithm
Example
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
j = k;
while( j < n) {
LA[j-1] = LA[j];
j = j + 1;
}
n = n -1;
Output
Search Operation
You can perform a search for an array element based on its value or its index.
Algorithm
Example
void main() {
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
printf("The original array elements are :\n");
j = j + 1;
}
Output
Update Operation
Update operation refers to updating an existing element from the array at a given index.
Algorithm
Example
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;
LA[k-1] = item;
Output
We take an unsorted array for our example. Bubble sort takes Ο(n 2) time so we're keeping it
short and precise.
Bubble sort starts with very first two elements, comparing them to check which one is greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33
with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
We swap these values. We find that we have reached the end of the array. After one iteration,
the array should look like this −
To be precise, we are now showing how an array should look like after each iteration. After the
second iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is completely sorted.
Algorithm
Pseudocode
We observe in algorithm that Bubble Sort compares each pair of array element unless the whole
array is completely sorted in an ascending order. This may cause a few complexity issues like
what if the array needs no more swapping as all the elements are already ascending.
To ease-out the issue, we use one flag variable swapped which will help us see if any swap has
happened or not. If no swap has occurred, i.e. the array requires no more processing to be
sorted, it will come out of the loop.
Pseudocode of BubbleSort algorithm can be written as follows −
Algorithm bubbleSort( list : array of items )
{
loop = list.count;
end for
end for
return list
}
Implementation
One more issue we did not address in our original algorithm and its improvised pseudocode, is
that, after every iteration the highest values settles down at the end of the array. Hence, the next
iteration need not include already sorted elements.
int main()
{
int arr[100], i, n, step, temp;
// ask user for number of elements to be sorted
printf("Enter the number of elements to be sorted: ");
scanf("%d", &n);
// input elements if the array
for(i = 0; i < n; i++)
{
printf("Enter element no. %d: ", i+1);
scanf("%d", &arr[i]);
}
// call the function bubbleSort
bubbleSort(arr, n);
return 0;
}
Linear Search
What is Search?
Search is a process of finding a value in a list of values. In other words, searching is the process
of locating given value position in a list of values.
Algorithm:
//Let array a[n] stores n elements. Determine whether element ‘x’ is present or not.
linsrch(a[n], x)
{
index = 0;
flag = 0;
while (index < n) do
{
if (x == a[index])
{
flag = 1;
break;
} index ++;
}
if(flag == 1)
write(“Data found at %d position“, index);
else
write(“data not found”);
}
Example
Consider the following list of elements and the element to be searched...
Implementation of Linear Search Algorithm using C++ Programming Language
#include<iostream.h>
#include<conio.h>
void main()
{
int list[20],size, i, sElement;
cout<<”Enter size of the list: ";
cin>>size;
cout<<”Enter any”<<size<<” integer values: ";
for(i = 0; i < size; i++)
cin>>list[i];
cout<<"Enter the element to be Search: ";
cin>>sElement;
// Linear Search Logic
for(i = 0; i < size; i++)
{
if(sElement == list[i])
{
Cout<<"Element is found at”<<i<< “ index” ;
break;
}
}
if(i == size)
cout<<"Given element is not found in the list!!!";
getch();
}
Binary Search
Binary search algorithm falls under the category of interval search algorithms. This algorithm
is much more efficient compared to linear search algorithm. Binary search only works
on sorted data structures. This algorithm repeatedly target the center of the sorted data structure
& divide the search space into half till the match is found.
The time complexity of binary search algorithm is O(Log n).
Binary search is implemented using following steps...
BSEARCH(ARR, LB, UB, ITEM, LOC)
{
/*Here, ARR is a sorted list of elements, with LB and UB are lower and upper bounds for the
array. ITEM needs to be searched in the array and algorithm returns location LOC, index at
which ITEM is present else return -1.*/
Algorithm
//Let array a[n] of elements in increasing order, n ≥0, determine whether ‘x’ is present, and if so,
set j, such that x = a[j] else return 0.
binsrch(a[], n, x)
{
low = 1; high = n;
while (low < high) do
{
mid = (low + high)/2
if (x < a[mid])
high = mid – 1;
else if (x > a[mid])
low = mid + 1;
else return mid;
} return 0;
Example:
Let’s say here, ITEM = 62
ARR[1] 2 3 4 5 6 7 8 9
BEG = 1 and END =9 Hence MID = (1+9)/2 = 5
ARR[MID]=ARR[5] = 52
6 7 8 9
Step 2: Now BEG =6 and END =9 thus MID = INT([6+9]/2)= 6
NOW ARR[6] =ITEM. Thus LOC = MID
ARR[6]=62 = ITEM =62,
Thus LOC = 6
BEG=MID+1=6+1=7
MID=7+9/2=16/2=8
ARR[8]==71
BEG 7, END =MID-1=7
ARR[7]= ITEM
71=71
The complexity of Binary Search
Here are the complexities of the binary search given below.
Worst Case: O(nlogn)
Best Case: O(1)
Average Case: O(nlogn)
Example 2
Consider-
We are given the following sorted linear array.
Element 15 has to be searched in it using Binary Search Algorithm.
Binary Search Algorithm works in the following steps-
Step-01:
To begin with, we take beg=0 and end=6.
We compute location of the middle element as-
mid
= (beg + end) / 2
= (0 + 6) / 2
=3
Here, a[mid] = a[3] = 20 ≠ 15 and beg < end.
So, we start next iteration.
Step-02:
Since a[mid] = 20 > 15, so we take end = mid – 1 = 3 – 1 = 2 whereas beg remains unchanged.
We compute location of the middle element as-
mid
= (beg + end) / 2
= (0 + 2) / 2
=1
Here, a[mid] = a[1] = 10 ≠ 15 and beg < end.
So, we start next iteration.
Step-03:
Since a[mid] = 10 < 15, so we take beg = mid + 1 = 1 + 1 = 2 whereas end remains
unchanged.
We compute location of the middle element as-
mid
= (beg + end) / 2
= (2 + 2) / 2
=2
Here, a[mid] = a[2] = 15 which matches to the element being searched.
So, our search terminates in success and index 2 is returned.
Examples
Intel Fortran record structures, using only intrinsic types, easily convert to Fortran
95/90 derived types. The conversion can be as simple as replacing the
keyword STRUCTURE with TYPE and removing slash ( / ) marks. The following
shows an example conversion:
Record Structure
STRUCTURE /employee_name/
CHARACTER*25 last_name
CHARACTER*15 first_name
END STRUCTURE
STRUCTURE /employee_addr/
CHARACTER*20 street_name
INTEGER(2) street_number
INTEGER(2) apt_number
CHARACTER*20 city
CHARACTER*2 state
INTEGER(4) zip
END STRUCTURE
The record structures can be used as subordinate record variables within another
record, such as the employee_data record. The equivalent Fortran 90 derived
type would use the derived-type objects as components in a similar manner, as
shown below:
Record Structure
STRUCTURE /employee_data/
RECORD /employee_name/ name
RECORD /employee_addr/ addr
INTEGER(4) telephone
INTEGER(2) date_of_birth
INTEGER(2) date_of_hire
INTEGER(2) social_security(3)
LOGICAL(2) married
INTEGER(2) dependents
END STRUCTURE
C/C++ arrays allow you to define variables that combine several data items of the
same kind, but structure is another user defined data type which allows you to
combine data items of different kinds.
Structures are used to represent a record, suppose you want to keep track of your
books in a library. You might want to track the following attributes about each
book −
Title
Author
Subject
Book ID
Defining a Structure
To define a structure, you must use the struct statement. The struct statement
defines a new data type, with more than one member, for your program. The
format of the struct statement is this −
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
The structure 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 structure's definition, before the final semicolon, you can specify one or
more structure variables but it is optional. Here is the way you would declare the
Book structure −
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
When the above code is compiled and executed, it produces the following result −
Book 1 title : Learn C++ Programming
Book 1 author : Chand Miyan
Book 1 subject : C++ Programming
Book 1 id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Yakit Singha
Book 2 subject : Telecom
Book 2 id : 6495700
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
void printBook( struct Books book ) {
cout << "Book title : " << book.title <<endl;
cout << "Book author : " << book.author <<endl;
cout << "Book subject : " << book.subject <<endl;
cout << "Book id : " << book.book_id <<endl;
}
When the above code is compiled and executed, it produces the following result −
Book title : Learn C++ Programming
Book author : Chand Miyan
Book subject : C++ Programming
Book id : 6495407
Book title : Telecom Billing
Book author : Yakit Singha
Book subject : Telecom
Book id : 6495700
Pointers to Structures
You can define pointers to structures in very similar way as you define pointer to any
other variable as follows −
struct Books *struct_pointer;
Now, you can store the address of a structure variable in the above defined pointer
variable. To find the address of a structure variable, place the & operator before the
structure's name as follows −
struct_pointer = &Book1;
To access the members of a structure using a pointer to that structure, you must use
the -> operator as follows −
struct_pointer->title;
Let us re-write above example using structure pointer, hope this will be easy for you to
understand the concept −
#include <iostream.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// Book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// Book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
// This function accept pointer to structure as parameter.
void printBook( struct Books *book ) {
cout << "Book title : " << book->title <<endl;
cout << "Book author : " << book->author <<endl;
cout << "Book subject : " << book->subject <<endl;
cout << "Book id : " << book->book_id <<endl;
}
When the above code is compiled and executed, it produces the following result −
Book title : Learn C++ Programming
Book author : Chand Miyan
Book subject : C++ Programming
Book id : 6495407
Book title : Telecom Billing
Book author : Yakit Singha
Book subject : Telecom
Book id : 6495700
Field or member - component of the record; each field has its own type
FieldName - the identifier used to refer to the field. The identifier only need to be
unique within the struct. It will not be in conflict with names used elsewhere or in
other structs.
Example:
Student (idnumber, name, status, credits, gpa)
enum StatusType {FULLTIME, PARTTIME, NONDEGREE, GRAD}
struct StudentType {
int idNumber;
char name[30];
StatusType status;
float credits;
float gpa;
};
StudentType student1;
StudentType student2;
Dot operator
The dot (.) operator is used to select members from a record (struct).
struct StudentType {
....
}student1,student2; //not recommended
struct {
.....
} student1, student2; //no way to later make a new instance
This is an important feature. Compare to the fact that you cannot assign whole arrays
or strings by themselves. You must copy an array element by element, a string using
the strcpy( ) function.
student1 = student2; //OK
Fieldname Issues
The fieldnames are local to the struct.
The same member name can be used in other structs.
Every member name reference is always associated with the record or instance name.
Membership
Any structure can be a member of a struct
struct NameType {
char first[15];
char middleInit;
char last[15];
};
struct StudentType {
NameType name;
int idNum;
float credits;
float gpa;
};
StudentType student1,student2;
student1.name.last;
student2.name.first[0];
Type compatibility
Two struct instances are compatible only if their types are identical or are renamings
of the same type.