330
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Thirteen: Lists, Stacks, and Queues Slides by Evan Gallagher

Cfe2 ch13 final

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 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

Page 2: Cfe2 ch13 final

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

Page 3: Cfe2 ch13 final

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.)

Page 4: Cfe2 ch13 final

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.

Page 5: Cfe2 ch13 final

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)

Page 6: Cfe2 ch13 final

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.

Page 7: Cfe2 ch13 final

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.

Page 8: Cfe2 ch13 final

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.

Page 9: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Using Linked Lists

Each node contains its data

Page 10: Cfe2 ch13 final

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

.

Page 11: Cfe2 ch13 final

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

.

Page 12: Cfe2 ch13 final

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.

.

Page 13: Cfe2 ch13 final

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.

.

Page 14: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Using Linked Lists

This is called a doubly-linked list.

Page 15: Cfe2 ch13 final

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.

Page 16: Cfe2 ch13 final

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!

Page 17: Cfe2 ch13 final

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!

Page 18: Cfe2 ch13 final

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!

Page 19: Cfe2 ch13 final

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!

Page 20: Cfe2 ch13 final

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?

Page 21: Cfe2 ch13 final

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.

Page 22: Cfe2 ch13 final

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.

Page 23: Cfe2 ch13 final

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!

Page 24: Cfe2 ch13 final

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!

Page 25: Cfe2 ch13 final

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!

Page 26: Cfe2 ch13 final

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.

Page 27: Cfe2 ch13 final

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.

Page 28: Cfe2 ch13 final

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.

Page 29: Cfe2 ch13 final

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,

Page 30: Cfe2 ch13 final

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.

Page 31: Cfe2 ch13 final

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;

Page 32: Cfe2 ch13 final

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");

Page 33: Cfe2 ch13 final

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]…

Page 34: Cfe2 ch13 final

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;

Page 35: Cfe2 ch13 final

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();

Page 36: Cfe2 ch13 final

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++;

Page 37: Cfe2 ch13 final

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--;

Page 38: Cfe2 ch13 final

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

Page 39: Cfe2 ch13 final

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

Page 40: Cfe2 ch13 final

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

Page 41: Cfe2 ch13 final

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";

Page 42: Cfe2 ch13 final

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

Page 43: Cfe2 ch13 final

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();

Page 44: Cfe2 ch13 final

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

Page 45: Cfe2 ch13 final

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

Page 46: Cfe2 ch13 final

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");

Page 47: Cfe2 ch13 final

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");

Page 48: Cfe2 ch13 final

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

Page 49: Cfe2 ch13 final

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!!!

Page 50: Cfe2 ch13 final

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++;}

Page 51: Cfe2 ch13 final

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;}

Page 52: Cfe2 ch13 final

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;}

Page 53: Cfe2 ch13 final

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;}

Page 54: Cfe2 ch13 final

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;}

Page 55: Cfe2 ch13 final

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;}

Page 56: Cfe2 ch13 final

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.

Page 57: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Using Linked Lists

A short demonstration program:

Page 58: Cfe2 ch13 final

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

Page 59: Cfe2 ch13 final

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

Page 60: Cfe2 ch13 final

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.

Page 61: Cfe2 ch13 final

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.

Page 62: Cfe2 ch13 final

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;};

Page 63: Cfe2 ch13 final

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;};

Page 64: Cfe2 ch13 final

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;};

Page 65: Cfe2 ch13 final

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;};

Page 66: Cfe2 ch13 final

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;};

Page 67: Cfe2 ch13 final

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.

Page 68: Cfe2 ch13 final

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();

Page 69: Cfe2 ch13 final

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;};

Page 70: Cfe2 ch13 final

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;};

Page 71: Cfe2 ch13 final

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;};

Page 72: Cfe2 ch13 final

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;};

Page 73: Cfe2 ch13 final

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;};

Page 74: Cfe2 ch13 final

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();

Page 75: Cfe2 ch13 final

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;

};

Page 76: Cfe2 ch13 final

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;

};

Page 77: Cfe2 ch13 final

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;};

Page 78: Cfe2 ch13 final

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;};

Page 79: Cfe2 ch13 final

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;};

Page 80: Cfe2 ch13 final

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:

Page 81: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Implementing Linked Lists

