CSC-2077 -- Week_1
CSC-2077 -- Week_1
Lecture No. 01
1
Course Overview
Basic idea of a data structure & Goals of this
course
2
Introduction
to Computing
Basics of computer science, problem solving, algorithm, flow charts,
procedural programming
Computer
Programming
Data
Structures &
Alogrithms
Solving problems by selecting efficient data structures (w.r.t time &
memory)
Analysis of
Algorithms
Efficiency of algorithms by estimating required resources (time & memory)
3
What this course is about ?
• Studying, analyzing, designing and
implementing conceptual and concrete ways
to organize data for efficient storage and
efficient manipulation
4
Data Structure
5
Classification of Data Structure
6
Efficient Solution
• A solution is said to be efficient if it solves the problem
within its resource constraints
Space
Time
7
Organizing Data
Anyorganization for a collection of records that can be
searched, processed in any order, or modified.
8
Selecting a Data Structure
Select a data structure as follows:
9
Some Questions to Think about DS
• Are all data inserted into the data structure at the
beginning?
10
Data Structures
• Basic Data Structure
Arrays
Link List
12
Array
An array is a structure of fixed-size, which can hold
items of the same data type.
13
Array
14
Array operations
• Traverse:
Go through the elements and print them.
• Search:
Search for an element in the array. You can
search the element by its value or its index
• Update:
Update the value of an existing element at a
given index
15
Array operations
• Inserting elements to an array and deleting
elements from an array cannot be done straight
away as arrays are fixed in size.
16
Applications of arrays
• Used as the building blocks to build other data
structures such as array lists, heaps, hash tables,
vectors and matrices.
17
Pointers
Introduction
• C++’s data types are classified into three categories:
a) simple,
b) structured, and
c) pointers.
Introduction
• The values belonging to pointer data types are the memory
addresses of your computer.
• Asin many other languages, there is no name associated with the
pointer data type in C++.
• Because the domain—that is, the set of values of a pointer data
type—is the addresses (memory locations), a pointer variable is a
variable whose content is an address, that is, a memory location.
• Pointer
variable: A variable whose content is an address (that is, a
memory address).
Declaring Pointer Variables
1. As remarked previously, there is no name associated with pointer data types.
2. Moreover, pointer variables store memory addresses.
• So, the obvious question is: If no name is associated with a pointer data type, how do
you declare pointer variables?
• The value of a pointer variable is an address.
• That is, the value refers to another memory space.
• The data is typically stored in this memory space.
• Therefore, when you declare a pointer variable, you also specify the data type of the
value to be stored in the memory location pointed to by the pointer variable.
• In C++, you declare a pointer variable by using the asterisk symbol (*) between the
data type and the variable name. The general syntax to declare a pointer variable is
Declaring Pointer Variables
int *p;
char *ch;
In these statements, both p and ch are pointer variables. The content of p
(when properly assigned) points to a memory location of type int, and the
content of ch points to a memory location of type char.
Usually, p is called a pointer variable of type int, and ch is called a pointer
variable of type char.
Declaring Pointer Variables
• Before discussing how pointers work, let us make the following
observations.
int *p;
is equivalent to the statement:
int* p;
which is equivalent to the statement:
int * p;
• Thus, the character * can appear anywhere between the data type
name and the variable name.
Declaring Pointer Variables
• Now, consider the following statement:
int* p, q;
• In this statement, only p is the pointer variable, not q. Here, q is an int
variable. To avoid confusion, we prefer to attach the character * to the
variable name. So, the preceding statement is written as:
int *p, q;
• Of course, the statement:
int *p, *q;
• declares both p and q to be pointer variables of type int.
Declaring Pointer Variables
• Because the value of a pointer is a memory address, a pointer
can store the address of a memory space of the designated
type.
• Forexample, if p is a pointer of type int, p can store the
address of any memory space of type int.
• C++ provides two operators —to work with pointers.
The address of operator (&) and
The dereferencing operator (*)
Address of Operator (&)
• In C++, the ampersand, &, called the address of
operator, is a unary operator that returns the
address of its operand.
• For example, given the statements:
• int x;
• int *p;
• p = &x;
• assigns the address of x to p. That is, x and the
value of p refer to the same memory location.
Dereferencing Operator (*)
• C++ also uses * as a unary operator. *, commonly referred to as the dereferencing operator or
indirection operator, refers to the object to which its operand (that is, the pointer) points. For
example, given the statements:
int x = 25;
int *p;
p = &x; //store the address of x in p
• the statement:
cout << *p << endl;
• prints the value stored in the memory space pointed to by p, which is the value of x. Also, the
statement:
*p = 55;
• stores 55 in the memory location pointed to by p—that is, in x.
Initializing Pointer Variables
• Because C++ does not automatically initialize variables, pointer variables must be initialized
if you do not want them to point to anything.
• Pointer variables are initialized using the constant value 0, called the null pointer.
• Thus, the statement p = 0; stores the null pointer in p, that is, p points to nothing. Some
programmers use the named constant NULL to initialize pointer variables.
• The following two statements are equivalent:
• p = NULL;
• p = 0;
• The number 0 is the only number that can be directly assigned to a pointer variable.
• In modern C++, it is recommended to use nullptr instead of NULL or 0 to initialize pointer
variables with a null pointer.
Pointers and Arrays
• Arrays and pointers are closely related
Array name is like constant pointer
All array’s elements are placed in the consecutive locations.
(This is only valid in static memory allocation)
Example:- int List [10];
• Int array1[10];
• Int *q = array1+ 20;
Invalid pointers and null
pointers
• Neither p nor q point to addresses known to contain a value, but none of the
above statements causes an error.
• In C++, pointers are allowed to take any address value, no matter whether
there actually is something at that address or not.
• What can cause an error is to dereference such a pointer (i.e., actually
accessing the value they point to).
• Accessing such a pointer causes undefined behavior, ranging from an error
during runtime to accessing some random value.
• But, sometimes, a pointer really needs to explicitly point to nowhere, and
not just an invalid address.
• For such cases, there exists a special value that any pointer type can take:
the null pointer value.
• This value can be expressed in C++ in two ways: either with an integer
value of zero, or with the nullptr keyword