Data Structures Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Structures

1. Structures in C++ are user-defined data types that allow you to


group together related data items of different types under a single
name.
2. They provide a way to create complex data structures and
organize data in a meaningful way.
3. Structures are used to represent real-world entities, such as a
student, employee, or a book, where each entity has multiple
attributes or properties.

Definition and Purpose of Structures:

4. The structure definition begins with the keyword struct, followed


by the name of the structure and a pair of braces {} enclosing the
members of the structure.
5. Each member has a data type and a name, similar to declaring
variables.
6. For example:
struct Student {
int rollNumber;
string name;
int age;
};

7. In the above example, we define a structure named "Student"


with three members: rollNumber, name, and age, each having a
different data type.
8. The purpose of structures is to group related data items together
so that they can be treated as a single entity.
9. It allows us to define a template or blueprint for creating multiple
objects of the same structure type, each with its own set of
values.

Declaring and Accessing Structure

10. Once the structure is defined, you can declare variables of that
structure type. For example:
Student s1; // Declaration of a structure variable
11. To access the members of a structure variable, we use the dot
operator (.) followed by the member name.
12. For example:
s1.rollNumber = 1001; // Accessing and
assigning value to the rollNumber member
s1.name = "John Doe"; // Accessing and
assigning value to the name member
s1.age = 20; // Accessing and assigning value
to the age member
13. Here, we access the members of the s1 variable and assign values
to them using the dot operator.

Example:
#include <iostream>
using namespace std;

struct Student {
int rollNumber;
string name;
int age;
};

int main() {
Student s1;
s1.rollNumber = 1001;
s1.name = "John Doe";
s1.age = 20;

cout << "Roll Number: " << s1.rollNumber <<


endl;
cout << "Name: " << s1.name << endl;
cout << "Age: " << s1.age << endl;

return 0;
}

In this example, we define a structure named "Student" with three


members. We declare a structure variable s1 and assign values to its
members. Finally, we print the values of the structure members.
Example:
#include <iostream>
using namespace std;

struct Student {
int rollNumber;
string name;
int age;
};

int main() {
Student s1;
s1.rollNumber = 1001;
s1.name = "John Doe";
s1.age = 20;

cout << "Roll Number: " << s1.rollNumber <<


endl;
cout << "Name: " << s1.name << endl;
cout << "Age: " << s1.age << endl;

return 0;
}
Array of Structures

14. We can create an array of structures to store multiple instances of


the same structure type. This allows us to work with a collection
of related data items.
15. Each element of the array is a structure, and we can access and
modify its members using indexing.
16. To create an array of structures, we follow the same syntax as
creating an array of any other data type.
17. We specify the structure name followed by the array name and
the number of elements in square brackets [].

Example:
struct Student {
int rollNumber;
string name;
int age;
};

int main() {
Student students[5]; // Creating an array of
5 Student structures

// Accessing and modifying elements in the


array
students[0].rollNumber = 1001;
students[0].name = "John Doe";
students[0].age = 20;

students[1].rollNumber = 1002;
students[1].name = "Jane Smith";
students[1].age = 19;

// ...

return 0;
}
In the above example, we create an array of 5 Student structures named
students. Each element of the array can hold information about a
student, including the rollNumber, name, and age.

Performing Operations on an Array of Structures:

Once we have an array of structures, we can perform various operations


on it, such as searching, sorting, or displaying the data. Here's an
example of displaying the details of all students in the array:

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


cout << "Student " << i + 1 << endl;
cout << "Roll Number: " <<
students[i].rollNumber << endl;
cout << "Name: " << students[i].name << endl;
cout << "Age: " << students[i].age << endl;
cout << endl;
}

Example:

#include <iostream>
using namespace std;

struct Student {
int rollNumber;
string name;
int age;
};

