![Page 1: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/1.jpg)
Chapter 6
Lists Plus
![Page 2: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/2.jpg)
2
Goals
• Use the C++ template mechanism fr defining generic data types
• Implement a circular linked list
• Implement a linked list with a header node or a trailer node or both
• Implement a doubly linked list
• Distinguish between shallow copying and deep copying
![Page 3: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/3.jpg)
3
Goals
• Overload C++ operators
• Implement a linked list as an array of records
• Implement dynamic binding with virtual functions
![Page 4: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/4.jpg)
4
C++ Templates
Generic Data Type
A data type for which the operations are defined but the types of the items being manipulated are not
Template
A C++ construct that allows the computer to generate multiple versions of a class type or a function by allowing parameterized types
Templates implement generic types
![Page 5: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/5.jpg)
5
C++ Templates
template <class ItemType>
class StackType
{ ... }
// client code
StackType<int>;
StackType<float>;
StackType<NameType>;
Formal parameter
Actual parameters
![Page 6: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/6.jpg)
6
C++ Templates
Summary
• The formal parameters are in the class template definition
• The actual parameters are in the client code
• Both formal and actual parameters are enclosed in angle brackets <...>
• The actual parameter can be any type, built-in or user-defined
![Page 7: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/7.jpg)
7
C++ Templates
What about function templates?
Formal parameter precedes function definition and follows class name before scope resolution operator
![Page 8: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/8.jpg)
8
C++ Templates
template<class ItemType>
void StackType<ItemType>::Push(ItemType newItem)
{
if (IsFull()) throw FullStack();
else
{
top++;
items[top] = newItem;
}
}
Formal parameters
![Page 9: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/9.jpg)
9
Circular Linked Lists
Circular linked list
A list in which each node has a successor; the “last” element is succeeded by the “first” element
![Page 10: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/10.jpg)
10
Circular Linked List
Why is it better to have
the external pointerpoint to thelast
element?
![Page 11: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/11.jpg)
11
Circular Linked List
Initialization for search
![Page 12: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/12.jpg)
12
Circular Linked List
FindItemcases
![Page 13: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/13.jpg)
13
Circular Linked List
Insertan
item
![Page 14: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/14.jpg)
14
Circular Linked List
Deletean
Item
![Page 15: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/15.jpg)
15
Doubly Linked List
Doubly linked list
A is in which each node is linked to both its successor and its predecessor
![Page 16: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/16.jpg)
16
Doubly Linked List
Insertan
item
![Page 17: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/17.jpg)
17
Doubly Linked List
Doesit
matterin
whichorderwe
changethe
pointers?
![Page 18: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/18.jpg)
18
Doubly Linked List
Delete an item
![Page 19: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/19.jpg)
19
Header and Trailer Nodes
Header NodeA placeholder node at the beginning of a list that contains a key value smaller than any possible keyTrailer NodeA placeholder node at the end of a list that contains a key larger than any possible key
How do header/trailer
nodessimplify
processing?
![Page 20: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/20.jpg)
20
Copy Structures
void CopyStack(StackType oldStack), StackType& copy){ StackType tempStack; ItemType item; while (!oldStack.IsEmpty()) { item = oldStack.Top(); oldStack.Pop(); temptStack.Push(item); } while (!tempStack.IsEmpty()) { item = tempStack.Top(); copy.Pop(); }}
What isthe status
of oldStack?
copy?Does the
implementationstructurematter?
![Page 21: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/21.jpg)
21
Copy Structures
oldStack is a value parameter; doesn’t that protect it from change?
Yes, but only in the array-based implementation
private: int top; ItemType items[MAX_ITEMS];
top and items are enclosed within the stack object
![Page 22: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/22.jpg)
22
Copy Structures
In a linked structure, the external pointer is enclosed within the stack object, but the linked structure to which it points is not
Whatcauses
thisproblem
?
![Page 23: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/23.jpg)
23
Copy Structures
Shallow copy
An operation that copies one class object to another without copying pointed-to data
Deep copy
An operation that not only copies one class object to another but also makes copies of any pointed-to data
See the difference?
![Page 24: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/24.jpg)
24
Copy Structures
![Page 25: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/25.jpg)
25
Copy Structures
In summary 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
How do we make a deep copy?
![Page 26: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/26.jpg)
26
Copy Structures
20 30
Private data: 7000 6000
topPtr 7000SomeStack
20 30
Private data: 5000 2000
topPtr 5000
MyStack
deep copyYes, but how?
![Page 27: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/27.jpg)
27
Copy StructuresDeep Copy
if anotherStack.topPtr is NULL
Set topPtr to NULL
else
Set topPtr to the address of a newly allocated node
Set Info(topPtr) to Info(anotherStack.topPtr)
Set ptr1 to Next(anotherStack.topPtr)
Set ptr2 to topPtr
while ptr1 is not NULL
Set Next(ptr2) to the address of a newly allocated node
Set ptr2 to next(ptr2)
Set Info(ptr2) to Info(ptr1)
Set ptr1 to Next(ptr1)
Set Next(ptr2) to NULL
![Page 28: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/28.jpg)
28
Copy Structures
Relativeposition
ofpointersat the
beginningof eachiteration
![Page 29: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/29.jpg)
29
Copy Structures
Copy constructor
A special member function of a class that is implicitly invoked when passing a parameter by value, initializing a variable in a declaration, and returning an object as the value of function
StackType(const StackType& anotherStack);
Of course, the code should implement a deep copy!
![Page 30: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/30.jpg)
30
Copy Structures
That about the assignment operator? The assignment operator does a shallow copy! We can overload the assignment operator
class StackType
{
public:... void operator=(StackType);
private:... void StackType::operator=(StackType anotherStack)
// code for a deep copy
}
Canwe
overloadother
operators?
![Page 31: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/31.jpg)
31
Copy Structrres
Al relational operators can be overloadedbool operator<(ItemType other) const;
// Returns true if self is less than other
bool operator>(ItemType other) const;
// Returns true if self is greater than other
bool operator==(ItemType other) const;
// Returns true if self is equal to other
...Of course, you must write the code as for
any other function
![Page 32: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/32.jpg)
32
Array-of-Records Implementation
Array instatic
storage
Array indynamicstorage
![Page 33: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/33.jpg)
33
Array-of-Records Implementation
Linked listin
staticstorage
Linked listin
dynamicstorage
![Page 34: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/34.jpg)
34
Array-of-Records Implementation
Sorted list
![Page 35: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/35.jpg)
35
Array-of-Records Implementation
A sorted list
of valuesand
a list offree space
![Page 36: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/36.jpg)
36
Array-of-Records Implementation
Two linkedlists plus freelist
![Page 37: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/37.jpg)
37
Array-of-Records Implementation
One structure
foreachlist
![Page 38: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/38.jpg)
38
Polymorphism
Polymorphism
The ability to determine which of several operations with the same name to apply to a particular object; a combination of static and dynamic binding
Binding
The time at which a name or symbol is bound to the appropriate
static binding: bound at compile time
dynamic binding: bound at run time
![Page 39: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/39.jpg)
39
Polymorphism
NewItemType is derived from ItemType
void PrintResult(ItemType& first, ItemType& second);
How can this be?
![Page 40: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/40.jpg)
40
Polymorphism
C++ relaxes scope rules to allow dynamic binding
The type of the actual parameter may be an object of a derived class of the formal parameter
virtual void PrintResult(ItemType& first, ItemType& second);
Word virtual in base class (ItemType), definition forces dynamic binding
Note: Parameters must be reference
![Page 41: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/41.jpg)
41
Circular Doubly Linked List
What are the advantages of this structure?
![Page 42: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/42.jpg)
42
Circular Doubly Linked List
![Page 43: Chapter 6 Lists Plus. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with](https://reader034.vdocument.in/reader034/viewer/2022051417/56649f585503460f94c7dffc/html5/thumbnails/43.jpg)
43
C++ Tips1. Operators :: . sizeof, and ?: may not be
overloaded
2. At least one operand must be a class instance
3. Precedence, operator symbols, or number of operands cannot be changed
4. Overloading ++ and -- requires prefix form use by client
5. To overload these operators = ( ) [ ], member functions must be used
6. An operator can be given multiple meanings if the data types of operands differ