![Page 1: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/1.jpg)
CS 225Data Structures
Feb. 12 – IteratorsWade Fagen-Ulmschneider
![Page 2: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/2.jpg)
List ADT
• Linked Memory Implementation (“Linked List”)• O(1) insert/remove at front/back• O(1) insert/remove after a given element• O(n) lookup by index
•Array Implementation (“Array List”)• O(1) insert/remove at front/back• O(n) insert/remove at any other location• O(1) lookup by index
CS 225 So Far…
![Page 3: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/3.jpg)
Queue ADT
• FIFO: First in, first out – like a line/queue at a shop
• Implemented with a list, O(1) enqueue/dequeue
Stack ADT
• LIFO: Last in, first out – list a stack of papers
• Implemented with a list, O(1) push/pop
CS 225 So Far…
![Page 4: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/4.jpg)
#ifndef QUEUE_H
#define QUEUE_H
template <class QE>
class Queue {
public:
void enqueue(QE e);
QE dequeue();
bool isEmpty();
private:
QE *items_;
unsigned capacity_;
unsigned count_;
};
#endif
Queue.h12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
What type of implementation is this Queue?
How is the data stored on this Queue?
![Page 5: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/5.jpg)
#ifndef QUEUE_H
#define QUEUE_H
template <class QE>
class Queue {
public:
void enqueue(QE e);
QE dequeue();
bool isEmpty();
private:
QE *items_;
unsigned capacity_;
unsigned count_;
};
#endif
Queue.h12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
What type of implementation is this Queue?
How is the data stored on this Queue?
Queue<int> q;q.enqueue(3);q.enqueue(8);q.enqueue(4);q.dequeue();q.enqueue(7);q.dequeue();q.dequeue();q.enqueue(2);q.enqueue(1);q.enqueue(3);q.enqueue(5);q.dequeue();q.enqueue(9);
![Page 6: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/6.jpg)
#ifndef QUEUE_H
#define QUEUE_H
template <class QE>
class Queue {
public:
Queue(); // …etc…
void enqueue(QE e);
QE dequeue();
bool isEmpty();
private:
QE *items_;
unsigned capacity_;
unsigned count_;
unsigned entry_;
unsigned exit_;
};
#endif
Queue.h12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
m o n
Queue<char> q;q.enqueue(m);q.enqueue(o);q.enqueue(n);…q.enqueue(d);q.enqueue(a);q.enqueue(y);q.enqueue(i);q.enqueue(s);q.dequeue();q.enqueue(h);q.enqueue(a);
![Page 7: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/7.jpg)
Implications of Design
struct ListNode {
T & data;
ListNode * next;
ListNode(T & data) : data(data), next(NULL) { }
};
1.
2.
3.
struct ListNode {
T * data;
…
struct ListNode {
T data;
…
![Page 8: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/8.jpg)
Implications of DesignStorage by Reference Storage by Pointer Storage by Value
Who manages the lifecycle of the data?
Is it possible for the data structure to store NULL?
If the data is manipulated by user code while in our data structure, is the change reflected in our data structure?
Speed
![Page 9: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/9.jpg)
Data Lifecycle
Sphere s;
myStack.push(s);
1
2
Sphere s;
myStack.push(&s);
1
2
Storage by reference:
Storage by pointer:
Storage by value:
Sphere s;
myStack.push(s);
1
2
![Page 10: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/10.jpg)
Possible to store NULL?
struct ListNode {
T & data;
ListNode * next;
ListNode(T & data) : data(data), next(NULL) { }
};
T ** arr;
Storage by reference:
Storage by pointer:
T * arr;
Storage by value:
![Page 11: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/11.jpg)
Data Modifications
Sphere s(1);
myStack.push(s);
s.setRadius(42);
Sphere r = myStack.pop();
// What is r’s radius?
1
2
3
4
5
6
7
![Page 12: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/12.jpg)
Speed
![Page 13: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/13.jpg)
![Page 14: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/14.jpg)
IteratorsSuppose we want to look through every element in our data structure:
8 2 5Ø
![Page 15: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/15.jpg)
Iterators encapsulated access to our data:
8 2 5Ø
Cur. Location Cur. Data Next
![Page 16: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/16.jpg)
IteratorsEvery class that implements an iterator has two pieces:
1. [Implementing Class]:
![Page 17: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/17.jpg)
IteratorsEvery class that implements an iterator has two pieces:
2. [Implementing Class’ Iterator]:• Must have the base class std::iterator
• Must implementoperator*operator++operator!=
![Page 18: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/18.jpg)
#include <list>
#include <string>
#include <iostream>
struct Animal {
std::string name, food;
bool big;
Animal(std::string name = "blob", std::string food = "you", bool big = true) :
name(name), food(food), big(big) { /* none */ }
}
int main() {
Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear");
std::list<Animal> zoo;
zoo.push_back(g);
zoo.push_back(p); // std::list’s insertAtEnd
zoo.push_back(b);
for ( std::list<Animal>::iterator it = zoo.begin(); it != zoo.end(); it++ ) {
std::cout << (*it).name << " " << (*it).food << std::endl;
}
return 0;
}
stlList.cpp12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
![Page 19: PowerPoint Presentation...Queue ADT •FIFO: First in, first out –like a line/queue at a shop •Implemented with a list, O(1) enqueue/dequeue Stack ADT •LIFO: Last in, first out](https://reader030.vdocuments.us/reader030/viewer/2022040609/5eca4fae3e03014e8747efd8/html5/thumbnails/19.jpg)
#include <list>
#include <string>
#include <iostream>
struct Animal {
std::string name, food;
bool big;
Animal(std::string name = "blob", std::string food = "you", bool big = true) :
name(name), food(food), big(big) { /* none */ }
}
int main() {
Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear");
std::list<Animal> zoo;
zoo.push_back(g);
zoo.push_back(p); // std::list’s insertAtEnd
zoo.push_back(b);
for ( const Animal & animal : zoo ) {
std::cout << animal.name << " " << animal.food << std::endl;
}
return 0;
}
stlList.cpp12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25