int main() {
int MAX_STUDENTS;
cin>>MAX_STUDENTS;
Student students[MAX_STUDENTS];

// Input student details


for (int i = 0; i < MAX_STUDENTS; i++) {
cout << "Enter details for Student " << i
+ 1 << endl;
cout << "Roll Number: ";
cin >> students[i].rollNumber;
cout << "Name: ";
cin >> students[i].name;
cout << "Age: ";
cin >> students[i].age;
}

// Output student details


cout << "Student Details:" << endl;
for (int i = 0; i < MAX_STUDENTS; i++) {
cout << "Student " << i + 1 << endl;
cout << "Roll Number: " <<
students[i].rollNumber << endl;
cout << "Name: " << students[i].name <<
endl;
cout << "Age: " << students[i].age <<
endl;
cout << endl;
}

return 0;
}

In this example, we have a structure Student with three members:


rollNumber, name, and age. We create an array of structures named
students to store multiple student details.

Using a loop, we input the details of MAX_STUDENTS number of


students. Each student's roll number, name, and age are assigned to the
corresponding members of the structure.

After that, another loop is used to output the student details from the
array of structures.

This example demonstrates the concept of an array of structures,


allowing us to store and access multiple instances of the Student
structure in a convenient manner.
Structure and Functions
Passing structures to functions
Returning structures from functions
Modifying structure members in functions

Example:

#include <iostream>
using namespace std;

struct Point {
int x;
int y;
};

// Function to display the coordinates of a Point


void displayPoint(Point p) {
cout << "Coordinates: (" << p.x << ", " <<
p.y << ")" << endl;
}

// Function to create and return a Point with


modified coordinates
Point modifyPoint(Point p, int newX, int newY) {
p.x = newX;
p.y = newY;
return p;
}
int main() {
Point p1 = {3, 5};
displayPoint(p1); // Output: Coordinates:
(3, 5)

Point p2 = modifyPoint(p1, 7, 9);


displayPoint(p2); // Output: Coordinates:
(7, 9)

return 0;
}

In this example, we have a structure Point representing a point on a 2D


plane with x and y coordinates. We define two functions that work with
the Point structure.

The displayPoint function takes a Point parameter p and displays its


coordinates.

The modifyPoint function takes a Point parameter p, along with new x


and y values. It modifies the x and y coordinates of p and returns the
modified Point.

In the main function, we create a Point p1 with initial coordinates (3, 5).
We then call the displayPoint function to display its coordinates.

Next, we call the modifyPoint function, passing p1 along with new


coordinates (7, 9). The function modifies the x and y coordinates of p1
and returns the modified Point, which is stored in p2. Finally, we call the
displayPoint function to display the coordinates of p2.

This example demonstrates how structures can be passed to functions,


modified within functions, and returned from functions, allowing for
convenient manipulation of structure members using functions.
Structure and Pointers
Structures can be accessed and manipulated using pointers. Pointers to
structures allow us to dynamically allocate memory for structures and
access their members using pointer notation.

Pointer to Structures
18. A pointer to a structure holds the memory address of the
structure variable.
19. The pointer is declared using the structure name followed by an
asterisk (*) and the pointer variable name.
20. To access the members of a structure through a pointer, the
arrow operator (->) is used.

Example:
struct Person {
string name;
int age;
};

int main() {
Person p1 = {"John Doe", 25};
Person* ptr = &p1;

cout << "Name: " << ptr->name << endl;


cout << "Age: " << ptr->age << endl;

return 0;
}

21. In this example, we have a structure Person with members name


and age.
22. We declare a structure variable p1 and initialize its values.
23. Then, we declare a pointer ptr to Person and assign the memory
address of p1 to it.
24. Using the arrow operator ->, we access the members of p1
through the pointer ptr and display their values.

Difference Between -> arrow and . dot operator

#include <iostream>
using namespace std;
struct Person {
string name;
int age;
};
int main() {
Person p1 = {"John Doe", 25};
Person* ptr = &p1;
cout << "Name: " << ptr->name << endl;
cout << "Age: " << ptr->age << endl;
cout << "Name: " << (*ptr).name << endl;
cout << "Age: " << (*ptr).age << endl;
return 0;
}

