Professional Documents
Culture Documents
3 LinkedLists
3 LinkedLists
IMPORTANT PROBLEMS
OF COMPUTER SCIENCE
Search
&
Sort
2
WHAT ARE SOME OF THE IMPORTANT
PROBLEMS?
3
SEARCHING
• Simple version
– Given a set of objects S, and a query object x, is x in
S?
• Complex version
– Given a set of objects S, and a partial information
about an object X, is there a set element which is
similar to X in some sense?
4
CONSIDERATIONS
5
CONSIDERATIONS
6
CONSIDERATIONS
7
CONSIDERATIONS
8
CONSIDERATIONS
• How much time are we willing to wait?
– O(1): Constant time: very good- can’t do any better
– but may not be desirable for other reasons (e.g.,
may require additional memory)
9
CONSIDERATIONS
• How much time are we willing to wait for
insertions and deletions?
– You may want to wait long for insertions and
deletions
– but want the find operations to be fast!
10
CONSIDERATIONS
• How much (additional) memory can we use?
(Additional to the set itself)
– None (not really “none” but constant additional
memory)
– O(N) additional memory (constant additional
memory per item stored)
11
HOW TO COMPARE?
• Equality
– Is X in S?
• Functional relationship
– Is there an element Y such that F(Y) = X?
• Is there a student (Y) with LastName(Y)=“Öztürk”?
• List all students (Ys) with GPA(Y) = 3.00
12
HOW TO COMPARE?
• Inequality
– Is there an element larger than X?
– Is there an element NOT equal to X?
– Is there an element between X and Y?
13
HOW TO COMPARE?
• Closeness
– What is/are the element(s) “closest” to X?
– What elements are at most “distance” y away
from X?
14
HOW TO COMPARE?
• Closeness
– What is/are the element(s) “closest” to X?
• What are the “correct” versions of the misspelled
words
– iktapalrdaik (kitaplardaki)
– evdaki (avdaki, evdeki)
– Çekoslovakyalılaştırabilemediklerimizdenmişsinizcesine
15
HOW TO COMPARE?
• Closeness
– What is/are the element(s) “closest” to X?
• What are the “correct” versions of the misspelled words
– iktapalrdaik (kitaplardaki)
– evdaki (avdaki, evdeki)
– Çekoslovakyalılaştırabilemediklerimizdenmişsinizcesine
– What is the Turkish sentence closest in meaning to
“Ben var şiş kebap yemek”?
• Ben şiş kebap yedim
16
HOW TO COMPARE?
• Closeness
– Find 6 letter words whose first letter is a and whose
last letter is m?
17
WHAT IF S IS INFINITE?
• How can we search infinite sets?
– S is the set of possible sentences in Turkish
– X is a sentence
– Is X a sentence in Turkish?
• Set S is implicitly defined (over a universe of
objects)
– All Y’s with a certain property are in S
– Find if X has that property?
18
WHAT IF COMPARISON IS NOT EASY?
19
WHAT IF COMPARISON IS NOT EASY?
• Whose fingerprint is
this?
20
LESSON
• All life is searching ()
22
ABSTRACT DATA TYPES
• Contrary to common conception,
– writing programs is not really about writing a
sequence of statements,
–but rather it is
about organizing
your data. 23
ABSTRACT DATA TYPES
• Contrary to common conception,
– writing programs is not really about writing a
sequence of statements,
– but rather it is about organizing your data. (Never
forget this!)
26
REPRESENTATION VS TYPE
Type
27
REPRESENTATION VS TYPE
Type
28
REPRESENTATION VS TYPE
29
REPRESENTATION VS TYPE
Type
30
REPRESENTATION VS TYPE
• Type determines what you can do!
– Polynomials of degree n
• Add, Subtract, Divide, Multiply, Evaluate, Differentiate,
Integrate, Find Roots,...
– n-D vectors
• Add, Subtract, Dot Product,...
– n-sample points
• Average, standard deviation ...
31
REPRESENTATION VS TYPE
• A given ADT may be based on different
representations.
• Sets: intersect, union, size, membership...
• Representations (which themselves can be
ADTs)
– Bit Vectors
– Hash Tables
– Linked Lists
– Trees
– ....
32
ADTS VS OBJECTS
• Objects (as in C++) are convenient mechanisms
for implementing ADTs in programming
languages.
35
LINKED LISTS
Tail (of the list)
A1 A2 A3
First Element
(Head) Pointers to the next element in the list
... AN—1 AN
Last Element
Null pointer
36
LINKED LISTS
Deleting an element
A1 A2 A3 A4 A5
A1 A2 A3 A4 A5
37
LINKED LISTS
Deleting an element
A1 A2 A3 A4 A5
A1 A2 A4 A5
A3
38
LINKED LISTS
Deleting an element
A1 A2 A3 A4 A5
A1 A2 A4 A5
A3
39
LINKED LISTS
Inserting an element
A1 A2 A3 A4 A5
Insertion point A6
40
LINKED LISTS
Inserting an element
A1 A2 A3 A4 A5
A6
41
LINKED LISTS
Inserting an element
A1 A2 A3 A4 A5
A6
42
LINKED LISTS
Inserting an element
A1 A2 A6 A3 A4
A5
43
LINKED LISTS
A1 A2 A6 A3
A4
The methods are a bit easier to write.
A5
44
LINKED LISTS
LISTNODE CLASS
A1
template <class Object>
class List; // Incomplete declaration.
Object element;
ListNode *next;
48
friends of a class
• But this is a one way access.
49
LINKED LISTS
LISTNODE CLASS
Object element;
ListNode *next;
ListNode * n = NULL )
: element( theElement ), next( n ) { }
Object element;
ListNode *next;
51
LISTNODE STORING A DATE
class DateListNode
{
...
Date element;
DateListNode *next;
...
};
52
LISTNODE STORING A RECTANGLE
class RectangleListNode
{
...
Rectangle element;
RectangleListNode *next;
...
};
53
LISTNODE STORING A CIRCLE
class CircleListNode
{
...
Circle element;
CircleListNode *next;
...
};
54
CLASS TEMPLATES
template <class Object>
class ListNode A template
{ parameter is a
ListNode( const Object & theElement = Object( ), ListNode * n = NULL ) special kind of
: element( theElement ), next( n ) { }
parameter that
Object element; can be used to
ListNode *next; pass a type as
friend class List<Object>;
argument: just like
friend class ListItr<Object>; regular function
}; parameters can be
ListNode<Date> node; // automatically creates the following class used to pass
class ListNode values to a
{ function
ListNode( const Date & theElement = Date( ), ListNode * n = NULL )
: element( theElement ), next( n ) { } It represents a
Date element;
type that has not
ListNode *next; yet been specified
Object element;
ListNode *next;
class ListNode
{
ListNode( const Rectangle & theElement = Rectangle( ), ListNode * n = NULL )
: element( theElement ), next( n ) { }
Rectangle element;
ListNode *next;
Object element;
ListNode *next;
class ListNode
{
ListNode( const Circle & theElement = Circle( ), ListNode * n = NULL )
: element( theElement ), next( n ) { }
Circle element;
ListNode *next;
private:
ListNode<Object> *current; // Current position
ListItr( ListNode<Object> *theNode )
: current( theNode ) { }
private:
ListNode<Object> *current; // Current position
private:
ListNode<Object> *current; // Current position
private:
ListNode<Object> *current; // Current position
private:
ListNode<Object> *header; 64
};
(Back to) LISTS
template <class Object>
class List
{
public:
List( );
List( const List & rhs );
~List( );
bool isEmpty( ) const;
void makeEmpty( );
ListItr<Object> zeroth( ) const;
ListItr<Object> first( ) const;
void insert( const Object & x, const ListItr<Object> & p );
ListItr<Object> find( const Object & x ) const;
ListItr<Object> findPrevious( const Object & x ) const;
void remove( const Object & x );
private:
ListNode<Object> *header;
}; 65
LISTS
template <class Object>
class List
{
public:
List( );
List( const List & rhs );
~List( );
bool isEmpty( ) const;
void makeEmpty( );
ListItr<Object> zeroth( ) const;
ListItr<Object> first( ) const;
void insert( const Object & x, const ListItr<Object> & p );
ListItr<Object> find( const Object & x ) const;
ListItr<Object> findPrevious( const Object & x ) const;
void remove( const Object & x );
private:
ListNode<Object> *header; 66
};
LISTS
template <class Object>
class List
{
public:
List( );
List( const List & rhs );
~List( );
bool isEmpty( ) const;
void makeEmpty( );
ListItr<Object> zeroth( ) const;
ListItr<Object> first( ) const;
void insert( const Object & x, const ListItr<Object> & p );
ListItr<Object> find( const Object & x ) const;
ListItr<Object> findPrevious( const Object & x ) const;
void remove( const Object & x );
private:
ListNode<Object> *header;
}; 67
LISTS
template <class Object>
class List
{
public:
List( );
List( const List & rhs );
~List( );
bool isEmpty( ) const;
void makeEmpty( );
ListItr<Object> zeroth( ) const;
ListItr<Object> first( ) const;
void insert( const Object & x, const ListItr<Object> & p );
ListItr<Object> find( const Object & x ) const;
ListItr<Object> findPrevious( const Object & x ) const;
void remove( const Object & x );
private:
ListNode<Object> *header;
}; 68
LISTS
template <class Object>
class List
{
public:
List( );
List( const List & rhs );
~List( );
bool isEmpty( ) const;
void makeEmpty( );
ListItr<Object> zeroth( ) const;
ListItr<Object> first( ) const;
void insert( const Object & x, const ListItr<Object> & p );
ListItr<Object> find( const Object & x ) const;
ListItr<Object> findPrevious( const Object & x ) const;
void remove( const Object & x );
private:
ListNode<Object> *header;
}; 69
LISTS
template <class Object>
class List
{
public:
List( );
List( const List & rhs );
~List( );
bool isEmpty( ) const;
void makeEmpty( );
ListItr<Object> zeroth( ) const;
ListItr<Object> first( ) const;
void insert( const Object & x, const ListItr<Object> & p );
ListItr<Object> find( const Object & x ) const;
ListItr<Object> findPrevious( const Object & x ) const;
void remove( const Object & x );
private:
ListNode<Object> *header;
}; 70
LISTS
template <class Object>
class List
{
public:
List( );
List( const List & rhs );
~List( );
bool isEmpty( ) const;
void makeEmpty( );
ListItr<Object> zeroth( ) const;
ListItr<Object> first( ) const;
void insert( const Object & x, const ListItr<Object> & p );
ListItr<Object> find( const Object & x ) const;
ListItr<Object> findPrevious( const Object & x ) const;
void remove( const Object & x );
const List & operator=( const List & rhs );
private:
ListNode<Object> *header;
}; 71
LISTS
template <class Object>
class List
{
public:
List( );
List( const List & rhs );
~List( );
bool isEmpty( ) const;
void makeEmpty( );
ListItr<Object> zeroth( ) const;
ListItr<Object> first( ) const;
void insert( const Object & x, const ListItr<Object> & p );
ListItr<Object> find( const Object & x ) const;
ListItr<Object> findPrevious( const Object & x ) const;
void remove( const Object & x );
/**
* Construct the list.
*/
template <class Object>
List<Object>::List( )
{
header = new ListNode<Object>;
}
73
isEmpty
/**
* Test if the list is logically empty.
* Return true if empty, false, otherwise.
*/
template <class Object>
bool List<Object>::isEmpty( ) const
{
// see if the header points to NULL
return header–>next == NULL;
}
74
insert
/**
* Insert item x after p.
*/
template <class Object>
void List<Object>::insert( const Object & x, const ListItr<Object> & p )
{
if ( p.current != NULL )
p.current->next = new ListNode<Object>( x, p.current->next );
}
A1 A2 A3 A4
X
75
find
/**
* Return iterator corresponding to the first node containing an item x.
* Iterator isPastEnd if item is not found.
*/
template <class Object>
ListItr<Object> List<Object>::find( const Object & x ) const
{
ListNode<Object> *itr = header->next; // Initialize
76
find
/**
* Return iterator corresponding to the first node containing an item x.
* Iterator isPastEnd if item is not found.
*/
template <class Object>
ListItr<Object> List<Object>::find( const Object & x ) const
{
ListNode<Object> *itr = header->next; // Initialize
/**
* Return iterator corresponding to the first node containing an item x.
* Iterator isPastEnd if item is not found.
*/
template <class Object>
ListItr<Object> List<Object>::find( const Object & x ) const
{
ListNode<Object> *itr = header->next; // Initialize
/**
* Return iterator prior to the first node containing an item x. */
79
remove
/**
* Remove the first occurrence of an item x.
*/
template <class Object>
void List<Object>::remove( const Object & x )
{
ListItr<Object> p = findPrevious( x ); // Locate previous of x, if any
if ( p.current->next != NULL )
{
ListNode<Object> *oldNode = p.current->next;
p.current->next = p.current->next->next; // Bypass deleted node
delete oldNode;
}
} p.current node to be deleted
A1 x A3 A4
80
remove
/**
* Remove the first occurrence of an item x.
*/
template <class Object>
void List<Object>::remove( const Object & x )
{
ListItr<Object> p = findPrevious( x ); // Locate previous of x, if any
A1 x A3 A4
81
remove
if ( p.current->next != NULL ) // not null if x is in the list!
{
ListNode<Object> *oldNode = p.current->next;
p.current->next = p.current->next->next;
// Bypass deleted node
delete oldNode;
}
A1 x A3 A4
oldNode
82
remove
if ( p.current->next != NULL ) // not null if x is in the list!
{
ListNode<Object> *oldNode = p.current->next;
p.current->next = p.current->next->next;
// Bypass deleted node
delete oldNode;
}
p.current node to be deleted
A1 x A3 A4
oldNode
83
remove
if ( p.current->next != NULL ) // not null if x is in the list!
{
ListNode<Object> *oldNode = p.current->next;
p.current->next = p.current->next->next;
// Bypass deleted node
delete oldNode;
}
A1 A3 A4
84
zeroth
/**
* Return an iterator representing the header node.
*/
template <class Object>
ListItr<Object> List<Object>::zeroth( ) const
{
return ListItr<Object>( header );
}
85
first
/**
* Return an iterator representing the first node in the list.
* This operation is invalid for empty lists.
*/
template <class Object>
ListItr<Object> List<Object>::first( ) const
{
return ListItr<Object>( header->next );
}
86
makeEmpty
/**
* Make the list logically empty.
*/
template <class Object>
void List<Object>::makeEmpty( )
{
while ( ! isEmpty( ) )
remove( first( ).retrieve( ) );
}
Shallow Copy
X A X is a pointer to an object
Y Y is a pointer
Y = X is a shallow copy
89
Shallow Copy vs Deep Copy
Deep Copy
X A X is a pointer to an object
Y Y is a pointer
90
Shallow Copy
List<Object> A;
List<Object> B;
A = B; // gets automatically converted to A.operator=(B)
// if the = operator is not overloaded, then the result
// will be a shallow copy
B.header
A1 A2 A3 A4
A.header
91
Deep Copy
/**
* Deep copy of linked lists.
*/
template <class Object>
const List<Object> & List<Object>::operator=( const List<Object> & rhs )
{
if ( this != &rhs ) // Make sure objects are different
{
makeEmpty( );
/**
* Deep copy of linked lists.
*/
template <class Object>
const List<Object> & List<Object>::operator=( const List<Object> & rhs )
{
if( this != &rhs ) // Make sure objects are different
{
makeEmpty( ); // Clean up this object
/**
* Deep copy of linked lists.
*/
template <class Object>
const List<Object> & List<Object>::operator=( const List<Object> & rhs )
{
if( this != &rhs ) // Make sure objects are different
{
makeEmpty( ); // Clean up this object
94
Deep Copy
/**
* Deep copy of linked lists.
*/
template <class Object>
const List<Object> & List<Object>::operator=( const List<Object> & rhs )
{
if( this != &rhs ) // Make sure objects are different
{
makeEmpty( ); // Clean up this object
List<Object> B;
A = B; // gets automatically converted to A.operator=(B)
B.header
A1 A2 A3 A4
A1 A2 A3 A4
A.header 96
COPY CONSTRUCTOR
/**
* Copy constructor.
*/
template <class Object>
List<Object>::List( const List<Object> & rhs )
{
header = new ListNode<Object>;
*this = rhs; // Deep Copy
}
97
COPY CONSTRUCTOR
98
Shallow Copy
void foo(List<Object> B) { … }
List<Object> A;
A.header
A1 A2 A3 A4
99
Deep Copy
void foo(List<Object> B) { … }
List<Object> A;
A.header
A1 A2 A3 A4
A1 A2 A3 A4
101
USING LISTS
// Simple print function
template <class Object>
void printList( const List<Object> & theList )
{
if ( theList.isEmpty( ) )
cout << "Empty list" << endl;
else
{
ListItr<Object> itr = theList.first( );
for ( ; ! itr.isPastEnd( ); itr.advance( ) )
cout << itr.retrieve( ) << " ";
}
cout << endl;
}
102
USING LISTS
// Simple print function
template <class Object>
void printList( const List<Object> & theList )
{
if ( theList.isEmpty( ) ) // if the list empty
cout << "Empty list" << endl;
else
{
ListItr<Object> itr = theList.first( );
for ( ; !itr.isPastEnd( ); itr.advance( ) )
cout << itr.retrieve( ) << " ";
}
printList( theList );
105
USING LISTS
for ( i = 0; i < 10; i += 2 )
theList.remove( i );
printList( theList );
List<int> list2;
list2 = theList;
printList( list2 );
return 0;
}
106
USING LISTS
Empty List
0
01
012
0123
01234
012345
0123456
01234567
012345678
0123456789
Find fails for 0
Find succeeds for 1
Find fails for 2
Find succeeds for 3
Find fails for 4
Find succeeds for 5
Find fails for 6
Find succeeds for 7
Find fails for 8
Find succeeds for 9
13579
13579 107
Reading List
Paul J.Deitel and Harvey M.Deitel, “ C++ How to Program, 10th
Edition
” , when you open this site select “Not listed? Click Here”
for the “Select Your Institution” dropdown. Then you may enter
by typing your SU email address. (available as an e-book at
Information Center).
Section 9.11. const (Constant) Objects and const Member
Functions
Section 9.13. friend Functions and friend Classes
Section 9.14. Using the this Pointer
Chapter 10. Operator Overloading; Class string and Array
Objects
Chapter 17. Exception Handling
Chapter 18 and 19. Templates
108
COMPLEXITY OF LIST OPERATIONS
109
VARIATIONS ON LISTS
• Circular linked lists
– Last node points to the first node
– So you can go back by going forward!
– End of list is detected by checking if next field is the
same as header->next
A1 A2 A3 A4
110
VARIATIONS ON LISTS
• Circular linked lists
– Last node points to the first node
– So you can go back by going forward!
– End of list is detected by checking if next field is the
same as header->next
– Not really a major improvement,
– Minor complications in coding the methods.
111
VARIATIONS ON LISTS
• Doubly Linked Lists
– With simple lists we can only go forward!
– Why not go backwards?
Data Field
112
DOUBLY LINKED LIST
113
DOUBLY LINKED LIST
• Need an additional field for left pointer in the
ListNode class
• ListItr class would have to be changed to
include a method for moving left.
• Insert and Remove methods would have to
deal with the left pointer also.
114
LISTS WITH LAST POINTERS
• Sometimes one needs to access the last
element of a list directly (e.g., in queues)
115
USING LISTS:POLYNOMIALS
• Polynomials are mathematical objects of the
sort anxn + an-1 x n-1 + .... + a1x + a0 where ai are
real/integer coefficients and n is an integer.
116
POLYNOMIALS
class Term {
private:
// methods for constructing and destroying go here
int coefficient;
int exponent;
117
POLYNOMIALS
class Polynomial
{
// we assume that the list nodes are ordered by
// decreasing exponent
public:
Polynomial( );
void insertTermAtFront(int coef, int exp);
void zeroPolynomial( );
Polynomial operator+( const Polynomial & rhs ) const;
Polynomial operator*( const Polynomial & rhs ) const;
void print( ostream & out ) const;
private:
List<Term> terms;
}; 118
POLYNOMIAL ADDITION
• Assume we have the two lists
• 10x5+7x3+5x2+2 is represented by
((10,5), (7, 3), (5, 2), (2, 0))
• 11x6+8x4+4x3+4x2+x is represented by
– ((11,6), (8,4), (4,3), (4, 2), (1, 1))
119
POLYNOMIALS
class Polynomial
{
// we assume that the list nodes are ordered by
// decreasing exponent
public:
Polynomial( );
void insertTermAtFront(int coef, int exp);
void zeroPolynomial( );
Polynomial operator+( const Polynomial & rhs ) const;
Polynomial operator*( const Polynomial & rhs ) const;
void print( ostream & out ) const;
private:
List<Term> terms;
};
120
POLYNOMIAL ADDITION
• Assume we have the two lists
– 10x5+7x3+5x2+2
• ((10,5), (7, 3), (5, 2), (2, 0))
– 11x6+8x4+4x3+4x2+x
• ((11,6), (8,4), (4,3), (4, 2), (1, 1))
• The sum is
– 11x6+ 10x5 + 8x4+ 11x3 + 9x2 + x + 2
• ((11,6), (10,5), (8,4), (11,3), (9,2),(1,1),(2,0))
121
POLYNOMIAL ADDITION
• Assume we have the two (ordered) lists
– ((10,5), (7, 3), (5, 2), (2, 0))
– ((11,6), (8,4), (4,3), (4, 2), (1, 1))
• Output List ( )
122
POLYNOMIAL ADDITION
• Assume we have the two (ordered) lists
– ( (10,5), (7, 3), (5, 2), (2, 0))
– ( (11,6), (8,4), (4,3), (4, 2), (1, 1))
• Set Iterators to first elements
• Compare exponents
– If exponents are not equal
• Output the element with the larger exponent to
the output list
• Advance the larger list’s iterator
123
POLYNOMIAL ADDITION
• Assume we have the two (ordered) lists
– ( (10,5), (7, 3), (5, 2), (2, 0))
– ( (11,6), (8,4), (4,3), (4, 2), (1, 1))
• Output = ( (11,6))
• Compare exponents
– If exponents are not equal
• Output the element with the larger exponent to the
output list
• Advance the larger list’s iterator
124
POLYNOMIAL ADDITION
• Assume we have the two (ordered) lists
– ( (10,5), (7, 3), (5, 2), (2, 0))
– ( (11,6), (8,4), (4,3), (4, 2), (1, 1))
• Output = ( (11,6), (10,5) )
• Compare exponents
– If exponents are not equal
• Output the element with the larger exponent to the
output list
• Advance the larger list’s iterator
125
POLYNOMIAL ADDITION
• Assume we have the two (ordered) lists
– ( (10,5), (7, 3), (5, 2), (2, 0))
– ( (11,6), (8,4), (4,3), (4, 2), (1, 1))
• Output = ( (11,6), (10,5), (8,4) )
• Compare exponents
– If exponents are not equal
• Output the element with the larger exponent to the
output list
• Advance the larger list’s iterator
126
POLYNOMIAL ADDITION
• Assume we have the two (ordered) lists
– ( (10,5), (7, 3), (5, 2), (2, 0))
– ( (11,6), (8,4), (4,3), (4, 2), (1, 1))
• Output = ( (11,6), (10,5), (8,4), (11,3) )
• Compare exponents
– If exponents are not equal
• Output the element with the larger exponent to the
output list
• Advance the larger list’s iterator
127
POLYNOMIAL ADDITION
• Assume we have the two (ordered) lists
– ( (10,5), (7, 3), (5, 2), (2, 0))
– ( (11,6), (8,4), (4,3), (4, 2), (1, 1))
• Output = ( (11,6), (10,5), (8,4), (11,3), (9,2) )
• Compare exponents
– If exponents are not equal
• Output the element with the larger exponent to the
output list
• Advance the larger list’s iterator
128
POLYNOMIAL ADDITION
• Assume we have the two (ordered) lists
– ( (10,5), (7, 3), (5, 2), (2, 0))
– ( (11,6), (8,4), (4,3), (4, 2), (1, 1))
• Output = ( (11,6), (10,5), (8,4), (11,3),(9,2))
• Compare exponents
– If exponents are not equal
• Output the element with the larger exponent to the
output list
• Advance the larger list’s iterator
129
POLYNOMIAL ADDITION
• Assume we have the two (ordered) lists
– ( (10,5), (7, 3), (5, 2), (2, 0) )
– ( (11,6), (8,4), (4,3), (4, 2), (1, 1) )
• Output = ( (11,6), (10,5), (8,4), (11,3),(9,2), (1,1))
• Compare exponents
– If one the lists is exhausted
• Add all the remaining elements of the other list to the
output
130
POLYNOMIAL ADDITION
• Assume we have the two (ordered) lists
– ( (10,5), (7, 3), (5, 2), (2, 0) )
– ( (11,6), (8,4), (4,3), (4, 2), (1, 1) )
• Output = ( (11,6), (10,5), (8,4), (11,3),(9,2), (1,1),(2,0))
• We are done
131
POLYNOMIAL ADDITION
Polynomial& Polynomial::operator+(
const Polynomial & Second) const
{
// initialize list iterators
ListItr<Term> X = terms.first();
ListItr<Term> Y = Second.terms.first();
Polynomial output;
132
POLYNOMIAL ADDITION
else if (X.retrieve().Exponent < Y.retrieve().Exponent) {
output.insertTermAtFront(Y.retrieve().Coefficient,
Y.retrieve().Exponent);
Y.advance();
}
else // Exponents are Equal
{
output.insertTermAtFront(
X.retrieve().Coefficient + Y.retrieve().Coefficient,
X.retrieve().Exponent);
X.advance();
Y.advance();
}
} // end of the while loop
133
POLYNOMIAL ADDITION
// copy any remaining terms
while ( ! X.isPastEnd() ) {
output.insertTermAtFront(X.retrieve().Coefficient,
X.retrieve().Exponent);
X.Advance();
}
while ( ! Y.isPastEnd() ) {
output.insertTermAtFront(Y.retrieve().Coefficient,
Y.retrieve().Exponent);
Y.Advance();
}
134
REVERSING A LIST
( 1,2,3,4,5,6,7,8) (8,7,6,5,4,3,2,1)
135
REVERSING A LIST
template <class Object>
void List<Object>::Reverse()
{
ListNode<Object> *current, *previous;
current = NULL;
previous = NULL;
// in place reversal of a list
// uses two additional pointers
ListNode<Object> *current,*previous;
current = NULL;
previous = NULL; current
// in place reversal of a list
// uses two additional pointers
{
ListNode<Object> *current,*previous;
current = NULL;
previous = NULL; current
// in place reversal of a list previous
// uses two additional pointers
{
ListNode<Object> *current,*previous;
current = NULL;
previous = NULL; current
previous
// in place reversal of a list
// uses two additional pointers
ListNode<Object> *current,*previous;
current = NULL;
previous = NULL;
// in place reversal of a list
previous current
// uses two additional pointers
ListNode<Object> *current,*previous;
current = NULL;
previous = NULL;
// in place reversal of a list
// uses two additional pointers previous
current
while (header->next != NULL) {
current = header->next;
header->next =
header->next->next;
current ->next = previous;
previous = current;
}
header->next = previous;
} 150