CS 225 Data Structures September 24 It Iterators Wade Fagen-Ulm - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures September 24 It Iterators Wade Fagen-Ulm - - PowerPoint PPT Presentation

CS 225 Data Structures September 24 It Iterators Wade Fagen-Ulm lmschneid ider CS 225 So Far List ADT Linked Memory Implementation (Linked List) O(1) insert/remove at front/back O(1) insert/remove after a given


slide-1
SLIDE 1

CS 225

Data Structures

September 24 – It Iterators

Wade Fagen-Ulm lmschneid ider

slide-2
SLIDE 2

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…

slide-3
SLIDE 3

Queue ADT

  • [Order]:
  • [Implementation]:
  • [Runtime]:

CS 225 So Far…

slide-4
SLIDE 4

Stack ADT

  • [Order]:
  • [Implementation]:
  • [Runtime]:

CS 225 So Far…

slide-5
SLIDE 5

#pragma once template <typename T> class Queue { public: void enqueue(T e); T dequeue(); bool isEmpty(); private: T *items_; unsigned capacity_; unsigned count_; };

Queue.h

1 2 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?

slide-6
SLIDE 6

#pragma once template <typename T> class Queue { public: void enqueue(T e); T dequeue(); bool isEmpty(); private: T *items_; unsigned capacity_; unsigned count_; };

Queue.h

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

slide-7
SLIDE 7

#pragma once template <typename T> class Queue { public: void enqueue(T e); T dequeue(); bool isEmpty(); private: T *items_; unsigned capacity_; unsigned count_; };

Queue.h

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 m

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

slide-8
SLIDE 8

Im Implications of f Design

class ListNode { public: T & data; ListNode * next; …

1. 2. 3.

class ListNode { public: T * data; … class ListNode { public: T data; …

slide-9
SLIDE 9

Im Implications of f Design

Storage by Reference Storage by Pointer Storage by Value Who manages the lifecycle

  • f 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

slide-10
SLIDE 10

Data Lifecycle

Cube c; myStack.push(c); 1 2 Cube c; myStack.push(&c); 1 2

Storage by reference: Storage by pointer: Storage by value:

Cube c; myStack.push(c); 1 2

slide-11
SLIDE 11

Possible to store NULL?

class ListNode { public: T & data; ListNode * next; ListNode(T & data) : data(data), next(NULL) { } }; T ** arr;

Storage by reference: Storage by pointer:

T * arr;

Storage by value:

slide-12
SLIDE 12

Data Modifications

Cube c(1); myStack.push(c); c.setLength(42); Cube r = myStack.pop(); // What is r's length? 1 2 3 4 5 6 7

slide-13
SLIDE 13

Speed

slide-14
SLIDE 14
slide-15
SLIDE 15

It Iterators

Suppose we want to look through every element in our data structure:

8 2 5

Ø

slide-16
SLIDE 16

Iterators encapsulated access to our data:

8 2 5

Ø

  • Cur. Location
  • Cur. Data

Next

slide-17
SLIDE 17

It Iterators

Every class that implements an iterator has two pieces:

  • 1. [Implementing Class]:
slide-18
SLIDE 18

It Iterators

Every class that implements an iterator has two pieces:

  • 2. [Implementing Class’ Iterator]:
  • Must have the base class std::iterator
  • Must implement
  • perator*
  • perator++
  • perator!=
slide-19
SLIDE 19

#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) { /* nothing */ } }; int main() { Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear"); std::vector<Animal> zoo; zoo.push_back(g); zoo.push_back(p); // std::vector’s insertAtEnd zoo.push_back(b); for ( std::vector<Animal>::iterator it = zoo.begin(); it != zoo.end(); it++ ) { std::cout << (*it).name << " " << (*it).food << std::endl; } return 0; }

stlList.cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

slide-20
SLIDE 20

#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::vector<Animal> zoo; zoo.push_back(g); zoo.push_back(p); // std::vector’s insertAtEnd zoo.push_back(b); for ( const Animal & animal : zoo ) { std::cout << animal.name << " " << animal.food << std::endl; } return 0; }

stlList.cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25