Title: Data Structures and Algorithms
1Data Structures and Algorithms
2Guidelines for Selecting ADT Operations
- Completeness -- Every ADT must be complete. It
must include enough operations, including
ADT-constructors, to build every possible value
of the domain. -
- Testability -- The preconditions of all ADT
operations must be testable. The ADT must
provide enough test operations for the client to
check all preconditions of the ADT operations. -
2
3Types of ADT Operations to Include
- ADT-Constructors -- to build values of the type
- Test Operations -- to test some condition about
an ADT value -
- I/O Operations -- to input or output an ADT
value - Copy Operations -- to copy one ADT value to
another ADT object -
- Selector Operations (Access Functions) -- to
retrieve a portion of an ADT value -
- Destructor Operations -- to deallocate any
dynamic data created by an ADT
3
4Definition of ADT Stack
- A stack is a linear data structure with
homogeneous components (elements), in which all
insertions and deletions occur at one end, called
the top of the stack. - A stack is a LIFO Last In, First Out structure.
5StackType ADT Operations
- StackType -- (Constructor) Creates an empty
stack. - 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 -- Removes the item at the top of the stack.
-
- Top -- Returns a copy of item currently at top of
stack.
5
6ADT Stack Operations
- Transformers
- StackType
- Push
- Pop
- Observers
- IsEmpty
- IsFull
- Top
-
change state observe state
6
7 Vector Implementation of Stack
StackType class
Private data top MAX_ITEMS-1
.
. .
2 1 items
0
StackType
IsEmpty
IsFull
Push
Pop
Top
8Stack of int items
8
9Stack of float items
9
10Stack of string items
10
11- //------------------------------------------------
---------- - // SPECIFICATION FILE (bstack.h)
- //------------------------------------------------
---------- - include "bool.h"
- include "ItemType.h" // for MAX_ITEMS and
- // class ItemType definition
- class StackType
- public
- StackType( )
- // Default constructor.
- // POST Stack is created and empty.
-
- Boolean IsEmpty( ) const
- // PRE Stack has been initialized.
- // POST FCTVAL (stack is empty)
- Boolean IsFull( ) const
- // PRE Stack has been initialized.
11
12- // SPECIFICATION FILE continued (bstack.h)
- void Push( ItemType newItem )
- // PRE Stack has been initialized and is not
full. - // Assigned (newItem).
- // POST newItem is at the top of the stack.
- void Pop( )
- // PRE Stack has been initialized and is not
empty. - // POST Top element has been removed from
stack. - ItemType Top( ) const
- // PRE Stack has been initialized and is not
empty. - // POST FCTVAL copy of item at top of
stack. - private
- int top // subscript of current top item
- ItemType itemsMAX_ITEMS // array of
ItemType
12
13Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
letter charStack.Top( ) charStack.Pop(
) charStack.Push(K)
14Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
letter charStack.Top( ) charStack.Pop(
) charStack.Push(K)
Private data top -1
MAX_ITEMS-1 .
.
. 2
1 items 0
15Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
letter charStack.Top( ) charStack.Pop(
) charStack.Push(K)
Private data top 0
MAX_ITEMS-1 .
.
. 2
1 items 0
V
16Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
letter charStack.Top( ) charStack.Pop(
) charStack.Push(K)
Private data top 1
MAX_ITEMS-1 .
.
. 2
1 C items
0 V
17Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
letter charStack.Top( ) charStack.Pop(
) charStack.Push(K)
Private data top 2
MAX_ITEMS-1 .
.
. 2 S
1 C
items 0 V
18Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
letter charStack.Top( ) charStack.Pop(
) charStack.Push(K)
Private data top 2
MAX_ITEMS-1 .
.
. 2 S
1 C
items 0 V
19Tracing Client Code
S
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
letter charStack.Top( ) charStack.Pop(
) charStack.Push(K)
Private data top 2
MAX_ITEMS-1 .
.
. 2 S
1 C
items 0 V
20Tracing Client Code
S
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
letter charStack.Top( ) charStack.Pop(
) charStack.Push(K)
Private data top 1
MAX_ITEMS-1 .
.
. 2 S
1 C
items 0 V
21End of Trace
S
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
letter charStack.Top( ) charStack.Pop(
) charStack.Push(K)
Private data top 2
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
22- //------------------------------------------------
------- - // IMPLEMENTATION FILE (bstack.cpp)
- //------------------------------------------------
------ - // Private data members of class
- // int top
- // ItemType itemsMAX_ITEMS
- //------------------------------------------------
------- - include bool.h
- include ItemType.h
- StackTypeStackType( )
- //----------------------------------------------
-- - // Default Constructor
- //----------------------------------------------
-- -
- top -1
22
23- // IMPLEMENTATION FILE continued (bstack.cpp)
- //------------------------------------------------
---------- - Boolean StackTypeIsEmpty( ) const
- //----------------------------------------------
----- - // PRE Stack has been initialized.
- // POST FCTVAL (stack is empty)
- //----------------------------------------------
----- -
- return ( top -1 )
-
- Boolean StackTypeIsFull( ) const
- //----------------------------------------------
----- - // PRE Stack has been initialized.
- // POST FCTVAL (stack is full)
- //----------------------------------------------
----- -
- return ( top MAX_ITEMS-1 )
23
24- // IMPLEMENTATION FILE continued (bstack.cpp)
- //------------------------------------------------
---------- - void StackTypePush ( ItemType newItem )
- //----------------------------------------------
----- - // PRE Stack has been initialized and is not
full. - // POST newItem is at the top of the stack.
- //----------------------------------------------
----- -
- top
- itemstop newItem
25- // IMPLEMENTATION FILE continued (bstack.cpp)
- //------------------------------------------------
---------- - ItemType StackTypeTop( ) const
- //----------------------------------------------
----- - // PRE Stack has been initialized top gt
0. - // POST FCTVAL copy of item at top of
stack. - //----------------------------------------------
----- -
- return itemstop
26- // IMPLEMENTATION FILE continued (bstack.cpp)
- //------------------------------------------------
---------- - void StackTypePop ( )
- //----------------------------------------------
----- - // PRE Stack has been initialized top gt
0. - // POST Top element has been removed from
stack. - //----------------------------------------------
----- -
- top--
27Another Stack Implementation
- One advantage of an ADT is that the kind of
implementation used can be changed. - The array implementation of the stack has a
weakness -- the maximum size of the stack is
fixed at compile time. - Instead we can dynamically allocate the space for
each stack element as it is pushed onto the
stack.
28 class StackType
StackType
IsEmpty
cat dog
IsFull
Push
Pop
Top
29Tracing Client Code
V
letter
char letter V StackTypelt char gt
myStack myStack.Push(letter) myStack.Push(C)
myStack.Push(S) if ( !myStack.IsEmpty( ) )
letter myStack.Top( )
myStack.Pop( ) myStack.Push(K)
30Tracing Client Code
V
letter
Private data topPtr NULL
char letter V StackTypelt char gt
myStack myStack.Push(letter) myStack.Push(C)
myStack.Push(S) if ( !myStack.IsEmpty( ) )
letter myStack.Top( )
myStack.Pop( ) myStack.Push(K)
31Tracing Client Code
letter
V
V
char letter V StackTypelt char gt
myStack myStack.Push(letter) myStack.Push(C)
myStack.Push(S) if ( !myStack.IsEmpty( ) )
letter myStack.Top( )
myStack.Pop( ) myStack.Push(K)
32Tracing Client Code
letter
V
C V
char letter V StackTypelt char gt
myStack myStack.Push(letter) myStack.Push(C)
myStack.Push(S) if ( !myStack.IsEmpty( ) )
letter myStack.Top( )
myStack.Pop( ) myStack.Push(K)
33Tracing Client Code
letter
V
S C V
char letter V StackTypelt char gt
myStack myStack.Push(letter) myStack.Push(C)
myStack.Push(S) if ( !myStack.IsEmpty( ) )
letter myStack.Top( )
myStack.Pop( ) myStack.Push(K)
34Tracing Client Code
letter
V
char letter V StackTypelt char gt
myStack myStack.Push(letter) myStack.Push(C)
myStack.Push(S) if ( !myStack.IsEmpty( ) )
letter myStack.Top( )
myStack.Pop( ) myStack.Push(K)
35Tracing Client Code
letter
S
char letter V StackTypelt char gt
myStack myStack.Push(letter) myStack.Push(C)
myStack.Push(S) if ( !myStack.IsEmpty( ) )
letter myStack.Top( )
myStack.Pop( ) myStack.Push(K)
36Tracing Client Code
letter
S
C V
char letter V StackTypelt char gt
myStack myStack.Push(letter) myStack.Push(C)
myStack.Push(S) if ( !myStack.IsEmpty( ) )
letter myStack.Top( )
myStack.Pop( ) myStack.Push(K)
37Tracing Client Code
letter
S
K C V
char letter V StackTypelt char gt
myStack myStack.Push(letter) myStack.Push(C)
myStack.Push(S) if ( !myStack.IsEmpty( ) )
letter myStack.Top( )
myStack.Pop( ) myStack.Push(K)
38Dynamically Linked Stack
- // SPECIFICATION OF STACK (ustack.h)
-
- include "bool.h"
- include "ItemType.h" // for ItemType
- struct NodeType
- ItemType info
- NodeType next
39Pointer Dereferencing and Member Selection
A 6000
ptr
ptr
ptr
A 6000
. info . next
ptr
ptr
A 6000
(ptr).info ptr-gtinfo
40- // SPECIFICATION OF STACK continued
(ustack.cpp) - class StackType
- public
- StackType( ) // constructor
- // Default constructor.
- // POST Stack is created and empty.
- Boolean IsEmpty( ) const
- // PRE Stack has been initialized.
- // POST FCTVAL (stack is empty)
- Boolean IsFull( ) const
- // PRE Stack has been initialized.
- // POST FCTVAL (stack is full)
- StackType(const StackType otherStk ) //
Copy-constructor - // POST Created stack as deep copy of
otherStk
41- // SPECIFICATION OF STACK continued
(ustack.cpp) - void Push( ItemType item )
- // PRE Stack initialized Stack is not
full. - // POST newItem is at the top of the stack.
- void Pop( )
- // PRE Stack initialized Stack is not
empty. - // POST Top element has been removed from
stack. - ItemType Top( ) const
- // PRE Stack initialized Stack is not
empty. - // POST FCTVAL copy of item currently at
top. - StackType( ) // destructor
- // PRE Stack has been initialized.
- // POST Memory allocated for nodes has been
- // deallocated.
- private
- NodeType char topPtr
41
42- // DYNAMIC LINKED IMPLEMENTATION OF STACK
(ustack.cpp) - // member function definitions for class
StackType - StackTypeStackType( ) // constructor
-
- topPtr NULL
-
- void StackTypeIsEmpty( ) const
- // Returns true if there are no elements
- // on the stack false otherwise
-
- return ( topPtr NULL )
43Using operator new
- If memory is available in an area called the free
store (or heap), operator new allocates the
requested object, and returns a pointer to the
memory allocated. - The dynamically allocated object exists until the
delete operator destroys it.
43
44Adding newItem to the stack
B
newItem
- newItem B
- NodeType location
- location new NodeType
- location-gtinfo newItem
- location-gtnext topPtr
- topPtr location
-
45Adding newItem to the stack
B
newItem
- newItem B
- NodeType location
- location new NodeType
- location-gtinfo newItem
- location-gtnext topPtr
- topPtr location
-
location
46Adding newItem to the stack
B
newItem
- newItem B
- NodeType location
- location new NodeType
- location-gtinfo newItem
- location-gtnext topPtr
- topPtr location
-
location
47Adding newItem to the stack
B
newItem
- newItem B
- NodeType location
- location new NodeType
- location-gtinfo newItem
- location-gtnext topPtr
- topPtr location
-
B
location
48Adding newItem to the stack
B
newItem
- newItem B
- NodeType location
- location new NodeType
- location-gtinfo newItem
- location-gtnext topPtr
- topPtr location
-
B
location
49Adding newItem to the stack
B
newItem
- newItem B
- NodeType location
- location new NodeType
- location-gtinfo newItem
- location-gtnext topPtr
- topPtr location
-
X C L
B
location
50Implementing Push
- void StackTypePush ( ItemType newItem )
- // Adds newItem to the top of the stack.
-
- NodeType location
- location new NodeType
- location-gtinfo newItem
- location-gtnext topPtr
- topPtr location
51Using operator delete
- The object currently pointed to by the pointer is
deallocated, and the pointer is considered
unassigned. The memory is returned to the free
store.
51
52Deleting item from the stack
item
- NodeType tempPtr
-
- item topPtr-gtinfo
- tempPtr topPtr
- topPtr topPtr-gtnext
- delete tempPtr
-
B X C L
tempPtr
53Deleting item from the stack
B
item
- NodeType tempPtr
-
- item topPtr-gtinfo
- tempPtr topPtr
- topPtr topPtr-gtnext
- delete tempPtr
-
B X C L
tempPtr
54Deleting item from the stack
B
item
- NodeType tempPtr
-
- item topPtr-gtinfo
- tempPtr topPtr
- topPtr topPtr-gtnext
- delete tempPtr
-
B X C L
tempPtr
55Deleting item from the stack
B
item
- NodeType tempPtr
-
- item topPtr-gtinfo
- tempPtr topPtr
- topPtr topPtr-gtnext
- delete tempPtr
-
B X C L
tempPtr
56Deleting item from the stack
B
item
- NodeType tempPtr
-
- item topPtr-gtinfo
- tempPtr topPtr
- topPtr topPtr-gtnext
- delete tempPtr
-
X C L
tempPtr
57- void StackTypePop ( )
- // Removes element at the top of the stack.
-
- NodeType tempPtr
-
- tempPtr topPtr
- topPtr topPtr-gtnext
- delete tempPtr
-
- void StackTypeTop ( ) const
- // FCTVAL copy of element at the top of
stack. -
- return topPtr-gtinfo
58- Boolean StackTypeIsFull( ) const
- // Returns true if there is no room for
- // another NodeType node on the free store
- // false otherwise.
-
- location new NodeTypeltItemTypegt
- if ( location NULL )
- return true
- else
-
- delete location
- return false
-
59Why is a destructor needed?
- When a local stack variable goes out of scope,
the memory space for data member topPtr is
deallocated. But the nodes that topPtr points to
are not automatically deallocated. - A class destructor is used to deallocate the
dynamic memory pointed to by the data member.
60- StackTypeStackType( ) // destructor
- // Post Stack is empty all elements
deallocated. -
- NodeType tempPtr
- while ( topPtr ! NULL )
-
- tempPtr topPtr
- topPtr topPtr-gtnext
- delete tempPtr
-
-
61 class StackType
StackType
IsEmpty
20 30
IsFull
Push
Pop
Top
62 What happens . . .
- When a function is called that uses pass by value
for a class object like our dynamically linked
stack?
MakeEmpty
IsEmpty
20 30
IsFull
Push
Pop
StackType
63Passing a class object by value
-
- void MyFunction( StackType SomeStack ) //
FUNCTION CODE -
- // Uses pass by value
-
- .
- .
- .
- .
64 Pass by value makes a shallow copy
StackType MyStack // CLIENT
CODE . . . MyFunction(
MyStack ) // function call
MyStack
SomeStack
Private data topPtr 7000
Private data 7000
6000 topPtr 7000
20 30
shallow copy
65Shallow Copy vs. Deep Copy
- A shallow copy copies only the class data
members, and does not make a copy of any
pointed-to data. - A deep copy copies not only the class data
members, but also makes separately stored copies
of any pointed-to data.
66Whats the difference?
- A shallow copy shares the pointed to data with
the original class object. - A deep copy stores its own copy of the pointed to
data at different locations than the data in the
original class object.
67 Making a deep copy
MyStack
Private data 7000
6000 topPtr 7000
20 30
SomeStack
Private data 5000
2000 topPtr 5000
20 30
deep copy
68Suppose MyFunction Uses Pop
- // FUNCTION CODE
- void MyFunction( StackType SomeStack )
- // Uses pass by value
-
- SomeStack.Pop( )
- .
- .
- .
-
-
- WHAT HAPPENS IN THE SHALLOW COPY SCENARIO?
69 MyStack.topPtr is left dangling
StackTypeltintgt MyStack // CLIENT CODE
. . . MyFunction( MyStack )
MyStack
SomeStack
Private data topPtr 6000
Private data 7000
6000 topPtr 7000
? 30
shallow copy
70 MyStack.topPtr is left dangling
MyStack
SomeStack
Private data topPtr 6000
Private data 7000
6000 topPtr 7000
? 30
shallow copy
71As a result . . .
- This default method used for pass by value is not
the best way when a data member pointer points to
dynamic data. - Instead, you should write what is called a copy
constructor, which makes a deep copy of the
dynamic data in a different memory location.
72More about copy constructors
- When there is a copy constructor provided for a
class, the copy constructor is used to make
copies for pass by value. - You do not call the copy constructor.
- Like other constructors, it has no return type.
- Because the copy constructor properly defines
pass by value for your class, it must use pass by
reference in its definition.
73Copy Constructor
- Copy constructor is a special member function of
a class that is implicitly called in these 3
situations - passing object parameters by value,
- initializing an object variable in its
declaration, - returning an object as the return value of a
function.
74- // DYNAMIC LINKED IMPLEMENTATION OF STACK
- class StackType
- public
- StackType( )
- // Default constructor.
- // POST Stack is created and empty.
- StackType( const StackType otherStk )
- // Copy constructor.
- // POST this Stack is a deep copy of otherStk
- // Is implicitly called for pass by value.
- .
- .
- .
- StackType( )
- // Destructor.
- // POST Memory for nodes has been deallocated.
- private
- NodeType topPtr
74
75Classes with Data Member Pointers Need
- CONSTRUCTOR
- COPY CONSTRUCTOR
- DESTRUCTOR
76- // COPY CONSTRUCTOR
- StackTypeStackType( const StackType otherStk )
- NodeType fromPtr // traverses otherStk nodes
- NodeType toPtr // traverses this Stack nodes
- if (otherStk.topPtr NULL )
- topPtr NULL
- else // allocate memory to copy first
node - topPtr new NodeType
- topPtr-gtinfo otherStk.topPtr-gtinfo
- fromPtr otherStk.topPtr-gtnext
- toPtr topPtr
- while ( fromPtr ! NULL ) // deep copy other
nodes - toPtr-gtnext new NodeType
- toPtr toPtr-gtnext
- toPtr-gtinfo fromPtr-gtinfo
- fromPtr fromPtr-gtnext
-
- toPtr-gtnext NULL
-
76
77What about the assignment operator?
- The default method used for assignment of class
objects makes a shallow copy. - If your class has a data member pointer to
dynamic data, you should write a member function
to overload the assignment operator to make a
deep copy of the dynamic data.
78- // DYNAMIC LINKED IMPLEMENTATION OF STACK
- class StackType
- public
- StackType( )
- // Default constructor.
- StackType( const StackType otherStk )
- // Copy constructor.
- void operator ( StackType otherStk)
- // Overloads assignment operator.
- .
- .
- .
- StackType( )
- // Destructor.
- private
- NodeType topPtr
78
79Overloading the assignment operator
- // DYNAMIC LINKED IMPLEMENTATION OF STACK
- void StackTypeoperator
- ( StackType otherStk )
- // Overloads assignment operator
- // to create a deep copy of otherStk.
-
- .
- .
- .
80C Operator Overloading Guides
- All operators except . sizeof ? may
be overloaded. - At least one operand must be a class instance.
- You cannot change precedence, operator symbols,
or number of operands. - Overloading and -- requires prefix form use by
default, unless special mechanism is used. - To overload these operators ( ) member
functions (not friend functions) must be used. - An operator can be given multiple meanings if the
data types of operands differ for different
meanings.
81Using overloaded binary operator
- After defining member function operator
- myStack yourStack
- Is translated by compiler into function call
- myStack.operator(yourStack)
-
81
82Using overloaded binary operator
- When a Member Function was defined
- myStack yourStack
- myStack.operator(yourStack)
- When a Friend Function was defined
- myStack yourStack
- operator(myStack, yourStack)
-
82
83What is ADT Queue?
- A queue is a linear data structure with
homogeneous components (elements), in which all
insertions occur at the rear, and all deletions
occur at the front. - A queue is a FIFO First In, First Out structure.
84Queue ADT Operations
- QueueType -- (Constructor) Creates an empty
queue. - IsEmpty -- Determines whether queue is currently
empty. -
- IsFull -- Determines whether queue is currently
full. -
- Enqueue (ItemType newItem) -- Adds newItem to
the rear of queue. -
- Dequeue -- Removes the item at the front of
queue. -
- Front -- Returns a copy of item at the front of
queue.
84
85ADT Queue Operations
- Transformers
- QueueType
- Enqueue
- Dequeue
- Observers
- IsEmpty
- IsFull
- Front
-
change state observe state
85
86class QueueType
Private Data front rear
QueueType
QueueType
Enqueue
Dequeue . . .
87- // SPECIFICATION OF QUEUE (uqueue.cpp)
-
- include "ItemType.h" // for ItemType
- class QueueType
- public
- QueueType( ) // CONSTRUCTOR
- QueueType( ) // DESTRUCTOR
- Boolean IsEmpty( ) const
- Boolean IsFull( ) const
- void Enqueue( ItemType item )
- void Dequeue( )
- ItemType Front( ) const
- QueueType( const QueueType otherQue )
- // COPY-CONSTRUCTOR
- private
- NodeType front
- NodeType rear
87
88- // DYNAMIC LINKED IMPLEMENTATION OF QUEUE
(uqueue.cpp) - // member function definitions for class
QueueType - QueueTypeQueueType( ) // CONSTRUCTOR
-
- front NULL
- rear NULL
-
- Boolean QueueTypeIsEmpty( ) const
-
- return ( front NULL )
89- void QueueTypeEnqueue( ItemType newItem )
- // Adds newItem to the rear of the queue.
- // Pre Queue has been initialized.
- // Queue is not full.
- // Post newItem is at rear of queue.
-
- NodeType ptr
- ptr new NodeType
- ptr-gtinfo newItem
- ptr-gtnext NULL
- if ( rear NULL )
- front ptr
- else
- rear-gtnext ptr
- rear ptr
-
90- void QueueTypeDequeue( )
- // Removes element from from front of queue
- // Pre Queue has been initialized.
- // Queue is not empty.
- // Post Front element has been removed from
queue. -
- NodeType tempPtr
- tempPtr front
- front front-gtnext
- if ( front NULL )
- rear NULL
- delete tempPtr