Week 1
Week 1
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;
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.
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