Professional Documents
Culture Documents
Nell Dale
Chapter 4-cont.
ADTs Stack and Queue
1
What is a Class Template?
A class template allows the compiler to
generate multiple versions of a class type
by using type parameters.
2
ACTUAL PARAMETER
StackType<int> numStack;
top 3
[MAX_ITEMS-1]
.
.
.
[3] 789
[2] -56
[1] 132
StackType<float> myStack;
top 3
[MAX_ITEMS-1]
.
.
.
[3] 3456.8
[2] -90.98
[1] 98.6
StackType<StrType> nameStack;
top 3
[MAX_ITEMS-1]
.
.
.
[3] Bradley
[2] Asad
[1] Rodrigo
private:
int top;
ItemType items[MAX_ITEMS];
}; 6
//--------------------------------------------------------
// CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS cont’d
//--------------------------------------------------------
// member function definitions for class StackType
.
.
.
{
top++;
items[top] = newItem; // STATIC ARRAY IMPLEMENTATION
} 7
Using class templates
8
Recall that . . .
char msg [ 8 ];
9
Addresses in Memory
When a variable is declared, enough memory to
hold a value of that type is allocated for it at an
unused memory location. This is the address of
the variable.
int x;
float number;
char ch;
x number ch
10
Obtaining Memory Addresses
The address of a non-array variable can be obtained
by using the address-of operator &.
int x;
float number;
char ch;
11
What is a pointer variable?
A pointer variable is a variable whose value is the
address of a location in memory.
12
Using a pointer variable
int x; 2000
x = 12; 12
x
3000
int* ptr;
2000
ptr = &x;
ptr
13
Unary operator * is the
dereference (indirection) operator
2000
int x;
12
x = 12;
x
3000
int* ptr;
2000
ptr = &x;
ptr
x = 12; 12 5
x
3000
int* ptr;
2000
ptr = &x;
ptr
15
Another Example
char ch; 4000
ch = ‘A’; A Z
ch
char* q;
5000 6000
q = &ch;
4000 4000
*q = ‘Z’; q p
char* p;
p = q; // the right side has value 4000
// now p and q both point to ch
16
C++ Data Types
Simple
Structured
Integral Floating
array struct union class
pointer reference 17
The NULL Pointer
STATIC DYNAMIC
ALLOCATION ALLOCATION
19
3 Kinds of Program Data
STATIC DATA: memory allocation exists
throughout execution of program.
static long SeedValue;
21
Dynamically Allocated Data
2000
char* ptr;
ptr
22
Dynamically Allocated Data
2000
char* ptr;
ptr
ptr
ptr
26
Some C++ pointer operations
Precedence
Higher -> Select member of class pointed to
+ - Add Subtract
Lower = Assignment
Dynamic Array Allocation
char *ptr; // ptr is a pointer variable that
// can hold the address of a char
6000
6000
ptr
28
Dynamic Array Allocation
char *ptr ;
ptr
30
What happens here?
31
Memory Leak
A memory leak occurs when dynamic memory (that
was created using operator new) has been left
without a pointer to it by the programmer, and so is
inaccessible.
8
int* ptr = new int; ptr
*ptr = 8;
-5
int* ptr2 = new int;
ptr2
*ptr2 = -5;
-5
ptr2 33
A Dangling Pointer
occurs when two pointers point to the same
object and delete is applied to one of them.
FOR EXAMPLE,
34
Leaving a Dangling Pointer
int* ptr = new int;
8
*ptr = 8;
ptr
int* ptr2 = new int;
*ptr2 = -5; -5
ptr = ptr2; ptr2
NULL
ptr2 35
DYNAMIC ARRAY
IMPLEMENTATION
class StackType
StackType
Private Data:
~StackType top 2
50 items [0]
Push maxStack 5
43 items [1]
items
Pop 80
items [2]
.
. items [3]
.
items [4]
36
//--------------------------------------------------------
// CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS
template<class ItemType>
class StackType {
public:
StackType( );
StackType( int max ); // PARAMETERIZED CONSTRUCTOR
~StackType( ) ; // DESTRUCTOR
. . .
private:
int top;
int maxStack;
ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION
}; 37
//--------------------------------------------------------
// CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS cont’d
//--------------------------------------------------------
template<class ItemType>
StackType<ItemType>::StackType( ) //DEFAULT CONSTRUCTOR
{
maxStack = 500;
top = -1;
items = new ItemType[500]; // dynamically allocates array
}
template<class ItemType>
StackType<ItemType>::StackType( int max ) // PARAMETERIZED
{
maxStack = max;
top = -1;
items = new ItemType[max]; // dynamically allocates array
}
38
//--------------------------------------------------------
// CLASS TEMPLATE DEFINITION AND MEMBER FUNCTIONS cont’d
//--------------------------------------------------------
template<class ItemType>
StackType<ItemType>::~StackType( )
{
delete [ ] items; // deallocates array
}
.
.
.
template<class ItemType>
bool StackType<ItemType>::IsFull( )
{
return (top == maxStack - 1)
}
39
What is a Queue?
Logical (or ADT) level: A queue is an
ordered group of homogeneous items
(elements), in which new elements are
added at one end (the rear), and
elements are removed from the other
end (the front).
A queue is a FIFO “first in, first out”
structure.
40
Queue ADT Operations
MakeEmpty -- Sets queue to an empty state.
IsEmpty -- Determines whether the queue is currently
empty.
Transformers
MakeEmpty change state
Enqueue
Dequeue
Observers
IsEmpty observe state
IsFull
42
DYNAMIC ARRAY
IMPLEMENTATION
class QueType
43
//--------------------------------------------------------
// CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE
#include "ItemType.h" // for ItemType
template<class ItemType>
class QueType {
public:
QueType( );
QueType( int max ); // PARAMETERIZED CONSTRUCTOR
~QueType( ) ; // DESTRUCTOR
. . .
private:
int front;
int rear;
int maxQue;
ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION 44
};
//--------------------------------------------------------
// CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE cont’d
//--------------------------------------------------------
template<class ItemType>
QueType<ItemType>::QueType( int max ) // PARAMETERIZED
{
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue]; // dynamically allocates
}
template<class ItemType>
bool QueType<ItemType>::IsEmpty( )
{
return ( rear == front )
}
45
//--------------------------------------------------------
// CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE cont’d
//--------------------------------------------------------
template<class ItemType>
QueType<ItemType>::~QueType( )
{
delete [ ] items; // deallocates array
}
.
.
.
template<class ItemType>
bool QueType<ItemType>::IsFull( )
{ // WRAP AROUND
return ( (rear + 1) % maxQue == front )
}
46