The List contains items

Page 82: Cfe2 ch13 final

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.

Page 83: Cfe2 ch13 final

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”.

Page 84: Cfe2 ch13 final

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”.

Page 85: Cfe2 ch13 final

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.

Page 86: Cfe2 ch13 final

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;}

Page 87: Cfe2 ch13 final

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;}

Page 88: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Implementing Linked Lists

The next method is very easy.

Page 89: Cfe2 ch13 final

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

Page 90: Cfe2 ch13 final

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…

Page 91: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Implementing Linked Lists

set the Iterator’s position:

Page 92: Cfe2 ch13 final

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; }

Page 93: Cfe2 ch13 final

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; }

Page 94: Cfe2 ch13 final

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.

Page 95: Cfe2 ch13 final

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.

Page 96: Cfe2 ch13 final

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.

Page 97: Cfe2 ch13 final

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; }}

Page 98: Cfe2 ch13 final

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.

Page 99: Cfe2 ch13 final

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;}

Page 100: Cfe2 ch13 final

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

Page 101: Cfe2 ch13 final

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

Page 102: Cfe2 ch13 final

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

Page 103: Cfe2 ch13 final

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.

Page 104: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Implementing Linked Lists

The code:

Page 105: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Implementing Linked Lists

Node* new_node = new Node(s);

NULL

Page 106: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Implementing Linked Lists

Node* new_node = new Node(s);

NULL

Page 107: Cfe2 ch13 final

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

Page 108: Cfe2 ch13 final

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

Page 109: Cfe2 ch13 final

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

Page 110: Cfe2 ch13 final

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;

Page 111: Cfe2 ch13 final

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;

Page 112: Cfe2 ch13 final

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;

Page 113: Cfe2 ch13 final

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; }}

Page 114: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Implementing Linked Lists

When last is NULL,

NULLNULL

Page 115: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Implementing Linked Lists

When last is NULL,

NULLNULL

Page 116: Cfe2 ch13 final

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.

Page 117: Cfe2 ch13 final

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);

Page 118: Cfe2 ch13 final

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);

Page 119: Cfe2 ch13 final

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.

Page 120: Cfe2 ch13 final

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.

Page 121: Cfe2 ch13 final

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){

Page 122: Cfe2 ch13 final

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; }

Page 123: Cfe2 ch13 final

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; }

Page 124: Cfe2 ch13 final

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;

Page 125: Cfe2 ch13 final

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;

Page 126: Cfe2 ch13 final

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);

Page 127: Cfe2 ch13 final

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);

Page 128: Cfe2 ch13 final

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;

Page 129: Cfe2 ch13 final

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;

Page 130: Cfe2 ch13 final

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;

Page 131: Cfe2 ch13 final

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;

Page 132: Cfe2 ch13 final

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;

Page 133: Cfe2 ch13 final

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; }}

Page 134: Cfe2 ch13 final

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);

Page 135: Cfe2 ch13 final

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.

Page 136: Cfe2 ch13 final

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.

Page 137: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Implementing Linked Lists

The code:

Page 138: Cfe2 ch13 final

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){

Page 139: Cfe2 ch13 final

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;

Page 140: Cfe2 ch13 final

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;

Page 141: Cfe2 ch13 final

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;

Page 142: Cfe2 ch13 final

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;

Page 143: Cfe2 ch13 final

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; }

Page 144: Cfe2 ch13 final

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; }

Page 145: Cfe2 ch13 final

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;

Page 146: Cfe2 ch13 final

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;

Page 147: Cfe2 ch13 final

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;

Page 148: Cfe2 ch13 final

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;

Page 149: Cfe2 ch13 final

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;

Page 150: Cfe2 ch13 final

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;

Page 151: Cfe2 ch13 final

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;}

Page 152: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Implementing Linked Lists

A demo program:

Page 153: Cfe2 ch13 final

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

Page 154: Cfe2 ch13 final

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

Page 155: Cfe2 ch13 final

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

Page 156: Cfe2 ch13 final

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

Page 157: Cfe2 ch13 final

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

Page 158: Cfe2 ch13 final

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

Page 159: Cfe2 ch13 final

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

Page 160: Cfe2 ch13 final

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

Page 161: Cfe2 ch13 final

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

