0% found this document useful (0 votes)
13 views43 pages

CSC-2077 -- Week_1

Uploaded by

aleenashahzad03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views43 pages

CSC-2077 -- Week_1

Uploaded by

aleenashahzad03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Data Structures - CSC2077

Lecture No. 01

Dr. Salman Raza


Assistant Professor

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

Analyzing & solving a problem using object oriented approach

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

A data structure is a particular


way of storing and organizing
data in a computer so that it can
be used efficiently.

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

• The cost of a solution is the amount of resources that


the solution consumes

7
Organizing Data
 Anyorganization for a collection of records that can be
searched, processed in any order, or modified.

 Thechoice of data structure and algorithm can make


the difference between a program running in a few
seconds or many days.

8
Selecting a Data Structure
Select a data structure as follows:

1. Analyze the problem to determine the resource constraints a


solution must meet.

2. Determine the basic operations that must be supported.


Quantify the resource constraints for each operation.

3. Select the data structure that best meets these requirements.

9
Some Questions to Think about DS
• Are all data inserted into the data structure at the
beginning?

• Can data be deleted?

• Are all data processed in some well-defined order or is


random access allowed?

10
Data Structures
• Basic Data Structure
 Arrays
 Link List

• Derived Data Structures


 Stacks
 Queues
 Trees
 Graphs
 Heaps
 etc.
11
Data Structure Philosophy
 Each data structure has costs and benefits.
 Rarely is one data structure better than another in all
situations.

 A data structure requires:


 space for each data item it stores,
 time to perform each basic operation,
 programming effort.

12
Array
 An array is a structure of fixed-size, which can hold
items of the same data type.

 Itcan be an array of integers, an array of floating-point


numbers, an array of strings or even an array of arrays
(such as 2-dimensional arrays).

 Arrays are indexed, meaning that random access is


possible

13
Array

Figure 1: Visualization of basic Terminology of Arrays

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.

• If you want to insert an element to an array, first you


will have to create a new array with increased size
(current size + 1), copy the existing elements and
add the new element.

• The same goes for the deletion with a new array of


reduced 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.

• Used for different sorting algorithms such as


insertion sort, quick sort, bubble sort and merge sort.

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];

 Pointers can do array subscripting operations (We can access


array elements using pointers).
 Example:- int *ptr = List;
Pointers and Functions
• Pass-by-reference with pointer arguments
 Simulate pass-by-reference
 Use pointers and indirection operator
 Pass address of argument using & operator
 Arrays not passed with & because array name already
pointer
 * operator used as alias/nickname for variable inside of
function
Pointers and Arrays
• Accessing array elements with pointers
 Assume declarations:
int List[ 5 ];
int *bPtr;
bPtr = List;
Effect:-
- List is an address, no need for &
- The bPtr pointer will contain the address of the first element of
array List.
 Element List[ 2 ] can be accessed by *( bPtr + 2 )
Operations on Pointer Variables
• The operations that are allowed on pointer variables are the
assignment and relational operations and some limited arithmetic
operations.
• The value of one pointer variable can be assigned to another
pointer variable of the same type.
• Two pointer variables of the same type can be compared for
equality, and so on.
• Integer values can be added and subtracted from a pointer variable.
• The value of one pointer variable can be subtracted from another
pointer variable.
Operations on Pointer Variables
• int *p, *q;
• The statement:
•p = q;
• copiesthe value of q into p. After this statement executes, both p
and q point to the same memory location.
• Any changes made to *p automatically change the value of *q,
and vice versa.
Operations on Pointer Variables
•p == q
• evaluatesto true if p and q have the same value—that is, if
they point to the same memory location.
• Similarly, the expression:
•p != q
• evaluates to true if p and q point to different memory locations.
Operations on Pointer Variables
• The arithmetic operations that are allowed differ from the
arithmetic operations on numbers.
• First,
let us use the following statements to explain the
increment and decrement operations on pointer variables:
• int *p;
• double *q;
• char *chPtr;
Operations on Pointer Variables
• Recall that the size of the memory allocated for an int variable is 4 bytes, a
double variable is 8 bytes, and a char variable is 1 byte.
• The statement:
• p++; or p = p + 1;
• increments the value of p by 4 bytes because p is a pointer of type int.
Similarly, the statements:
• q++;
• chPtr++;
• increment the value of q by 8 bytes and the value of chPtr by 1 byte,
respectively.
Operations on Pointer Variables
• The increment operator increments the value of a pointer variable by the
size of the memory to which it is pointing.
• Similarly, the decrement operator decrements the value of a pointer
variable by the size of the memory to which it is pointing.
• Moreover, the statement:
• p = p + 2;
• increments the value of p by 8 bytes.
Operations on Pointer Variables
• Thus, when an integer is added to a pointer variable, the value of
the pointer variable is incremented by the integer times the size
of the memory that the pointer is pointing to.
• Similarly,when an integer is subtracted from a pointer variable,
the value of the pointer variable is decremented by the integer
times the size of the memory to which the pointer is pointing.
Void pointers
• Thevoid pointer, also known as the generic pointer, is a special
type of pointer that can be pointed at objects of any data type! A
void pointer is declared like a normal pointer, using the void
keyword as the pointer’s type:
Void pointers
• However, because the void pointer does not know what
type of object it is pointing to, it cannot be dereferenced
directly! Rather, the void pointer must first be explicitly
cast to another pointer type before it is dereferenced.

• Void pointers can be set to a null value:


Void pointers
• Although some compilers allow deleting a void pointer that points to
dynamically allocated memory, doing so should be avoided, as it can result
in undefined behaviour.
• It is not possible to do pointer arithmetic on a void pointer. This is because
pointer arithmetic requires the pointer to know what size object it is
pointing to, so it can increment or decrement the pointer appropriately.
• Note that there is no such thing as a void reference. This is because a void
reference would be of type void &, and would not know what type of value
it referenced.
Invalid pointers and null
pointers
• In principle, pointers are meant to point to valid addresses, such as the address
of a variable or the address of an element in an array.
• But pointers can point to any address, including addresses that do not refer to
any valid element.
• Typical examples of this are uninitialized pointers and pointers to non-existent
elements of an array:
• int *p;

• 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy