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

Data Structures Using C++ 2E: Pointers and Array-Based Lists

The chapter discusses pointers and array-based lists. It covers pointer variables, dynamic memory allocation using new and delete, pointer arithmetic, and dynamic arrays. Array-based lists are examined by using an array to store a list and variables to track the list length and array size. A generic arrayListType class is defined to implement lists as an abstract data type using templates for various element types. Member functions include isEmpty, isFull, insertAt, print, and isItemAtEqual.

Uploaded by

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

Data Structures Using C++ 2E: Pointers and Array-Based Lists

The chapter discusses pointers and array-based lists. It covers pointer variables, dynamic memory allocation using new and delete, pointer arithmetic, and dynamic arrays. Array-based lists are examined by using an array to store a list and variables to track the list length and array size. A generic arrayListType class is defined to implement lists as an abstract data type using templates for various element types. Member functions include isEmpty, isFull, insertAt, print, and isItemAtEqual.

Uploaded by

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

Data Structures Using C++ 2E

Chapter 3
Pointers and Array-Based Lists
Objectives

• Learn about the pointer data type and pointer


variables
• Explore how to declare and manipulate pointer
variables
• Learn about the address of operator and
dereferencing operator
• Discover dynamic variables
• Examine how to use the new and delete operators
to manipulate dynamic variables
• Learn about pointer arithmetic
Data Structures Using C++ 2E 2
Objectives (cont’d.)

• Discover dynamic arrays


• Explore how dynamic arrays are used to process
lists

Data Structures Using C++ 2E 3


The Pointer Data Type and Pointer
Variables
• Pointer data types
– Values are computer memory addresses
– No associated name
– Domain consists of addresses (memory locations)
• Pointer variable
– Contains an address (memory address)

Data Structures Using C++ 2E 4


The Pointer Data Type and Pointer
Variables (cont’d.)
• Declaring pointer variables
– Specify data type of value stored in the memory
location that pointer variable points to
– General syntax
dataType *identifier;
– Asterisk symbol (*)
• Between data type and variable name
• Can appear anywhere between the two
• Preference: attach * to variable name
– Examples: int *p; and char *ch;

Data Structures Using C++ 2E 5


The Pointer Data Type and Pointer
Variables (cont’d.)
• Address of operator (&)
– Unary operator
– Returns address of its operand
• Dereferencing operator (*)
– Unary operator
• Different from binary multiplication operator
– Also known as indirection operator
– Refers to object where the pointer points (operand of
the *)

Data Structures Using C++ 2E 6


The Pointer Data Type and Pointer
Variables (cont’d.)
• Pointers and classes
– Dot operator (.)
• Higher precedence than dereferencing operator (*)
– Member access operator arrow ( ->)
• Simplifies access of class or struct components via
a pointer
• Consists of two consecutive symbols: hyphen and
‘‘greater than’’ symbol
– Syntax
pointerVariableName -> classMemberName

Data Structures Using C++ 2E 7


The Pointer Data Type and Pointer
Variables (cont’d.)
• Initializing pointer variables
– No automatic variable initialization in C++
– Pointer variables must be initialized
• If not initialized, they do not point to anything
– Initialized using
• Constant value 0 (null pointer)
• Named constant NULL
– Number 0
• Only number directly assignable to a pointer variable

Data Structures Using C++ 2E 8


The Pointer Data Type and Pointer
Variables (cont’d.)
• Dynamic variables
– Variables created during program execution
• Real power of pointers
– Two operators
• new: creates dynamic variables
• delete: destroys dynamic variables
• Reserved words

Data Structures Using C++ 2E 9


The Pointer Data Type and Pointer
Variables (cont’d.)
• Operator new
– Allocates single variable
– Allocates array of variables
– Syntax
new dataType;
new dataType[intExp];
– Allocates memory (variable) of designated type
• Returns pointer to the memory (allocated memory
address)
• Allocated memory: uninitialized

Data Structures Using C++ 2E 10


The Pointer Data Type and Pointer
Variables (cont’d.)
• Operator delete
– Destroys dynamic variables
– Syntax
delete pointerVariable;
delete [ ] pointerVariable;
– Memory leak
• Memory space that cannot be reallocated
– Dangling pointers
• Pointer variables containing addresses of deallocated
memory spaces
• Avoid by setting deleted pointers to NULL after delete

Data Structures Using C++ 2E 11


The Pointer Data Type and Pointer
Variables (cont’d.)
• Operations on pointer variables
– Operations allowed
• Assignment, relational operations; some limited
arithmetic operations
• Can assign value of one pointer variable to another
pointer variable of the same type
• Can compare two pointer variables for equality
• Can add and subtract integer values from pointer
variable
– Danger
• Accidentally accessing other variables’ memory
locations and changing content without warning

Data Structures Using C++ 2E 12