Page 162: Cfe2 ch13 final

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

Page 163: Cfe2 ch13 final

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

Page 164: Cfe2 ch13 final

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

Page 165: Cfe2 ch13 final

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?

Page 166: Cfe2 ch13 final

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.

Page 167: Cfe2 ch13 final

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.

Page 168: Cfe2 ch13 final

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.

Page 169: Cfe2 ch13 final

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.

Page 170: Cfe2 ch13 final

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.

Page 171: Cfe2 ch13 final

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)

Page 172: Cfe2 ch13 final

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;

Page 173: Cfe2 ch13 final

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;

Page 174: Cfe2 ch13 final

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.

Page 175: Cfe2 ch13 final

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:

Page 176: Cfe2 ch13 final

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

Page 177: Cfe2 ch13 final

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.

Page 178: Cfe2 ch13 final

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.

Page 179: Cfe2 ch13 final

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.

Page 180: Cfe2 ch13 final

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.

Page 181: Cfe2 ch13 final

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.

Page 182: Cfe2 ch13 final

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).

Page 183: Cfe2 ch13 final

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.

Page 184: Cfe2 ch13 final

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

Page 185: Cfe2 ch13 final

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);

Page 186: Cfe2 ch13 final

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);

Page 187: Cfe2 ch13 final

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);

Page 188: Cfe2 ch13 final

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.

Page 189: Cfe2 ch13 final

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.

Page 190: Cfe2 ch13 final

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?

Page 191: Cfe2 ch13 final

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:

Page 192: Cfe2 ch13 final

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:

Page 193: Cfe2 ch13 final

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:

Page 194: Cfe2 ch13 final

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:

Page 195: Cfe2 ch13 final

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:

Page 196: Cfe2 ch13 final

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:

Page 197: Cfe2 ch13 final

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:

Page 198: Cfe2 ch13 final

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:

Page 199: Cfe2 ch13 final

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!

Page 200: Cfe2 ch13 final

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.

Page 201: Cfe2 ch13 final

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!)

Page 202: Cfe2 ch13 final

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?

Page 203: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

stackline

Page 204: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

stackline queue

Page 205: Cfe2 ch13 final

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.

Page 206: Cfe2 ch13 final

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!

Page 207: Cfe2 ch13 final

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

Page 208: Cfe2 ch13 final

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.

Page 209: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

.

Another push, please.

top

Page 210: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

top

One more push.

Page 211: Cfe2 ch13 final

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.

Page 212: Cfe2 ch13 final

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.

Page 213: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

top

Pop me again, please.

Page 214: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

.

Another pop.

top

Page 215: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

top

Pop.

Page 216: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

top

Pop.

Page 217: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

top

LI

Page 218: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

top

LI

Page 219: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

top

LI

Page 220: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

.

top

FO

Page 221: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

top

FO

Page 222: Cfe2 ch13 final

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?

Page 223: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

top

No, this is discipline!

Page 224: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

top

Well, a discipline!

L I Fo

Page 225: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

top

Stack discipline!

LI – last in (push)

Page 226: Cfe2 ch13 final

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)

Page 227: Cfe2 ch13 final

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)

Page 228: Cfe2 ch13 final

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.

Page 229: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

.

What would happen

Page 230: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

.

top

What would happenif we popped,

Page 231: Cfe2 ch13 final

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,

Page 232: Cfe2 ch13 final

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,

Page 233: Cfe2 ch13 final

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…

Page 234: Cfe2 ch13 final

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…

Page 235: Cfe2 ch13 final

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…

Page 236: Cfe2 ch13 final

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…

Page 237: Cfe2 ch13 final

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…

Page 238: Cfe2 ch13 final

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…

Page 239: Cfe2 ch13 final

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?

Page 240: Cfe2 ch13 final

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

Page 241: Cfe2 ch13 final

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.

Page 242: Cfe2 ch13 final

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(); }

Page 243: Cfe2 ch13 final

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(); }

Page 244: Cfe2 ch13 final

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(); }

Page 245: Cfe2 ch13 final

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(); }

Page 246: Cfe2 ch13 final

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(); }

Page 247: Cfe2 ch13 final

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(); }

Page 248: Cfe2 ch13 final

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(); }

Page 249: Cfe2 ch13 final

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(); }

