Professional Documents
Culture Documents
-Structs are similar to classes because we can include both data and
functions in them. Although it isn’t common to include functions in them
Classes:
In C++:
This is called a dynamic object and the
memory allocated to the object will be in the
Heap:
Each object is allocated 8 bytes because each
object has 2 int variables a and b
The variable obj1 will be in the Stack and will
hold the memory address
The syntax used with structs will be the same as classes. Struct objects will
also be stored the same way. The only difference is:
1- There is no inheritance in structs
2- By default, the properties and functions in a struct are public whereas
in a class they are private
The basic use for structs is when we want to combine the data storing
capabilities of multiple primitive data types into a single entity
Dynamic Linked Lists:
A linked list basically consists of Nodes where each Node points to the
next one.
So each Node consists of data and a pointer to the next node
To do that we write:
The address of the node in the
Heap will be stored in the temp
pointer
This code appends Nodes to the end of the list by checking for a null value
in the next pointer.
Another way is by inserting the Nodes at the
beginning of the list. To do that we only have to use
2 lines:
When the list is empty, Head will be null. Temp is a
new Node and the next value will be null as well.
Since both of them are null, this line won’t have any
effect for the first Node addition