The Pointer Data Type and Pointer
Variables (cont’d.)
• Dynamic arrays
– Static array limitation
• Fixed size
• Not possible for same array to process different data
sets of the same type
– Solution
• Declare array large enough to process a variety of data
sets
• Problem: potential memory waste
– Dynamic array solution
• Prompt for array size during program execution
Data Structures Using C++ 2E 13
The Pointer Data Type and Pointer
Variables (cont’d.)
• Dynamic arrays (cont’d.)
– Dynamic array
• An array created during program execution
– Dynamic array creation
• Use new operator
– Example
p=new int[10];

Data Structures Using C++ 2E 14


The Pointer Data Type and Pointer
Variables (cont’d.)
• Array name: a constant pointer
– Array name value: constant
– Increment, decrement operations cannot be applied

FIGURE 3-14 list and array list

FIGURE 3-15 Array list after the execution


of the statements list[0] = 25; and list[2] = 78;

Data Structures Using C++ 2E 15


The Pointer Data Type and Pointer
Variables (cont’d.)
• Functions and pointers
– Pointer variable passed as parameter to a function
• By value or by reference
– Declaring a pointer as a value parameter in a function
heading
• Same mechanism used to declare a variable
– Making a formal parameter be a reference parameter
• Use & when declaring the formal parameter in the
function heading

Data Structures Using C++ 2E 16


The Pointer Data Type and Pointer
Variables (cont’d.)
• Functions and pointers (cont’d.)
– Declaring formal parameter as reference parameter
• Must use &
• Between data type name and identifier name, include *
to make identifier a pointer
• Between data type name and identifier name, include &
to make the identifier a reference parameter
• To make a pointer a reference parameter in a function
heading, * appears before & between data type name
and identifier

Data Structures Using C++ 2E 17


The Pointer Data Type and Pointer
Variables (cont’d.)
• Functions and pointers (cont’d.)
– Example

Data Structures Using C++ 2E 18


The Pointer Data Type and Pointer
Variables (cont’d.)
• Dynamic two-dimensional arrays
– Creation

Data Structures Using C++ 2E 19


The Pointer Data Type and Pointer
Variables (cont’d.)
• Dynamic two-dimensional arrays (cont’d.)
– Declare board to be a pointer to a pointer
int **board;
– Declare board to be an array of 10 rows and 15
columns
• To access board components, use array subscripting
notation

Data Structures Using C++ 2E 20


Array-Based Lists

• List
– Collection of elements of same type
• Length of a list
– Number of elements in the list
• Many operations may be performed on a list
• Store a list in the computer’s memory
– Using an array

Data Structures Using C++ 2E 21


Array-Based Lists (cont’d.)
• Three variables needed to maintain and process a
list in an array
– The array holding the list elements
– A variable to store the length of the list
• Number of list elements currently in the array
– A variable to store array size
• Maximum number of elements that can be stored in the
array
• Desirable to develop generic code
– Used to implement any type of list in a program
– Make use of templates

Data Structures Using C++ 2E 22


Array-Based Lists (cont’d.)

• Define class implementing list as an abstract data


type (ADT)

FIGURE 3-29 UML class diagram


of the class arrayListType

Data Structures Using C++ 2E 23


Array-Based Lists (cont’d.)

• Definitions of functions isEmpty, isFull,


listSize and maxListSize

Data Structures Using C++ 2E 24


Array-Based Lists (cont’d.)

• Template print (outputs the elements of the list)


and template isItemAtEqual

Data Structures Using C++ 2E 25


Array-Based Lists (cont’d.)

• Template insertAt

Data Structures Using C++ 2E 26


Array-Based Lists (cont’d.)

• Template insertEnd and template removeAt

Data Structures Using C++ 2E 27


Array-Based Lists (cont’d.)
• Template replaceAt and template clearList

Data Structures Using C++ 2E 28


Array-Based Lists (cont’d.)
• Definition of the constructor and the destructor

Data Structures Using C++ 2E 29


Array-Based Lists (cont’d.)

• Copy constructor
– Called when object passed as a (value) parameter to
a function
– Called when object declared and initialized using the
value of another object of the same type
– Copies the data members of the actual object into the
corresponding data members of the formal parameter
and the object being created

Data Structures Using C++ 2E 30


Array-Based Lists (cont’d.)

• Copy constructor (cont’d.)


– Definition

Data Structures Using C++ 2E 31


Array-Based Lists (cont’d.)

• Overloading the assignment operator


– Definition of the function template

Data Structures Using C++ 2E 32


Array-Based Lists (cont’d.)
• Searching for an element
– Linear search example: determining if 27 is in the list
– Definition of the function template

FIGURE 3-32 List of seven elements

Data Structures Using C++ 2E 33


Array-Based Lists (cont’d.)

• Inserting an element

Data Structures Using C++ 2E 34


Array-Based Lists (cont’d.)

• Removing an element

Data Structures Using C++ 2E 35


Array-Based Lists (cont’d.)
TABLE 3-1 Time complexity of list operations

Data Structures Using C++ 2E 36


Summary
• Pointers contain memory addresses
– All pointers must be initialized
– Static and dynamic variables
– Several operators allowed
• Static and dynamic arrays
• Array-based lists
– Several operations allowed
– Use generic code

Data Structures Using C++ 2E 37

You might also like