Page 250: Cfe2 ch13 final

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(); }

Page 251: Cfe2 ch13 final

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(); }

Page 252: Cfe2 ch13 final

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(); }

Page 253: Cfe2 ch13 final

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(); }

Page 254: Cfe2 ch13 final

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(); }

Page 255: Cfe2 ch13 final

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(); }

Page 256: Cfe2 ch13 final

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

Page 257: Cfe2 ch13 final

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(); }

Page 258: Cfe2 ch13 final

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();

Page 259: Cfe2 ch13 final

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();

Page 260: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

Queues

Next!

Page 261: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Stacks and Queues

Queues

A queue lets you

Page 262: Cfe2 ch13 final

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

Page 263: Cfe2 ch13 final

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

Page 264: Cfe2 ch13 final

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;

Page 265: Cfe2 ch13 final

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;

Page 266: Cfe2 ch13 final

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;

Page 267: Cfe2 ch13 final

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;

Page 268: Cfe2 ch13 final

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;

Page 269: Cfe2 ch13 final

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;

Page 270: Cfe2 ch13 final

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;

Page 271: Cfe2 ch13 final

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;

Page 272: Cfe2 ch13 final

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;

Page 273: Cfe2 ch13 final

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;

Page 274: Cfe2 ch13 final

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()

Page 275: Cfe2 ch13 final

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;

Page 276: Cfe2 ch13 final

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;

Page 277: Cfe2 ch13 final

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;

Page 278: Cfe2 ch13 final

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;

Page 279: Cfe2 ch13 final

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;

Page 280: Cfe2 ch13 final

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()

Page 281: Cfe2 ch13 final

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;

Page 282: Cfe2 ch13 final

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;

Page 283: Cfe2 ch13 final

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;

Page 284: Cfe2 ch13 final

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;

Page 285: Cfe2 ch13 final

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;

Page 286: Cfe2 ch13 final

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;

Page 287: Cfe2 ch13 final

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;

Page 288: Cfe2 ch13 final

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;

Page 289: Cfe2 ch13 final

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;

Page 290: Cfe2 ch13 final

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()

Page 291: Cfe2 ch13 final

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;

Page 292: Cfe2 ch13 final

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()

Page 293: Cfe2 ch13 final

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;

Page 294: Cfe2 ch13 final

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;

Page 295: Cfe2 ch13 final

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;

Page 296: Cfe2 ch13 final

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;

Page 297: Cfe2 ch13 final

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;

Page 298: Cfe2 ch13 final

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;

Page 299: Cfe2 ch13 final

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;

Page 300: Cfe2 ch13 final

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;

Page 301: Cfe2 ch13 final

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;

Page 302: Cfe2 ch13 final

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;

Page 303: Cfe2 ch13 final

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;

Page 304: Cfe2 ch13 final

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;

Page 305: Cfe2 ch13 final

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;

Page 306: Cfe2 ch13 final

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;

Page 307: Cfe2 ch13 final

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;

Page 308: Cfe2 ch13 final

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;

Page 309: Cfe2 ch13 final

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.

Page 310: Cfe2 ch13 final

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(); }

Page 311: Cfe2 ch13 final

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(); }

Page 312: Cfe2 ch13 final

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(); }

Page 313: Cfe2 ch13 final

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(); }

Page 314: Cfe2 ch13 final

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(); }

Page 315: Cfe2 ch13 final

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(); }

Page 316: Cfe2 ch13 final

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(); }

Page 317: Cfe2 ch13 final

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(); }

Page 318: Cfe2 ch13 final

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(); }

Page 319: Cfe2 ch13 final

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(); }

Page 320: Cfe2 ch13 final

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(); }

Page 321: Cfe2 ch13 final

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(); }

Page 322: Cfe2 ch13 final

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(); }

Page 323: Cfe2 ch13 final

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(); }

Page 324: Cfe2 ch13 final

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();

Page 325: Cfe2 ch13 final

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(); }

Page 326: Cfe2 ch13 final

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

Page 327: Cfe2 ch13 final

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

Page 328: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Chapter Summary

Page 329: Cfe2 ch13 final

Chapter Summary

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 330: Cfe2 ch13 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reservedSlides by Evan Gallagher

Chapter Thirteen: Lists, Stacks and Queues