Cfe2 ch13 final
-
Upload
ismailabumuhfouz -
Category
Education
-
view
439 -
download
0
description
Transcript of Cfe2 ch13 final
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Chapter Thirteen: Lists, Stacks, and Queues
Slides by Evan Gallagher
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
• To become familiar with the list, stack, andqueue data types
• To understand the implementation of linked lists• To understand the efficiency of vector and list
operations
Chapter Goals
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Can we hope that the employees will be hiredin order of their last names
so that we will be putting them intothe vector in the order we want?
How else can the data be keptin the order we want it in the vector?
(Insert an employee; sort; insert an employee; sort;
insert an employee; sort;…
No! Too much processing.)
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
For each new employee,we could find the position in the vector
where their data should be insertedand insert it.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
For each new employee,we could find the position in the vector
where their data should be insertedand insert it.
But what about all the other records after this one?
Won’t they need to be MOVED inside the vector?
(yes)
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Linked list
is a data structurethat supports
efficient addition and removalof elements in the middle
of a sequence.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Linked list
No shifting of data is done whenan item in a linked list is
inserted or removed.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Rather than storing the data in a single block of memory,a linked list uses a different strategy.
Each value is stored in its own memory block,together with
the locations of the block “before” it and “after” itin the sequence.
This memory block is traditionally called:
a node.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Each node contains its data
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Each node contains its dataand a pointer to the previous node in the list
.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Each node contains its dataand a pointer to the previous node in the list
.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Each node contains its dataand a pointer to the previous node in the list
and a pointer the next node in the list.
.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Each node contains its dataand a pointer to the previous node in the list
and a pointer the next node in the list.
.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
This is called a doubly-linked list.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
In a singly-linked list each node has a link only to the next node in the list, with no link to the predecessor elements.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To add a node, only the links need to be changed. No shifting required!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To add a node, only the links need to be changed. No shifting required!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To add a node, only the links need to be changed.
No shifting required!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To add a node, only the links need to be changed.
No shifting required!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
And what must happen whenan employee leaves the company?
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To delete a node, only the links need to be changed.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To delete a node, only the links need to be changed.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To delete a node, only the links need to be changed.
No shifting required!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To delete a node, only the links need to be changed.
No shifting required!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
What’s the catch?
It can’t be that easy!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Well, insertion and deletion are that easy.
Where to do the insert or delete is the problem.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Try to delete the 5th element in a linked list.
You would have to first do a
linear search
just to find the 5th element!
This is called Sequential Access.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
In a vector or an array, using the [ ],you can go directly to an element position.
This is called Random Access.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Random doesn’t really mean “random,”it means “arbitrary”–
go directly to any specific itemwithout
having to go throughall of the items before it in the sequence
to get there,
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
The standard C++ library hasan implementation of the
linked list container structure.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Just like vector,the standard list is a template.
You define it by specifying the type it will hold,
list<string> names;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Just like vector,the standard list is a template.
You define it by specifying the type it will hold,and use the push_back method to put items into it:
list<string> names;
names.push_back("Tom");names.push_back("Dick");names.push_back("Harry");
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Recall that you cannot go directlyto an element in the list using [ ].
…names[2]…
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To visit an element, you use a
list iterator:
list<string>::iterator pos;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To have pos mark the beginning position in the names list,you use the begin method of the list :
list<string>::iterator pos;pos = names.begin();
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To move pos to the next position in the names list,you use ++
list<string>::iterator pos;pos = names.begin();pos++;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To move pos backward,you use --
list<string>::iterator pos;pos = names.begin();pos++;pos--;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
To obtain the value that pos indicates,you use *
list<string>::iterator pos;pos = names.begin();pos++;pos--;string value = *pos;// store the value from// the list into value*pos = "Romeo";// The list value at the// position is changed
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Beware the dangers of getting these expressions confused:
* pos pos
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Beware the dangers of getting these expressions confused:
* pos pos
the value in the the iterator that list at pos indicates a position in the list
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Beware the dangers of getting these expressions confused:
*pos = "Romeo";
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Beware the dangers of getting these expressions confused:
*pos = "Romeo";// The list value at the// position is changed
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Beware the dangers of getting these expressions confused:
*pos = "Romeo";// The list value at the// position is changed
pos = names.begin();
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Beware the dangers of getting these expressions confused:
*pos = "Romeo";// The list value at the// position is changed
pos = names.begin();// The position is again at// the beginning of the list
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Beware the dangers of getting these expressions confused:
*pos = "Romeo";// The list value at the// position is changed
pos = names.begin();// The position is again at// the beginning of the list
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
The insert methodhere to insert a new element
and the value for that new element:
names.insert(pos, "Romeo");
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
This convention makes it easyto insert a new element
before the first value of a list:
pos = names.begin();names.insert(pos, "Romeo");
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
There is also an end method for listswhich indicates the position AFTER the last one.
That is exactly where a new last element should go:
pos = names.end(); // Points past the end of the list
names.insert(pos, "Juliet");// Insert past the end of the
list,// A new last element is appended
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
Because end indicatesthe position AFTER the last one,
using that position is an error,just as it is an error to access an element
in an array past the last element.
string value = *names.end();
ERROR!!!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
The begin and end methods are used whenlooping through all the elements in a list.
pos = names.begin();while (pos != names.end()){ cout << *pos << endl; pos++;}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
The begin and end methods are used whenlooping through all the elements in a list.
Or more correctly with a for loop:
pos = names.begin();while (pos != names.end()){ cout << *pos << endl; pos++;}
for (pos = names.begin(); pos != names.end(); pos++){ cout << *pos << endl;}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
But notice something VERY important:
pos = names.begin();while (pos != names.end()){ cout << *pos << endl; pos++;}
for (pos = names.begin(); pos != names.end(); pos++){ cout << *pos << endl;}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
But notice something VERY important:in the test, we are NOT using < or <=, we are using !=
pos = names.begin();while (pos != names.end()){ cout << *pos << endl; pos++;}
for (pos = names.begin(); pos != names.end(); pos++){ cout << *pos << endl;}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
But notice something VERY important:in the test, we are NOT using < or <=, we are using !=
pos = names.begin();while (pos != names.end()){ cout << *pos << endl; pos++;}
for (pos = names.begin(); pos != names.end(); pos++){ cout << *pos << endl;}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
The elements in a linked list are not arranged as in an array,so the less-than relationship cannot be assumed.
pos = names.begin();while (pos != names.end()){ cout << *pos << endl; pos++;}
for (pos = names.begin(); pos != names.end(); pos++){ cout << *pos << endl;}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
The erase method removes an element at a position:
pos = names.begin();pos++;pos = names.erase(pos);
This code removes the second element from the list.
erase returns the position after the one removedso pos now points to what was the third element.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
A short demonstration program:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists#include <string>#include <list>#include <iostream>using namespace std;
int main(){ list<string> names;
names.push_back("Tom"); names.push_back("Dick"); names.push_back("Harry"); names.push_back("Juliet");
// Add a value in fourth place list<string>::iterator pos = names.begin(); pos++; pos++; pos++; names.insert(pos, "Romeo");
ch13/list1.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Using Linked Lists
// Remove the value in second place
pos = names.begin(); pos++;
names.erase(pos);
// Print all values
for (pos = names.begin(); pos != names.end(); pos++) { cout << *pos << endl; }
return 0;}
ch13/list1.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Recall that we said that the values in a listare really stored in separate memory blocks.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
The list class of the STLdefines many useful member functions.
For simplicity, we will only studythe implementation of the most useful ones:
push_back, insert, erase, and the iterator operations.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
A doubly linked list stores each value in a Node.So we will start with its definition:
class Node{public: Node(string s);private: string data; Node* previous; Node* next; friend class List; friend class Iterator;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
A Node object holds a value,and pointers to the previous and next Nodes:
class Node{public: Node(string s);private: string data; Node* previous; Node* next; friend class List; friend class Iterator;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
When a Node is created,its previous and next pointersare set to NULL in the constructor.
class Node{public: Node(string s);private: string data; Node* previous; Node* next; friend class List; friend class Iterator;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
friendship declarations allows the List and Iteratormember functions to inspect and directly modifythe private: data members of the Node class.
class Node{public: Node(string s);private: string data; Node* previous; Node* next;friend class List;friend class Iterator;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
friendship declarations allows the List and Iteratormember functions to inspect and directly modifythe private: data members of the Node class.
class Node{public: Node(string s);private: string data; Node* previous; Node* next;friend class List;friend class Iterator;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
A class should not grant friendship to another class lightly,because it breaks the privacy protection.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
We will call our class List, with an uppercase L,to differentiate it from the standard list class template.
class List{public: List();
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
A List object holds the locationsof the first and last Nodes in the list:
class List{public: List(); void push_back(string data); void insert(Iterator pos, string s); Iterator erase(Iterator pos); Iterator begin(); Iterator end();private: Node* first; Node* last; friend class Iterator;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
It does not store any data – only pointers that point to the first as last Nodes in the list.
The List’s data is in the Nodes!
class List{public: List(); void push_back(string data); void insert(Iterator pos, string s); Iterator erase(Iterator pos); Iterator begin(); Iterator end();private: Node* first; Node* last; friend class Iterator;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
If first and last are NULL,the list is empty.
class List{public: List(); void push_back(string data); void insert(Iterator pos, string s); Iterator erase(Iterator pos); Iterator begin(); Iterator end();private: Node* first; Node* last; friend class Iterator;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Our List object will have, as the STL list does,the methods push_back, insert and eraseas well as the begin() and end() methods
that work with (our) Iterator class.class List{public: List(); void push_back(string data); void insert(Iterator pos, string s); Iterator erase(Iterator pos); Iterator begin(); Iterator end();private: Node* first; Node* last; friend class Iterator;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
And we befriend the Iterator class.
class List{public: List(); void push_back(string data); void insert(Iterator pos, string s); Iterator erase(Iterator pos); Iterator begin(); Iterator end();private: Node* first; Node* last;friend class Iterator;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
And now for the Iterator class.It indicates a position in a List.
class Iterator{public: Iterator();
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
It has a pointer to the “current” Node’s positionand a pointer to the List that created it
(the List that the Node is in).
class Iterator{public: Iterator();
private: Node* position; List* container;
};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
An STL iterator supports the operators:* (get the value at) ++ (next) -- (previous) == (equals)
so we will write methods using those names.
class Iterator{public: Iterator(); string get() const; void next(); void previous(); bool equals(Iterator b) const;private: Node* position; List* container;
};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
And we grant friend status to our List class.
class Iterator{public: Iterator(); string get() const; void next(); void previous(); bool equals(Iterator b) const;private: Node* position; List* container;friend class List;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
If an Iterator points past the end of a List,then the position pointer is NULL.
class Iterator{public: Iterator(); string get() const; void next(); void previous(); bool equals(Iterator b) const;private: Node* position; List* container;friend class List;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
In this situation, if the previous member function is called,the container pointer is used to set the position pointer
to the last element of the List.
class Iterator{public: Iterator(); string get() const; void next(); void previous(); bool equals(Iterator b) const;private: Node* position; List* container;friend class List;};
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Here is the relationship betweenthe List, Node, Iterator classes:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
The List contains items
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
The List contains items stored in Nodes
that are doubly linked.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
The List contains items stored in Nodes
that are doubly linked.Those really are “the list”.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
An Iterator on the Listis shown pointing at
the first Node in “the list”.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Iterators are created by the begin and endmember functions of the List class.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
The begin method creates an Iterator whoseposition pointer points to the first node in the List.The Iterator’s container is set to the correct List,the one that begin should be pointing into – this one.
Iterator List::begin(){ Iterator iter; iter.position = first; iter.container = this; return iter;}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
The end method creates an Iteratorwhose position pointer is NULL.
We choose NULL to mean the past-the-end positionThe Iterator’s container is also set to the correct List.
Iterator List::end(){ Iterator iter; iter.position = NULL; iter.container = this; return iter;}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
The next method is very easy.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Because the Iterator’s position fieldpoints to the Node whose next pointer
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Because the Iterator’s position fieldpoints to the Node whose next pointerpoints to the Node that the Iterator’s
position field should point toafter the next method is finished…
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
set the Iterator’s position:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
set the Iterator’s position:
void Iterator::next() { position = position->next; }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
set the Iterator’s position:
void Iterator::next() { position = position->next; }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
It is an error to dereference a NULL pointer
so
you can evaluate position->nextonly if position is not NULL.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
That makes it illegal to advance an Iteratoronce it is in the past-the-end position (NULL).
Should our implementation check for this error?
The implementation of the standard C++ library doesn’t.
(!)
So we won’t either.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
The previous method is a bit more complex.
In most cases it’s:
position = position->previous;
which is just the “opposite” of what the next method does.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
However, if an Iterator is currentlypast-the-end, then you must make it point to
the last element in the List it is on.
void Iterator::previous(){ if (position == NULL) { position = container->last; } else { position = position->previous; }}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
The get method simply returns the data valueof the Node to which position points.
string Iterator::get() const{ return position->data;}
It is illegal to call get if an Iteratorpoints past-the-end of the List.
And again we follow the STL’s lead and don’t check for this.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
To test if two Iterators (this one and another one)are pointing to the same Node (in the same List)
the equals method compares their two position pointers.
bool Iterator::equals(Iterator b) const{ return position == b.position;}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
When the List is not empty, the push_back methodappends a new Node to the end of the List.
NULL
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
When the List is not empty, the push_back methodappends a new Node to the end of the List.
NULL
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
When the List is not empty, the push_back method appends a new Node to the end of the List.
NULL
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
When the List is not empty, the push_back methodappends a new Node to the end of the List.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
The code:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Node* new_node = new Node(s);
NULL
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Node* new_node = new Node(s);
NULL
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Node* new_node = new Node(s);new_node->previous = last;
NULL
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Node* new_node = new Node(s);new_node->previous = last;
NULLNULL
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Node* new_node = new Node(s);new_node->previous = last;last->next = new_node;
NULLNULL
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Node* new_node = new Node(s);new_node->previous = last;last->next = new_node;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Node* new_node = new Node(s);new_node->previous = last;last->next = new_node;last = new_node;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Node* new_node = new Node(s);new_node->previous = last;last->next = new_node;last = new_node;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
When last is NULL,which can happen only when the List is empty,
after the call to push_back,the Node has a single Node—namely, new_node.
void List::push_back(string data){ Node* new_node = new Node(data); if (last == NULL) // List is empty { first = new_node; last = new_node; } else { new_node->previous = last; last->next = new_node; last = new_node; }}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
When last is NULL,
NULLNULL
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
When last is NULL,
NULLNULL
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Inserting into the middle of a List is a bit more difficult.
void List::insert(Iterator iter, string s);
This will insert a new Node containing s at iter.position.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Get a new Node:
Node* new_node = new Node(s);
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Get a new Node:
Node* new_node = new Node(s);
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Update the pointers in thetwo Nodes surroundingthe new Node.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Update the pointers in thetwo Nodes surroundingthe new Node.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
What happens if you are inserting past-the-end of the list?iter.position would be NULL.
void List::insert(Iterator iter, string s){
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
What happens if you are inserting past-the-end of the list?iter.position would be NULL.Call push_back and you are done.
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Give names to the surrounding nodes.
Let before be the Node before the insertion location,and let after be the Node after that:
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Give names to the surrounding nodes.
Let before be the Node before the insertion location,and let after be the Node after that:
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; } Node* after = iter.position; Node* before = after->previous;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Create the new Node:
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; } Node* after = iter.position; Node* before = after->previous;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Create the new Node:
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; } Node* after = iter.position; Node* before = after->previous; Node* new_node = new Node(s);
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Connect the new Node:
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; } Node* after = iter.position; Node* before = after->previous; Node* new_node = new Node(s);
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Connect the new Node:
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; } Node* after = iter.position; Node* before = after->previous; Node* new_node = new Node(s); new_node->previous = before; new_node->next = after;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Connect the after Node:
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; } Node* after = iter.position; Node* before = after->previous; Node* new_node = new Node(s); new_node->previous = before; new_node->next = after;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Connect the after Node to the new node:
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; } Node* after = iter.position; Node* before = after->previous; Node* new_node = new Node(s); new_node->previous = before; new_node->next = after; after->previous = new_node;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Connecting the before Node depends.
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; } Node* after = iter.position; Node* before = after->previous; Node* new_node = new Node(s); new_node->previous = before; new_node->next = after; after->previous = new_node;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Are we at the start of the List or not?
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; } Node* after = iter.position; Node* before = after->previous; Node* new_node = new Node(s); new_node->previous = before; new_node->next = after; after->previous = new_node;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
At the start, before would be NULLand the new Node would be added at the start,
otherwise, we just adjust the pointer.
. . .if (before == NULL) // Insert at beginning { first = new_node; } else { before->next = new_node; }}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Finally, look at the implementation of the erase method:
Iterator List::erase(Iterator iter);
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
This will delete the Node at iter.position,reset the pointers in the Node s before and after it,and return an Iterator that points to the Node
after the one that was removed.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
This will delete the Node at iter.position,reset the pointers in the Node s before and after it,and return an Iterator that points to the Node
after the one that was removed.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
The code:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Create variables for the Node to be removed,the Node before it, and the Node after it:
Iterator List::erase(Iterator iter){
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Create variables for the Node to be removed,the Node before it, and the Node after it:
Iterator List::erase(Iterator iter){ Node* remove = iter.position; Node* before = remove->previous; Node* after = remove->next;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Update the next and previous pointers of the beforeand after Nodes to bypass the node that is to be removed:
Iterator List::erase(Iterator iter){ Node* remove = iter.position; Node* before = remove->previous; Node* after = remove->next;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Update the next and previous pointers of the beforeand after Nodes to bypass the node that is to be removed:
Iterator List::erase(Iterator iter){ Node* remove = iter.position; Node* before = remove->previous; Node* after = remove->next; before->next = after; after->previous = before;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
But wait!What if we are at either end of the List?
Iterator List::erase(Iterator iter){ Node* remove = iter.position; Node* before = remove->previous; Node* after = remove->next; before->next = after; after->previous = before;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Then either of before and after would NULL?So instead of just assigning, we need to check:
. . . Node* after = remove->next; if (remove == first) { first = after; } else { before->next = after; } if (remove == last) { last = before; } else { after->previous = before; }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
You must adjust the Iterator’s positionso it no longer points to the removed element.
. . . after->previous = before; }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
You must adjust the Iterator‘s positionso it no longer points to the removed element.
. . . after->previous = before; } iter.position = after;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Finally, recycle the removed node:
. . . after->previous = before; } iter.position = after;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Finally, recycle the removed node:
. . . after->previous = before; } iter.position = after; delete remove;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
But wait!Again there’s more.
. . . after->previous = before; } iter.position = after; delete remove;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
But wait!Again there’s more.
. . . after->previous = before; } iter.position = after; delete remove;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
You must return the Iterator that pointsto the Node after the one that was removed:
. . . after->previous = before; } iter.position = after; delete remove;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
You must return the Iterator that pointsto the Node after the one that was removed:
. . . after->previous = before; } iter.position = after; delete remove; Iterator r; r.position = after; r.container = this; return r;}
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
A demo program:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists#include <string>#include <iostream>using namespace std;
class List;class Iterator;
class Node{public: /** Constructs a node with a given data value. @pram s the data to store in this node */ Node(string s);private: string data; Node* previous; Node* next;friend class List;friend class Iterator;};
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
class List{public: /** Constructs an empty list. */ List();
/** Appends an element to the list. @pram data the value to append */ void push_back(string data);
/** Inserts an element into the list. @pram iter the position before which to insert @pram s the value to append */ void insert(Iterator iter, string s);
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
/** Removes an element from the list. pram iter the position to remove @return an iterator pointing to the element after the erased element */ Iterator erase(Iterator iter);
/** Gets the beginning position of the list. @return an iterator pointing to the beginning of the list */ Iterator begin();
/** Gets the past-the-end position of the list. @return an iterator pointing past the end of the list */ Iterator end();
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
private: Node* first; Node* last;friend class Iterator;};
class Iterator{public: /** Constructs an iterator that does not point into any list. */ Iterator();
/** Looks up the value at a position. @return the value of the node to which the iterator points */ string get() const;
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists /** Advances the iterator to the next node. */ void next();
/** Moves the iterator to the previous node. */ void previous();
/** Compares two iterators. @pram b the iterator to compare with this iterator @return true if this iterator and b are equal */ bool equals(Iterator b) const;
private: Node* position; List* container;friend class List;};
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Node::Node(string s){ data = s; previous = NULL; next = NULL;}
List::List(){ first = NULL; last = NULL;}void List::push_back(string data){ Node* new_node = new Node(data); if (last == NULL) // List is empty { first = new_node; last = new_node; }
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
else { new_node->previous = last; last->next = new_node; last = new_node; }}
void List::insert(Iterator iter, string s){ if (iter.position == NULL) { push_back(s); return; }
Node* after = iter.position; Node* before = after->previous; Node* new_node = new Node(s); new_node->previous = before;
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
else { before->next = after; } if (remove == last) { last = before; } else { after->previous = before; } delete remove; Iterator r; reposition = after; r.container = this; return r;}
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
Iterator List::begin(){ Iterator iter; iter.position = first; iter.container = this; return iter;}
Iterator List::end(){ Iterator iter; iter.position = NULL; iter.container = this; return iter;}
Iterator::Iterator(){ position = NULL; container = NULL;}
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
string Iterator::get() const{ return position->data;}
void Iterator::next(){ position = position->next;}
void Iterator::previous(){ if (position == NULL) { position = container->last; } else { position = position->previous; }}
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
bool Iterator::equals(Iterator b) const{ return position == b.position;}
int main(){ List names; names.push_back("Tom"); names.push_back("Dick"); names.push_back("Harry"); names.push_back("Juliet");
// Add a value in fourth place Iterator pos = names.begin(); pos.next(); pos.next(); pos.next(); names.insert(pos, "Romeo");
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Implementing Linked Lists
// Remove the value in second place pos = names.begin(); pos.next(); names.erase(pos); // Print all values for (pos = names.begin(); !pos.equals(names.end()); pos.next()) { cout << pos.get() << endl; }
return 0;}
ch13/list2.cpp
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
How efficient are these operations:
• Getting the kth element
• Adding or removing an element at a given position(an iterator or index)
• Adding or removing an element at the end
for lists, arrays, and vectors?
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
list – getting the kth element
Getting to the kth elementrequires starting at the beginning
and advancing the iterator k times.
If it takes time T to advance the iterator once,advancing the iterator to the kth element takes kT time
so locating the kth element is an O(k) operation.
Advancing an iterator does some checking— this quantity of time is independent of the iterator position.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
array – getting the kth element
Getting to the kth elementrequires only a calculation for [ ]to go directly to the kth element.
A simple calculation is O(1),so locating the kth element is an O(1) operation.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – getting the kth element
Getting to the kth element,just as with an array,
requires only a calculation for [ ]to go directly to the kth element.
The calculation is slightly more involvedbut it is still a simple calculation,
so locating the kth element is an O(1) operation.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
list – inserting and deleting
These operations involve only changing two pointer values.
Clearly O(1) operations.
Note that we will have already done thesearching for where to do the insert or delete
so that time is not considered here.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
array – inserting and deleting
vector – inserting and deleting
Arrays are easy to visualize.
Inserting and deleting for vectorrequires seeing how the
vector class is implemented.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – internal organization
A vector keeps its data in a dynamic buffer (an array)
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – internal organization
A vector keeps its data in a dynamic buffer (an array)and a pointer to that area;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – internal organization
A vector keeps its data in a dynamic buffer (an array)and a pointer to that area;
it keeps the value of the current capacity of the buffer;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – internal organization
A vector keeps its data in a dynamic buffer (an array)and a pointer to that area;
it keeps the value of the current capacity of the buffer;
and it keeps the number of elements currently in the buffer.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
array – inserting and deleting
vector – inserting and deleting
Because the vector’s data is stored in an array,the analysis for both of these is similar:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
array – inserting and deleting
vector – inserting and deleting
In both, to insert an elementat position k, the elementswith higher index valuesmust be moved
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
array – inserting and deleting
vector – inserting and deleting
In both, to insert an elementat position k, the elementswith higher index valuesmust be moved to makeroom for the new element.
And size would beincreased by one.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
array – inserting and deleting
vector – inserting and deleting
To delete an element atposition k, the elements withhigher index values must bemoved to take up the roomthat had been used for thedeleted element value.
And size would bereduced by one.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
array – inserting and deleting
vector – inserting and deleting
For the analysis, we need to knowmany elements are affected in a move.
For simplicity, we will assume that insertions and deletions happen at random locations.
Then, on average, where n is the size of the array or vector,each insertion or deletion moves n L 2 elements
Insert and delete are O(n) operations.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
list – adding or removing an element at the end
If we assume the list has a “maintenance pointer”which points to the last item in the list, then,
same as always, just reset some pointer values:
an O(1) operation.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
array – adding or removing an element at the end
vector – adding or removing an element at the end
The array and vector will have to be considered separately.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
array – adding or removing an element at the end
The array must be large enough to insertat the end or we simply cannot insert.
Inserting and deleting involve [ ],a simple calculation as before,
plus arithmetic on the size.There is no moving of data.
O(1).
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
To insert at the end of a vector,the push_back method is used.
When the capacity is sufficient, this is an O(1) processrequiring only accessing and assigning to a position
already there in the dynamic array (the buffer)and arithmetic on the size.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
When the buffer is filled to its current capacity,
v.push_back(9);
5
5
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
When the buffer is filled to its current capacity,the buffer will be increased in size to
accommodate the call to the push_back method
5
5
v.push_back(9);
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
When the buffer is filled to its current capacity,the buffer will be increased in size to
accommodate the call to the push_back method
5
10
v.push_back(9);
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
When the buffer is filled to its current capacity,the buffer will be increased in size to
accommodate the call to the push_back method
10
6
v.push_back(9);
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
The reallocation does not happen very often.That helps with the efficiency.
But makes the analysis a bit harder.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
The reallocation algorithm effects the analysis also.
Suppose we choose to double the size with each reallocation.
If we start a vector with capacity 10,we must reallocate when the buffer reaches sizes10, 20, 40, 80, 160, 320, 640, 1280, and so on.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
Assume that one insertion without reallocation takes time T1 .
Assume that reallocation of k elements takes time kT2.
What is the cost of 1,280 push_back operations?
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
Of course, we pay 1280 * T1 for the 1280 insertions.
The reallocation cost is:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
Of course, we pay 1280 * T1 for the 1280 insertions.
The reallocation cost is:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
Of course, we pay 1280 * T1 for the 1280 insertions.
The reallocation cost is:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
Of course, we pay 1280 * T1 for the 1280 insertions.
The reallocation cost is:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
Of course, we pay 1280 * T1 for the 1280 insertions.
The reallocation cost is:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
Of course, we pay 1280 * T1 for the 1280 insertions.
The reallocation cost is:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
The total cost is a bit less than for 1280 insertions:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
The cost of n push_back operations is then less than:
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
Because the second factor is a constant,we conclude that n push_back operations
take O(n) time.
But Wait!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
We know that it isn’t quite true that anindividual push_back operation
takes O(1) time becauseoccasionally a push_back is unlucky
and must reallocate the buffer.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
vector – adding or removing an element at the end
But if the cost of that reallocation is distributedover the preceding push_back operations,
then the surcharge for eachof them is still a constant amount.
We say that push_back takes amortized O(1) time,
which is written as O(1)+.
Much better than O(n).
(Thank you accountants of the world!)
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
The Efficiency of List, Array, and Vector Operations
So now, how efficient are these operationsfor lists, arrays, and vectors?
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
stackline
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
stackline queue
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
A stack lets you insert and remove elements
at one end only, traditionally called the top of the stack.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
A stack lets you insert and remove elements
at one end only, traditionally called the top of the stack.
Don’t worry, stacks are very stable, it won’t fall over!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
New items can be added to the top of the stack.
top
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
New items can be added to the top of the stack.
This is called pushingthe item onto the stack.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
.
Another push, please.
top
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
One more push.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
Items are removed from the top of the stack as well.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
Items are removed from the top of the stack as well.
This is called poppingitems from the stack.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
Pop me again, please.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
.
Another pop.
top
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
Pop.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
Pop.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
LI
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
LI
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
LI
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
.
top
FO
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
FO
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
What is this?
Fee, Fi, Fo, Fum?
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
No, this is discipline!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
Well, a discipline!
L I Fo
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
Stack discipline!
LI – last in (push)
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
Stack discipline!
LI – last in (push)will be
Fo – first out (pop)
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
Stack discipline!
LI – last in (push)will be
Fo – first out (pop)
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Stack discipline!
L I Fo Last in, first out.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
.
What would happen
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
.
top
What would happenif we popped,
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
What would happenif we popped,and popped,
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
What would happenif we popped,and popped,and popped,
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
What would happenif we popped,and popped,and popped,
and popped…
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
What would happenif we popped,and popped,and popped,
and popped…
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
What would happenif we popped,and popped,and popped,
and popped…
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
What would happenif we popped,and popped,and popped,
and popped…
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
What would happenif we popped,and popped,and popped,
and popped…
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
What would happenif we popped,and popped,and popped,
and popped…
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
…and kept popping on the last item?
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
top
There’s still a stack – it’s just empty
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The C++ stack implements the concept of a stack.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Include the stack header.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
As with all template classes, choose what type will be stored.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
As with all template classes, choose what type will be stored.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Use the push method to insert into the top of the stack.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Use the push method to insert into the top of the stack.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Use the push method to insert into the top of the stack.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Use the push method to insert into the top of the stack.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The size method returns the number of items in the stack.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The size method returns the number of items in the stack.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The top method returns value at top of the stack
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The top method returns value at top of the stack
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The top method returns value at top of the stack but the value stays in the stack until it is popped.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The top method returns value at top of the stack but the value stays in the stack until it is popped.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
This code puts “Tom”, “Dick” and “Harry” into the stack.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
This code puts “Tom”, “Dick” and “Harry” into the stack.The LIFO discipline caused this output:
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
HarryDickTom
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Notice that the pop method does not return anything.
#include <stack>using namespace std; . . . stack<string> s; s.push("Tom"); s.push("Dick"); s.push("Harry"); while (s.size() > 0) { cout << s.top() << endl; s.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
You should not use pop or top on an empty stack
stack<string> s; // empty stack
cout << s.top() << endl;
s.pop();
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
You should not use pop or top on an empty stack– so you should always know how many items arein the stack.
Use the size method.
stack<string> s; // empty stack
cout << s.top() << endl;
s.pop();
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Queues
Next!
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Queues
A queue lets you
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Queues
A queue lets youadd items to one end of the queue (the back)
the back
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Queues
A queue lets youadd items to one end of the queue (the back)
and remove them from the other end of the queue (the front).
the back
the front
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
the front the back
q.push( )
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
the front the back
q.push( )
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
the front the back
q.push( )
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,
the front the backNext!
queue<Customer> q;
q.front()
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,
the front the backThank you!
q.front()
Thank you!
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
q.pop()
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
q.pop()
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
q.push( )
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the backNext!
queue<Customer> q;
q.front()
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
q.pop()
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
q.pop()
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
Grrr…
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
Grrr…
Grumble…
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is:items enter only at the back (push);
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the backNext!
queue<Customer> q;
q.front()
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
q.pop()
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the backNext!
queue<Customer> q;
q.front()
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The discipline of a queue is items:enter only at the back (push),
cannot be processed (front) until they get to the front,and exit only front the front (pop).
the front the back
q.pop()
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
And queues can be empty.
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Will there be any of that
Fee, Fi, Fo, Fum?
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
F i F o
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
the front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
Fo - first outthe front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
Fo - first outthe front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
Fo - first outthe front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
Fo - first outthe front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
Fo - first outthe front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
Fo - first outthe front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
Fo - first outthe front the back
Finally!
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
No, but there is the queue discipline:
Fi - first in
Fo - first outthe front the back
queue<Customer> q;
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The C++ queue implements the concept of a queue.
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Include the queue header.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
As with all template classes, choose what type will be stored.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
As with all template classes, choose what type will be stored.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Use the push method to insert into the back of the queue.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Use the push method to insert into the back of the queue.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Use the push method to insert into the back of the queue.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Use the push method to insert into the back of the queue.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The size method returns the number of items in the queue.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The size method returns the number of items in the queue.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The front method returns value at front of the queue
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The front method returns value at front of the queue
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The front method returns value at front of the queuebut the value stays in the queue until it is popped.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
The front method returns value at front of the queuebut the value stays in the queue until it is popped.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
Notice that the front method does not return anything.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
As with the stack, the pop and front methodsshould not be used on an empty stack
cout << q.front() << endl; q.pop();
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
This code puts “Tom”, “Dick” and “Harry” into the queue.
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
This code puts “Tom”, “Dick” and “Harry” into the stack.The FIFO discipline caused this output:
#include <queue>using namespace std; . . . queue<string> q; q.push("Tom"); q.push("Dick"); q.push("Harry"); while (q.size() > 0) { cout << q.front() << endl; q.pop(); }
TomDickHarry
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Stacks and Queues
#include <stack> The LIFO discipline of stack caused this output:
#include <queue> The FIFO discipline of queue caused this output:TomDickHarry
HarryDickTom
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
Chapter Summary
Chapter Summary
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved
C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reservedSlides by Evan Gallagher
Chapter Thirteen: Lists, Stacks and Queues