CS 225 Data Structures Se Sept. 20 20 Ar Array Li Lists - St - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Se Sept. 20 20 Ar Array Li Lists - St - - PowerPoint PPT Presentation

CS 225 Data Structures Se Sept. 20 20 Ar Array Li Lists - St Stac acks and and Que ueue ues G G Carl Evans List.h 1 #pragma once 2 3 template <typename T> 4 class List { 5 public: /* ... */ 28 private: 29


slide-1
SLIDE 1

CS 225

Data Structures

Se

  • Sept. 20

20 – Ar Array Li Lists - St Stac acks and and Que ueue ues

G G Carl Evans

slide-2
SLIDE 2

#pragma once template <typename T> class List { public: /* ... */ private: };

List.h

1 2 3 4 5 … 28 29 30 31 32 33

slide-3
SLIDE 3

Ar Array Implementation

C S 2 2 5 [0] [1] [2] [3] [4]

slide-4
SLIDE 4

Ar Array Implementation

C S 2 2 5 [0] [1] [2] [3] [4]

insertAtFront:

slide-5
SLIDE 5

Resize Strategy gy: +2 elements every time

slide-6
SLIDE 6

Resize Strategy gy: +2 elements every time

slide-7
SLIDE 7

Resize Strategy gy: x2 elements every time

slide-8
SLIDE 8

Resize Strategy gy: x2 elements every time

slide-9
SLIDE 9

Ar Array Implementation

Singly Linked List Array Insert/Remove at front Insert at given element Remove at given element Insert at arbitrary location Remove at arbitrary location

slide-10
SLIDE 10

st std::v ::vect ctor

slide-11
SLIDE 11
slide-12
SLIDE 12
slide-13
SLIDE 13

St Stack A ADT

slide-14
SLIDE 14

Qu Queue ADT

slide-15
SLIDE 15

#pragma once #include <vector> template <typename T> class Stack { public: void push(T & t); T & pop(); bool isEmpty(); private: std::vector<T> list_; }; #include "Stack.hpp"

Stack.h

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

slide-16
SLIDE 16

St Stack I Imp mpleme mentation

  • n

template <typename T> void Stack<T>::push(const T & t) { list_.push_back(t); } template <typename T> const T & Stack<T>::pop() { const T & data = list_.back(); list_.pop_back(); return data; } 3 4 5 6 7 8 9 10 11 12 13

slide-17
SLIDE 17

Implications of Design gn

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

1. 2. 3.

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

slide-18
SLIDE 18

Implications of Design gn

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? Is it possible to store literals? Speed

slide-19
SLIDE 19

Da Data L a Lifecy cycl cle

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

slide-20
SLIDE 20

Po 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-21
SLIDE 21

Da Data M a 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

slide-22
SLIDE 22

Sp Speed