0% found this document useful (0 votes)
19 views

Week 1

Uploaded by

riyabhart02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Week 1

Uploaded by

riyabhart02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Week#01

Data Structures
CS2001
Email: [email protected]
ADT - Abstract Data Types
• Data types such as int, float, double, long, etc. are considered
to be in-built data types and we can perform basic operations
with them such as +,-,*,/, etc.
• We can create data structures along with their operations, and
such data structures that are not in-built are known as Abstract
Data Type (ADT).
• Abstract Data type (ADT) is a type (or class) for objects whose
behavior is defined by a set of values and a set of operations.
• The definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented.
ADT - Abstract Data Types
• The implementation decides which data structure should be
used to make execution most efficient in terms of time and
space. An item specified in terms of operations is called an
abstract data type
• An abstract data type is not a part of a program, because a
program written in a programming language requires the
definition of a data structure, not just the operations on the data
structure. However, an object-oriented language (OOL) such as
C++ has a direct link to abstract data types by implementing
them as a class
Some common ADTs
•Stack
•Operations: push, pop, top, isEmpty, size
•Principle: Last-In-First-Out (LIFO)
•Queue
•Operations: enqueue, dequeue, front, isEmpty, size
•Principle: First-In-First-Out (FIFO)
•Linked List
•Operations: insertAtBeginning, insertAtEnd, delete, search,
display
•Types: Singly Linked List, Doubly Linked List, Circular Linked List
Pointers
• Variables that content the location of another variable are called
pointers.
• Pointers are usually auxiliary variables that allow us to access the
values of other variables indirectly.
• A pointer is analogous to a road sign that leads us to a certain
location or to a slip of paper on which an address has been jotted
down.
• pointers—like all variables—also have two attributes: a content and a
location.
• This location can be stored in another variable, which then becomes
a pointer to a pointer
• int i=15, *p;
• p=&I;
• int *pp=&p;
Figure 1.1 Changes of values
after assignments are made
using pointer variables.
Note that (b) and (c) show the
same situation, and so do (d)
and (e), (g) and (h), (i) and (j),
(k) and (l), and (m) and (n).
Dynamically allocate and deallocate memory
• which is performed dynamically during the run of the program, unlike
for variables, whose locations are allocated at compilation time.
• To dynamically allocate and deallocate memory, two functions are
used. One function, new, takes from memory as much space as
needed to store an object whose type follows new.
• For example, with the instruction
• p = new int; // dynamically allocate memory
• If the space occupied by the integer accessible from p is no longer
needed, it can be returned to the pool of free memory locations
managed by the operating system by issuing the instruction
• delete p; // deallocate memory
Memory Leak
p = new int;
p = new int;

The code should be:

p = new int;
delete p;
p = new int;
Memory leaks can become a serious problem when a program uses
more and more memory without releasing it, eventually exhausting
memory and leading to abnormal termination.
Pointers and Arrays
In the previous slides, the pointer p refers to a block of memory
that holds one integer.
A more interesting situation is when a pointer refers to a data
structure that is created and modified dynamically.
This is a situation where we would need to overcome the
restrictions imposed by arrays.
Arrays in C++, and in most programming languages, have to be
declared in advance; therefore, their sizes have to be known
before the program starts.
The problem is solved with the use of pointers.
Pointers and Arrays
• memory address c+i*sizeof(long).
Arrays declared dynamically
p = new int[n];
. For example, the
sum of numbers in
the array p can be
found with the code
that uses array
notation
Arrays declared dynamically
delete [] p;
• Note the use of empty brackets in the instruction. The brackets
indicate that p points to an array. Also, delete should be used
with pointers that were assigned a value with new. For this
reason, the two following applications of delete are very likely to
lead to a program crash:
int a[10], *p = a;
delete [] p;
int n = 10, *q = &n;
delete q;
Pointers and Copy
Constructors
Figure 1.2 Illustrating the necessity of using a copy
constructor for objects with pointer members.
Pointers and Copy
Constructors
Figure 1.2 Illustrating the necessity of using a copy
constructor for objects with pointer members.
Pointers and Copy
Constructors
Figure 1.2 Illustrating the necessity of using a copy
constructor for objects with pointer members.

node1 = node2;
Pointers and Destructors
• When a local object of type Node is destroyed, the memory occupied by the object itself is automatically
released. This means the space allocated for its data members is freed.

• However, if one of the data members is a pointer that points to dynamically allocated memory (e.g., a
string allocated with malloc, new, or strdup), that memory is not automatically freed when the object is
destroyed.

