Title: Nell Dale
1C Plus Data Structures
Nell Dale David Teague Chapter 6 Lists
Plus Slides by Sylvia Sorkin, Community College
of Baltimore County - Essex Campus
2ADT Sorted List Operations
- Transformers
- MakeEmpty
- InsertItem
- DeleteItem
- Observers
- IsFull
- LengthIs
- RetrieveItem
-
- Iterators
- ResetList
- GetNextItem
change state observe state process all
3 class SortedTypeltchargt
SortedType
Private data length
3 listData currentPos
MakeEmpty
SortedType
RetrieveItem
InsertItem
DeleteItem . . .
GetNextItem
4What is a Circular Linked List?
- A circular linked list is a list in which every
node has a successor the last element is
succeeded by the first element.
B C L
listData
5Fig 6.1 A circular linked list
listData
C
A
D
B
6Fig 6.2 A circular linked list with the
external pointer pointing to the rear element
listData
(a)
C
A
D
B
(b)
listData
A
(c)
listData
(empty list)
7Finding a List Item
- RetrieveItem, InsertItem, and DeleteItem all
require a search.Lets write a general FindItem
routine that takes item as a parameter and
returns location, predLoc, and found.InsertItem
and DeleteItem need the location of the
predecessor node(predloc) - In the linear list, we initialized location to
point to the first node in the list and set
predLoc to NULL(fig 6.3a) - For the circular list search, we initialized
location to point to the first node and set
predLoc to point to its predecessor last node
in the list(fig 6.3b)
8Fig 6.3 Initializing for FindItem
(a) For a linear linked list
listData
C
A
B
location listData predLoc NULL
location
predLoc
(b) For a circular linked list
C
A
B
location listData-gt next predLoc listData
predLoc
location
listdata
9Finding a List Item
- FindElement- Algorithm
- Set location to Next(listData)
- Set predloc to listData
- Set found to false
- Set moreToSearch to true
- while moreToSearch to true AND NOT Found DO
- if(item lt Info(location)
- Set moreToSearch to false
- else if (item Info(location)
- Set found to true
- else
- Set predLoc to location
- Set location to Next(location)
- Set moreToSearch to (location ! Next(listData)
10Fig 6.4 The FindItem operation for a circular
list
(a) The general case (Find B)
listData
predLoc
location
C
A
D
B
11Fig 6.4 The FindItem operation for a circular
list
(b) Searching for the smallest item (Find A)
listData
location
predLoc
C
A
D
B
12Fig 6.4 The FindItem operation for a circular
list
(c) Searching for the item that isnt there (Find
C)
predLoc
listData
location
J
A
K
B
13Fig 6.4 The FindItem operation for a circular
list
(d) Searching for the item bigger than any in the
list (Find E)
listData
predLoc
location
C
A
D
B
14Inserting into a Circular List
- InsertItem Algorithm
- Set newNode to address of newly allocated node
- Set Info(newNode) to item
- Find the place where the new element belongs
- Put new element into the list
15Inserting into a Circular List
- The task of allocating space as for the linear
list.we allocate space for the node using the new
operator and then store item into newNode-gtinfo - The next task is equally simple,just call
- FindItem(listdata, item, location, predLoc,
found)
16Inserting into a Circular List
- We dont find the element because it isnt there
it is predLoc pointer that interests us. The new
node is linked into the list immediately after
Node(predLoc). - To put the new element into the list we store
- predLoc-gtnext into newNode-gtnext and
- newNode into predLoc-gtnext.
17Fig 6.5 Inserting into a circular linked list
(a) General case (Insert C)
newNode
B
listData
predLoc
1
2
D
A
E
B
18Fig 6.5 Inserting into a circular linked list
(b) Special case the empty list (Insert A)
listData
newNode
A
19Fig 6.5 Inserting intoa circular linked list
(c) Special case ( ?) inserting to front of list
(Insert A)
newNode
A
listData
predLoc
1
2
C
D
B
20Fig 6.5 Inserting into a circular linked list
(d) Special caseinserting to end of list (Insert
E)
listData
newNode
predLoc
D
A
E
B
2
1
21Deleting from a Circular List
- DeleteItem - Algorithm
- Find the element in the list
- Remove the element from the list
- Deallocate the node
22Fig 6.6 Deleting from a circular linked list
predLoc-gt next location-gt next
predLoc
listData
location
C
A
B
(a)General case (Delete B)
23Fig 6.6 Deleting from a circular linked list
predLoc-gt next location-gt next
location
listData
predLoc
C
A
B
(b)Special case(?) deleting the only item
(DeleteA)
24Fig 6.6 Deleting from a circular linked list
predLoc-gt next location-gt next (the general
case PLUS) listData predLoc
listData
location
predLoc
C
A
B
(d) Special casedeleting the largest item
(Delete C)
25What is a Doubly Linked List?
- A doubly linked list is a list in which each node
is linked to both its successor and its
predecessor.
A C F
T Z
listData
26Each node contains two pointers
templatelt class ItemType gt struct NodeType
ItemType info // Data member
NodeTypeltItemTypegt back // Pointer to
predecessor NodeTypeltItemTypegt next //
Pointer to successor
3000 A NULL
. back . info . next
27What are Header and Trailer Nodes?
- A Header Node is a node at the beginning of a
list that contains a key value smaller than any
possible key. - A Trailer Node is a node at the end of a list
that contains a key larger than any possible key. - Both header and trailer are placeholding nodes
used to simplify list processing.
INT_MIN 5 8
13 INT_MAX
listData
28Recall Definition of Stack
- Logical (or ADT) level 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.
29Stack 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.
29
30 class StackTypeltintgt
31 What happens . . .
- When a function is called that uses pass by
value for a class object like our dynamically
linked stack?
32Passing a class object by value
- // FUNCTION CODE
- templateltclass ItemTypegt
- void MyFunction( StackTypeltItemTypegt SomeStack )
- // Uses pass by value
-
- .
- .
- .
- .
32
33 Pass by value makes a shallow copy
StackTypeltintgt MyStack // CLIENT
CODE . . . MyFunction(
MyStack ) // function call
MyStack
SomeStack
Private data topPtr 7000
Private data 7000
6000 topPtr 7000
20 30
shallow copy
34Shallow Copy vs. Deep Copy
- A shallow copy copies only the class data
members, and does not copy 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.
35Whats 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.
36 Making a deep copy
MyStack
Private data 7000
6000 topPtr 7000
20 30
SomeStack
Private data 5000
2000 topPtr 5000
20 30
deep copy
37Suppose MyFunction Uses Pop
- // FUNCTION CODE
- templateltclass ItemTypegt
- void MyFunction( StackTypeltItemTypegt SomeStack )
- // Uses pass by value
-
- ItemType item
- SomeStack.Pop(item)
- .
- .
- .
-
- WHAT HAPPENS IN THE SHALLOW COPY SCENARIO?
37
38 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
39 MyStack.topPtr is left dangling
NOTICE THAT NOT JUST FOR THE SHALLOW COPY, BUT
ALSO FOR ACTUAL PARAMETER MyStack, THE DYNAMIC
DATA HAS CHANGED!
MyStack
SomeStack
Private data topPtr 6000
Private data 7000
6000 topPtr 7000
? 30
shallow copy
40As 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.
41More 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.
42Copy Constructor
- Copy constructor is a special member function of
a class that is implicitly called in these three
situations - passing object parameters by value,
- initializing an object variable in a
declaration, - returning an object as the return value of a
function.
43- // DYNAMICALLY LINKED IMPLEMENTATION OF STACK
- templateltclass ItemTypegt
- class StackType
- public
- StackType( )
- // Default constructor.
- // POST Stack is created and empty.
- StackType( const StackTypeltItemTypegt
anotherStack ) - // Copy constructor.
- // Implicitly called for pass by value.
- .
- .
- .
- StackType( )
- // Destructor.
- // POST Memory for nodes has been deallocated.
- private
43
44Classes with Data Member Pointers Need
- CLASS CONSTRUCTOR
- CLASS COPY CONSTRUCTOR
- CLASS DESTRUCTOR
45- templateltclass ItemTypegt // COPY CONSTRUCTOR
- StackTypeltItemTypegt
- StackType( const StackTypeltItemTypegt
anotherStack ) - NodeTypeltItemTypegt ptr1
- NodeTypeltItemTypegt ptr2
- if ( anotherStack.topPtr NULL )
- topPtr NULL
- else // allocate memory for first node
- topPtr new NodeTypeltItemTypegt
- topPtr-gtinfo anotherStack.topPtr-gtinfo
- ptr1 anotherStack.topPtr-gtnext
- ptr2 topPtr
- while ( ptr1 ! NULL ) // deep copy other nodes
- ptr2-gtnext new NodeTypeltItemTypegt
- ptr2 ptr2-gtnext
- ptr2-gtinfo ptr1-gtinfo
- ptr1 ptr1-gtnext
-
- ptr2-gtnext NULL
45
46What 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.(See additional
Lect16)
47- // DYNAMICALLY LINKED IMPLEMENTATION OF STACK
- templateltclass ItemTypegt
- class StackType
- public
- StackType( )
- // Default constructor.
- StackType( const StackTypeltItemTypegt
anotherStack ) - // Copy constructor.
- void operator ( StackTypeltItemTypegt )
- // Overloads assignment operator.
- .
- .
- .
- StackType( )
- // Destructor.
- private
47
48C Operator Overloading Guides
- All operators except these . 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.
49Using 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)
-
49
50Composition (containment)
- Composition (or containment) means that an
internal data member of one class is defined to
be an object of another class type.
A FAMILIAR EXAMPLE . . .
51ItemType Class Interface Diagram
class ItemType
ComparedTo
Private data value
Print
Initialize
52 Sorted list contains an array of ItemType
SortedType class
MakeEmpty
Private data length info 0
1 2
MAX_ITEMS-1 currentPos
IsFull
LengthIs
RetrieveItem
InsertItem
DeleteItem
ResetList
GetNextItem
53Inheritance
- Inheritance is a means by which one class
acquires the properties--both data and
operations--of another class. - When this occurs, the class being inherited from
is called the Base Class. - The class that inherits is called the Derived
Class.
AN EXAMPLE . . .
54Recall Definition of 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.
55Queue 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.
55
56class QueTypeltchargt
QueType
Private Data qFront qRear
QueType
Enqueue
Dequeue . . .
57- // DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE
-
- include "ItemType.h" // for ItemType
- templateltclass ItemTypegt
- class QueType
- public
- QueType( ) // CONSTRUCTOR
- QueType( ) // DESTRUCTOR
- bool IsEmpty( ) const
- bool IsFull( ) const
- void Enqueue( ItemType item )
- void Dequeue( ItemType item )
- void MakeEmpty( )
- private
- NodeTypeltItemTypegt qFront
- NodeTypeltItemTypegt qRear
57
58SAYS ALL PUBLIC MEMBERS OF QueType CAN BE INVOKED
FOR OBJECTS OF TYPE CountedQue
- // DERIVED CLASS CountedQue FROM BASE CLASS
QueType - templateltclass ItemTypegt
- class CountedQue public QueTypeltItemTypegt
-
- public
- CountedQue( )
- void Enqueue( ItemType newItem )
- void Dequeue( ItemType item )
- int LengthIs( ) const
- // Returns number of items on the counted queue.
- private
- int length
58
59class CountedQueltchargt
60- // Member function definitions for class
CountedQue - templateltclass ItemTypegt
- CountedQueltItemTypegtCountedQue( )
QueTypeltItemTypegt( ) -
- length 0
-
- templateltclass ItemTypegt
- int CountedQueltItemTypegtLengthIs( ) const
-
- return length
60
61- templateltclass ItemTypegt
- void CountedQueltItemTypegtEnqueue( ItemType
newItem ) - // Adds newItem to the rear of the queue.
- // Increments length.
-
- length
- QueTypeltItemTypegtEnqueue( newItem )
-
- templateltclass ItemTypegt
- void CountedQueltItemTypegtDequeue(ItemType item
) - // Removes item from the rear of the queue.
- // Decrements length.
-
- length--
- QueTypeltItemTypegtDequeue( item )
61