25. -> Arrow operator


a. The -> operator is used when accessing members of a
structure or class through a pointer to that structure or
class.
b. It combines the process of dereferencing the pointer and
accessing the member using a single operator.
c. It is commonly used with pointers to structures or classes.
26. . dot operator
a. The . operator is used when accessing members of a
structure or class using the actual structure or class variable
itself.
b. It is used with the object or variable of the structure or class
directly, without involving pointers.
c. It is commonly used with actual structure or class variables.

Dynamic Allocation of Structures using Pointers

27. Structures can also be dynamically allocated using pointers,


allowing us to create structures at runtime.
28. The new operator is used to allocate memory for the structure,
and the address is assigned to the pointer variable.
29. To access the members of a dynamically allocated structure, the
arrow operator (->) is used.

Example:
struct Point {
int x;
int y;
};

int main() {
Point* ptr = new Point; // Dynamic
allocation of Point structure

ptr->x = 3; // Accessing members using


pointer
ptr->y = 5;

cout << "Coordinates: (" << ptr->x << ", " <<
ptr->y << ")" << endl;

delete ptr; // Deallocating the dynamically


allocated memory

return 0;
}
Pointers in structures have several uses

30. Dynamic Memory Allocation:


a. Pointers allow us to dynamically allocate memory for
structures at runtime using functions like new or malloc.
This enables us to create structures of variable size and
manage memory efficiently.

31. Efficient Data Passing:


a. Instead of passing a copy of a structure to a function, we
can pass a pointer to the structure. This saves memory and
improves performance, especially when dealing with large
structures.

32. Linked Data Structures:


a. Pointers are often used to create linked data structures
such as linked lists, trees, and graphs. Each node in these
data structures contains a structure and a pointer to the
next or previous node, forming a chain or hierarchy.

Enumeration
Enumeration is a user-defined data type that consists of a set of named
values called enumerators. It allows programmers to define a list of
named constants, making the code more readable and maintainable.

The syntax for defining an enumeration is as follows:

enum EnumName {
Enumerator1,
Enumerator2,
// ...
};

Here's a breakdown of the components:


33. enum: Keyword used to define an enumeration.
34. EnumName: The name given to the enumeration type.
35. Enumerator1, Enumerator2, etc.: The named values or constants
within the enumeration.

Each enumerator is assigned an integer value starting from 0 by default,


and subsequent enumerators are assigned incrementing values.
However, you can explicitly assign specific values to the enumerators.

Example:

#include <iostream>
using namespace std;

enum Day {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};

int main() {
Day today = Wednesday;
cout << "Today is ";

switch (today) {
case Monday:
cout << "Monday";
break;
case Tuesday:
cout << "Tuesday";
break;
case Wednesday:
cout << "Wednesday";
break;
case Thursday:
cout << "Thursday";
break;
case Friday:
cout << "Friday";
break;
case Saturday:
cout << "Saturday";
break;
case Sunday:
cout << "Sunday";
break;
}

cout << endl;

return 0;
}

In the above example, an enumeration `Day` is defined with the names


of the days of the week. The variable `today` is assigned the value
`Wednesday`. Using a `switch` statement, we can easily match the
value of `today` with the corresponding enumerator and print the day of
the week.

Example:
#include <iostream>
using namespace std;

enum Color {
Red = 1,
Green = 2,
Blue = 4
};

int main() {
Color c1 = Red;
Color c2 = Green;

cout << "c1 value: " << c1 << endl;


cout << "c2 value: " << c2 << endl;

return 0;
}

The assigned values can be used in arithmetic operations and


comparisons.

Enumerations are useful when we have a set of related constants that


need to be represented in a meaningful way, making the code more
readable and self-explanatory.

Class Codes

3 ways to initialize structures

#include <iostream>
using namespace std;

struct Student{
int rollNumber;
string Name;
int age;
};

