You are on page 1of 7

Structs and Classes:

-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:

Suppose we make the following class:

When creating an object in Java/C# we use the


following code:

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 following code will only work in C++

This is because in Java/C# all objects are dynamic and we need to


initialize it before accessing its properties

In C++ however we can create normal


objects in the Stack
Structs:

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

This can be represented using a struct:


The pointer will point to another Node of the
same type

This is how the Node will be stored in memory:


(This Node is static and will be created in the
Stack)

The Head pointer is a Node pointer variable


It is also created in the Stack and initially it will
Have a garbage value. To remove that garbage value
We have to explicitly change it to null
A linked list is dynamic which means it can grow and shrink according to
our requirements. In order to make it dynamic we need to make the Nodes
dynamic and store them in the Heap

To do that we write:
The address of the node in the
Heap will be stored in the temp
pointer

Since it is dynamic, its variables will have a default


value of 0:

Now we need the Head pointer to point to the first


Node which is temp:
This is a special case for the first Node
H

This is what the list looks like now: Temp

To append new Nodes to the list we must first


iterate through the list and find where it ends. To
do this we make a copy of the Head pointer.
Each Node has a pointer variable next that
points to the next Node in the list. We have to
use a while loop to iterate through the list until
we find a Node whose next pointer is null
(tH -> next checks the next variable of the current Node)

When we find a Node with a null next pointer,


we make a new Node and assign its address to
the next pointer
(Note: temp in this pic is a new Node and not the same as the first Node.
Although it has the same name, it will be given a different memory space in
the Heap)

This is the complete code to append


a new Node into the List:

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

You might also like