Title: CSI 1340 Introduction to Computer Science II
1CSI 1340Introduction to Computer Science II
- Chapter 4
- ADTs Stack and Queue
2Stacks of Plates in Cafeteria
New plates are pushed on top
The next plate is popped from the top
3What is a Stack?
- A stack is an ordered group of homogeneous items
(elements), in which the removal and addition of
stack items can take place only at the top of the
stack. - A stack is a LIFO last in, first out structure.
- Entries are taken out of the stack in the reverse
order of their insertion.
4Are Stacks Useful?
- Web Surfing
- Lost in hyperlink space
- CNN article on wild goats
- Yahoo search for wild goat cheese recipe
- Ebay bid on goat hair purse
- How did I get here?
5Stack ADT Operations
- MakeEmpty -- Sets stack to an empty state.
- IsEmpty -- Determines whether the stack is
currently empty. - IsFull -- Determines whether the stack is
currently full. - Push (ItemType newItem) -- Adds newItem to the
top of the stack. - Pop (ItemType item) -- Removes the item at the
top of the stack and returns it in item.
6Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top
MAX_ITEMS-1 .
.
. 2
1 items 0
7Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top -1
MAX_ITEMS-1 .
.
. 2
1 items 0
8Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top 0
MAX_ITEMS-1 .
.
. 2
1 items 0
V
9Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top 1
MAX_ITEMS-1 .
.
. 2
1 C items
0 V
10Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top 2
MAX_ITEMS-1 .
.
. 2 S
1 C
items 0 V
11Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top 2
MAX_ITEMS-1 .
.
. 2 S
1 C
items 0 V
12Tracing Client Code
S
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top 1
MAX_ITEMS-1 .
.
. 2 S
1 C
items 0 V
13Tracing Client Code
S
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top 2
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
14Tracing Client Code
S
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top 2
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
15Tracing Client Code
K
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top 1
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
16Tracing Client Code
K
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top 1
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
17Tracing Client Code
C
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top 0
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
18Tracing Client Code
C
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top 0
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
19Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top -1
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
20End of Trace
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( ))
charStack.Pop(letter)
Private data top -1
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
21Just How Generic Are We?
- typedef string GenType
- class StackType
-
- public
- StackType()
- bool push(const GenType url)
- bool pop(GenType url)
- private
- enum MAXSTACK 100
- int top
- GenType itemMAXSTACK
typedef int GenType class StackType public St
ackType() bool push(const GenType url) bool
pop(GenType url) private enum MAXSTACK
100 int top GenType itemMAXSTACK
22What is a Class Template?
- A class template allows the compiler to generate
multiple versions of a class type by using type
parameters. - The formal parameter appears in the class
template definition, and the actual parameter
appears in the client code. Both are enclosed in
pointed brackets, lt gt.
23StackTypeltintgt numStack
ACTUAL PARAMETER
24StackTypeltfloatgt myStack
ACTUAL PARAMETER
25Using class templates
- The actual parameter to the template is a data
type. Any type can be used, either built-in or
user-defined. - When using class templates, both the
specification and implementation should be
located in the same file (the header file),
instead of in separate .h and .cpp files.
26Recall that
- Memory is like a shelf of slots
- Each slot has a number and some content value
27Addresses 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 // 2 bytes
- float number // 4 bytes
- char ch // 1 byte
- 2000 2002
2006
28Obtaining Memory Addresses
- We can get the value of a slot. How about the
slot itself? - The address of a non-array variable can be
obtained by using the address-of operator . - int x
- float number
- char ch
- cout ltlt Address of x is ltlt x ltlt endl
- cout ltlt Address of number is ltlt number ltlt
endl - cout ltlt Address of ch is ltlt ch ltlt endl
-
29Results
- int x
- float number
- char ch
- cout ltlt Address of x is ltlt x ltlt endl
- cout ltlt Address of number is ltlt number ltlt
endl - cout ltlt Address of ch is ltlt ch ltlt endl
- Output
- Address of x is 0012FF7C
- Address of number is 0012FF78
- Address of ch is 0012FF74
30What is a pointer variable?
- A pointer variable is a variable whose value is
the address of a location in memory. - To declare a pointer variable, you must specify
the type of value that the pointer will point to.
For example, - int ptr // ptr will hold the address of an int
- char q // q will hold the address of a char
31Using a pointer variable
2000
12 x 3000 2000 ptr
- int x
- x 12
- int ptr
- ptr x
- NOTE Because ptr holds the address of x,
- we say that ptr points to x
-
32 - dereference operator
2000
12 x 3000 2000 ptr
- int x 12
- int ptr x
- coutltltAddr of x ltlt ptr ltlt endl
- coutltltValue of xltlt ptr ltlt endl
- NOTE The value pointed to by ptr is denoted by
ptr
33Results
- int x 12
- int ptr x
- coutltltAddr of x ltlt ptr ltlt endl
- coutltltValue of xltlt ptr ltlt endl
- Output
- Addr of x 0012FF7C
- Value of x 12
34Using the dereference operator
2000 12
5 x 3000 2000 ptr
- int x
- x 12
- int ptr
- ptr x
- ptr 5 // changes the value // at
address ptr to 5 -
35Another Example
- char ch
- ch A
- char q
- q ch
- q Z
- char p
- p q // the right side has value 4000
- // now p and q both point to ch
4000 A
Z ch 5000
6000 4000 4000 q
p
36The NULL Pointer
- There is a special pointer value called the null
pointer denoted by NULL to represent a pointer
variable that is not pointing to anything - NULL is not a valid memory address
- It is an error to dereference (e.g., x) a
pointer whose value is NULL. Such an error may
cause your program to crash, or behave
erratically. It is the programmers job to
check for this. - while (ptr ! NULL)
- . . . // ok to use ptr here
-
37Allocation of memory
STATIC ALLOCATION Static
allocation is the allocation of memory space at
compile time.
DYNAMIC ALLOCATION Dynamic
allocation is the allocation of memory space at
run time by using operator new.
38Using operator new
- If memory is available, new allocates the
requested object or array and returns a pointer
to (address of ) the memory allocated. - Otherwise, the null pointer is returned.
- The dynamically allocated object exists until the
delete operator destroys it.
39New Operator
- Creates a new dynamic variable of the specified
type and returns a pointer to this new dynamic
variable. - Example
- double dptr
- dptr new double
-
- dptr 3.14259
dptr
dptr
3.14259
dptr
40Dynamically Allocated Data
- char ptr
- ptr new char
- ptr B
- cout ltlt ptr
-
2000 ptr
41Dynamically Allocated Data
- char ptr
- ptr new char
- ptr B
- cout ltlt ptr
- NOTE Dynamic data has no variable name
-
2000 ptr
42Dynamically Allocated Data
- char ptr
- ptr new char
- ptr B
- cout ltlt ptr
- NOTE Dynamic data has no variable name
-
43Dynamically Allocated Data
- char ptr
- ptr new char
- ptr B
- cout ltlt ptr
- delete ptr
-
2000 ptr NOTE Delete deallocates
the memory pointed to by ptr.
?
44Using operator delete
- The object or array currently pointed to by the
pointer is deallocated, and the pointer is
considered unassigned. The memory is returned to
the free store.
45Dynamic Array Allocation
char ptr // ptr is a pointer variable
that // can hold the
address of a char ptr new char 5
// dynamically, during run time, allocates
// memory for 5 characters and places into
// the contents of ptr their beginning
address
6000
6000
ptr
46Dynamic Array Allocation
char ptr ptr new char 5
strcpy( ptr, Bye ) ptr 1 u // a
pointer can be subscripted cout ltlt ptr 2
6000
u
6000
B y e \0
ptr
47Declaring a Dynamic Array
- Example
- int arrayptr // (1) Declare a pointer
- arrayptr new int10 // (2) Create an array
pointed - // to by the pointer
- arrayptr0 65 // (3) Assign a value to the
first - // memory location in the array
- NOTE With dynamic arrays, omit the when
assigning values or otherwise accessing the array
elements. After allocation, access is identical
to statically allocated arrays.
48Dynamic Array Deallocation
char ptr ptr new char 5
strcpy( ptr, Bye ) ptr 1
u delete ptr // deallocates array
pointed to by ptr // ptr
itself is not deallocated, but
// the value of ptr is considered
unassigned
?
ptr
49Declaring a Dynamic Record
- Example
- struct RecType
-
- int iNum
- float fNum
-
- RecType rptr
50What is a Queue?
- 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. - Like a line at the grocery store.
51Queue ADT Operations
- MakeEmpty -- Sets queue to an empty state.
- IsEmpty -- Determines whether the queue is
currently empty. - IsFull -- Determines whether the queue is
currently full. - Enqueue (ItemType newItem) -- Adds newItem to
the rear of the queue. - Dequeue (ItemType item) -- Removes the item at
the front of the queue and returns it in item.
52Accessing a Dynamic Record
- Example
- rptr new RecType // Creates a record pointed
to by - // the pointer
- (rptr). iNum 65 // Assigns a value to the
first - // field in the record
- rptr -gt iNum 65 // Alternative syntax
53Dereferencing Operator w/ Records
- Asterisk and the pointer name are enclosed in
parentheses. - Example
- cout ltlt (rptr).iNum
54Arrow Operator w/Records
- -gt (a hyphen followed by a greater-than symbol)
- For records and classes, it is equivalent to the
use of the dereferencing operator and
parentheses. - Example
- cout ltlt rptr -gt iNum
55Pointers as Value Parameters
- Example
- void myFunc (int intptr) // Prototype for
myFunc -
56What happens here?
- int ptr new int
- ptr 3
- ptr new int // changes value of ptr
- ptr 4
-
3 ptr
3 ptr 4
57Memory Leak
- A memory leak occurs when dynamic memory (that
was created using operator new) is left without a
pointer to it by the program - int ptr new int
- ptr 8
- int ptr2 new int
- ptr2 -5
-
8 ptr
-5 ptr2
58Causing a Memory Leak
8 ptr
-5 ptr2
- int ptr new int
- ptr 8
- int ptr2 new int
- ptr2 -5
- ptr ptr2 // here the 8 becomes
inaccessible -
8 ptr
-5 ptr2
59A Dangling Pointer
- Occurs when two pointers point to the same
object and delete is applied to one of them. -
- int ptr new int
- ptr 8
- int ptr2 new int
- ptr2 -5
- ptr ptr2
-
60Leaving a Dangling Pointer
- int ptr new int
- ptr 8
- int ptr2 new int
- ptr2 -5
- ptr ptr2
-
- delete ptr2 // ptr is left dangling
- ptr2 NULL
8 ptr NULL
ptr2
61Classes and Dynamic MemoryAllocation
62- class Counter
-
- public
- Counter( )
- Counter(int initCount)
- void incrementCounter( )
- void decrementCounter( )
- void setCounter(int initCount)
- int getCounter( )
- private
- int countptr
countptr
63- include "counter.h
- CounterCounter( ) // default constructor
-
- countptr new int
- countptr 0
0
countptr
countptr
64- CounterCounter(int initCount) // parameterized
constructor -
- countptr new int
- countptr initCount
initCount
countptr
countptr
65- void CountersetCounter(int initCount)
-
- countptr initCount
initCount
countptr
countptr
66- void CounterincrementCounter( )
-
- countptr countptr 1
0
countptr
1
countptr
67- void CounterdecrementCounter( )
-
- countptr countptr - 1
1
countptr
0
countptr
68- int CountergetCounter( ) const
-
- return countptr
0
countptr
countptr
69Destructor Syntax
- Example
- CounterCounter( ) // Implementation for
destructor -
- delete countptr // memory location pointed to
by - // countptr is returned to heap
- NOTE the pointer is NOT returned to the heap