• When Node object has a pointer name that points to a dynamically allocated string, destroying the Node
object only frees the memory used by the name pointer itself, not the memory allocated for the string it
points to.

• As a result, the memory occupied by the string remains allocated but becomes inaccessible (a memory
leak), because there’s no longer a pointer referencing that memory. This leads to a situation where
memory is wasted and cannot be reclaimed until the program ends.

• To avoid this problem, a destructor should be defined in the Node class. The destructor is a special
member function that is automatically called when an object is destroyed.
• The destructor's job is to clean up any resources that the object was using, such as dynamically allocated
memory.
Code example!
https://docs.google.com/document/d/1b24WWF-1UcGD5y4IK1cH
XCG9UOSyL8jlBi9n3ieOQlU/edit?usp=sharing
Pointers and
Reference
Variables
A reference variable must
be initialized in its
declaration as a
reference to a particular
variable, and this
reference cannot be
changed. This means
that a reference variable
cannot be null. A
reference variable r can
be considered a different
name for a variable n so
that if n changes then r
changes as well. This is
because a reference
variable is implemented
as a constant pointer to
the variable.
int n = 5, *p = &n, &r = n;
Reference variable
By design, references are meant
to be immutable in terms of
which variable they refer to,
providing a clear and consistent
way to alias variables in C++.
Constant Pointer
(int *const)
• A constant pointer is a pointer
whose address (the value it
holds) cannot be changed after
initialization.

• Must be initialized at the time of


declaration.

• You cannot change the address


that a constant pointer points to,
but you can modify the value at
that address if the value itself is
not constant.
Pointer to Constant
(const int *)

• A pointer to a constant integer


is a pointer that points to a
constant value, meaning you
cannot change the value at
the address it points to.

• You can change the address


stored in the pointer, but you
cannot modify the value at the
address.
Passing arguments by reference to function
calls
Reference variables are
used in passing arguments
by reference to function
calls. Passing by reference
is required if an actual
parameter should be
changed permanently
during execution of a
function. This can be
accomplished with pointers
Pointers and Reference
Variables
Reference type is also
used in indicating the
return type of functions.
For example, having
defined the function
Pointer as Return
type
Note that we can accomplish
the same with pointers, but
dereferencing has to be
used explicitly:
Caution!!
Reference variables and the
reference return type have
to be used with caution
because there is a
possibility of compromising
the information-hiding
principle when they are
used improperly. Consider
class C:
Pointers to
Functions
Just like pointers to
variables, you can
have pointers to
functions. A pointer to
a function stores the
address where the
function's code is
located in memory.
The Rule of Three in C++
If a class requires a custom implementation for any one of the following, it should implement all three.
Functions Involved:
• Destructor
• Copy Constructor
• Copy Assignment Operator

Why Apply the Rule of Three?


● Prevent Resource Leaks: Ensures proper cleanup of resources.
● Avoid Double Deletion: Prevents issues from multiple deletions of the same resource.
● Ensure Deep Copying: Properly copies dynamically allocated resources.

code example :
https://docs.google.com/document/d/1uoNqkw4LGdppPkPaAqxukSfseipahaZZTPewSYQ4gjk/edit?usp=
sharing
Static Array
• Static arrays are arrays whose size is fixed at compile time and
cannot be changed during runtime. The memory for static
arrays is allocated on the stack, and their lifetime is limited to
the scope in which they are defined.
Dynamic Array

• Dynamic arrays are


arrays whose size is
determined at runtime.
They are allocated on
the heap, allowing for
flexible size
adjustments during the
program's execution.
To use dynamic arrays
in C++, you often use
pointers and dynamic
memory allocation
functions.
Dynamic Array
A Dynamic array (vector in C++) automatically grows when we try
to make an insertion and there is no more space left for the new
item.
Approach:
• Doubles the array size (or to a specified new size) when the
array is full.
• Reduces the array size when elements are removed.
• Adds an element to the end of the array.
• Adds an element at a specific position.
• Removes the last element and optionally shrinks the array.
• Removes an element at a given index.
Dynamic Array
Dynamic Array
Dynamic Array
Dynamic Safe Array
Code:
https://docs.google.com/document/d/1MyP4PLHr9AnPuS7iTPGA
tE0FWw6vbxUeZQnwEHOdnmU/edit?usp=sharing
The Power of Persistence
This highlights
the main
takeaway from
Einstein's quote,
focusing on the
importance of
staying dedicated
to
problem-solving.

You might also like