int main() {
Student std1 = {1002332, "Aveek", 27};

Student std2;
std2.rollNumber= 12321031;
std2.Name = "Akshay";
std2.age = 21;

Student std3;
cin>>std3.rollNumber>>std2.Name>>std2.age;

return 0;
}

Example
#include <iostream>
using namespace std;

struct Student{
int rollNumber;
string Name;
int age;
};

int main() {
Student std1 = {1002332, "Aveek", 27};

Student std2;
std2.rollNumber= 12321031;
std2.Name = "Akshay";
std2.age = 21;
Student std3;
cin>>std3.rollNumber>>std3.Name>>std3.age;

cout<<"Information about Student 1\n";


cout<<"Roll Number = "<<std1.rollNumber<<"\n";
cout<<"Name = "<<std1.Name<<"\n";
cout<<"Age = "<<std1.age<<"\n";
cout<<"----------------------\n";
cout<<"Information about Student 2\n";
cout<<"Roll Number = "<<std2.rollNumber<<"\n";
cout<<"Name = "<<std2.Name<<"\n";
cout<<"Age = "<<std2.age<<"\n";
cout<<"----------------------\n";
cout<<"Information about Student 3\n";
cout<<"Roll Number = "<<std3.rollNumber<<"\n";
cout<<"Name = "<<std3.Name<<"\n";
cout<<"Age = "<<std3.age<<"\n";
return 0;
}

Example:
#include <iostream>
using namespace std;
struct Student{
int rollNumber;
string Name;
int age;
};

int main() {
Student arr[5];
// arr[0].rollNumber = 11231;
// arr[0].Name = "Avi";
// arr[0].age = 21;
//
// arr[1].rollNumber = 23231;
// arr[1].Name = "Savin";
// arr[1].age = 23;
for(int i=0;i<5;i++)
{
cin>>arr[i].rollNumber;
cin>>arr[i].Name;
cin>>arr[i].age;
}

for(int i=0;i<5;i++)
{
cout<<"Information about Student "<<(i+1)<<"\n";
cout<<"Roll Number = "<<arr[i].rollNumber<<"\n";
cout<<"Name = "<<arr[i].Name<<"\n";
cout<<"Age = "<<arr[i].age<<"\n";
cout<<"-------------------\n";
}
return 0;
}

Example:
#include <iostream>
using namespace std;

struct Point{
int x;
int y;
};

void displayPoint(Point p)
{
cout<<"Coordinates = "<<p.x<<","<<p.y<<"\n";
}

Point modifyPoint(Point p, int a, int b)


{
p.x = a;
p.y = b;
return p;
}

int main() {
Point p1 = {3,5};
displayPoint(p1);

Point p2 = modifyPoint(p1,6,9);
displayPoint(p1);
displayPoint(p2);
return 0;
}

Example:
#include <iostream>
using namespace std;

struct Point{
int x;
int y;
};
void displayPoint(Point *p)
{
cout<<"Coordinates = "<<p->x<<","<<p->y<<"\n";
}

Point* modifyPoint(Point *p, int a, int b)


{
p->x = a;
p->y = b;
return p;
}

int main() {
Point p1 = {3,5};
Point *ptr = &p1;
displayPoint(&p1);
Point* p2 = modifyPoint(ptr, 10,20);
cout<<"After modification\n";
displayPoint(&p1);
displayPoint(p2);

return 0;
}

Example:
#include <iostream>
using namespace std;

struct Point{
int x;
int y;
};

void displayPoint(Point *p)


{
cout<<"Coordinates = "<<p->x<<","<<p->y<<"\n";
}

Point* modifyPoint(Point *p, int a, int b)


{
p->x = a;
p->y = b;
return p;
}

int main() {
Point* p1 = new Point;
p1->x = 4;
p1->y = 8;
displayPoint(p1);
// Point *ptr = &p1;
// displayPoint(&p1);
// Point* p2 = modifyPoint(ptr, 10,20);
// cout<<"After modification\n";
// displayPoint(&p1);
// displayPoint(p2);

return 0;
}

You might also like