161
Collections, Part Two

Collections, Part Two

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Collections Part Two

Outline for Today

Stacks Pancakes meets parsing

Queues Playing some music

Stack

This car canrsquot leavehellipThis car

canrsquot leavehellip

hellip until these two do

hellip until these two do

Thanks to Nick Troccoli for this example

Thanks to Nick Troccoli for this example

Any new car precedes all the old cars Only this car

can leave

Any new car precedes all the old cars Only this car

can leave

Thanks to Nick Troccoli for this example

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

42

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Outline for Today

Stacks Pancakes meets parsing

Queues Playing some music

Stack

This car canrsquot leavehellipThis car

canrsquot leavehellip

hellip until these two do

hellip until these two do

Thanks to Nick Troccoli for this example

Thanks to Nick Troccoli for this example

Any new car precedes all the old cars Only this car

can leave

Any new car precedes all the old cars Only this car

can leave

Thanks to Nick Troccoli for this example

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

42

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

This car canrsquot leavehellipThis car

canrsquot leavehellip

hellip until these two do

hellip until these two do

Thanks to Nick Troccoli for this example

Thanks to Nick Troccoli for this example

Any new car precedes all the old cars Only this car

can leave

Any new car precedes all the old cars Only this car

can leave

Thanks to Nick Troccoli for this example

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

42

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

This car canrsquot leavehellipThis car

canrsquot leavehellip

hellip until these two do

hellip until these two do

Thanks to Nick Troccoli for this example

Thanks to Nick Troccoli for this example

Any new car precedes all the old cars Only this car

can leave

Any new car precedes all the old cars Only this car

can leave

Thanks to Nick Troccoli for this example

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

42

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Thanks to Nick Troccoli for this example

Any new car precedes all the old cars Only this car

can leave

Any new car precedes all the old cars Only this car

can leave

Thanks to Nick Troccoli for this example

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

42

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Thanks to Nick Troccoli for this example

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

42

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

42

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

42

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

42

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

42

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

137

42

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

271

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

271

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

42

137

0

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Stack

A Stack is a data structure representing a stack of things

Objects can be pushed on top of the stack or popped from the top of the stack

0

42

137 Only the topmost element of a Stack can be accessed

Do you see why we call it the call stack and talk about stack frames

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

An Application Balanced Parentheses

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

[

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

(

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1 ^

Balancing Parentheses

int foo() if (x (y + z[1]) lt 137) x = 1

Balancing Parentheses

( [ ) ]

Balancing Parentheses

( [ ) ]^

Balancing Parentheses

( [ ) ]^

(

Balancing Parentheses

( [ ) ] ^

(

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Balancing Parentheses

( [ ) ] ^

(

[

Oops Wrong type of parenthesis here

Oops Wrong type of parenthesis here

Balancing Parentheses

( (

Balancing Parentheses

( (^

Balancing Parentheses

( (^

(

Balancing Parentheses

( ( ^

(

Balancing Parentheses

( ( ^

(

(

Balancing Parentheses

( (

(

(

Balancing Parentheses

( (

(

(

Oops We never matched this

Oops We never matched this

Balancing Parentheses

)

Balancing Parentheses

)^

Balancing Parentheses

)^

Oops Therersquos nothing on the stack

to match

Oops Therersquos nothing on the stack

to match

Our Algorithm

For each character If itrsquos an open parenthesis or brace push it

onto the stack If itrsquos a close parenthesis or brace

ndash If the stack is empty report an errorndash If the character doesnrsquot pair with the character

on top of the stack report an error

At the end return whether the stack is empty (nothing was left unmatched)

More Stack Applications

Stacks show up all the time in parsing recovering the structure in a piece of text Often used in natural language processing take CS224N for

details Used all the time in compilers ndash take CS143 for details Therersquos a deep theorem that says that many structures

appearing in natural language are perfectly modeled by operations on stacks come talk to me after class if yoursquore curious

Theyrsquore also used as building blocks in larger algorithms for doing things like making sure a cityrsquos road networks are navigable (finding

strongly connected components take CS161 for details) and searching for the best solution to a problem ndash stay tuned

Time-Out for Announcements

Assignment 1

Assignment 1 is due this Friday at the start of class

Have questions Call into the LaIR Ask on EdStem Email your section leader

Discussion Sections

Discussion sections have started You should have received an email with your section time and section leaderrsquos name

Donrsquot have a section You can sign up for any open section by visiting

cs198stanfordedu

logging in via ldquoCS106 Sections Loginrdquo and picking a section of your choice

Get Involved in ResearchApplications for Summer Computer Science Research Internships (CURIS) at Stanford open on Sunday January 24th CURIS is the Computer Science departmentrsquos undergraduate research program Stanford students work with PhD students and a faculty member for the Summer with the goal of learning about and contributing to research Many internships produce an identifiable and publishable research result with students going on to top PhD programs Students who participate in CURIS open up opportunities in top-tier jobs graduate schools and often see their work shared and adopted worldwide

CURIS stipends are $5000-$9000 for 10 weeks The stipend level is set by the University following the guidelines on this page (go down to Dynamic Stipend Overview) httpsundergradstanfordeduopportunitiesresearchinformation-faculty-stafffaculty-department-grant-administration

Apply at httpcurisstanfordedu Applications are due by Sunday February 7 You rank the projects yoursquove applied to research groups rank the students who applied to their projects and CURIS matches students to projects using the Gale-Shapely bipartite matching algorithm

Projects hold office hours (over Zoom) during the application period so you can talk with the faculty and students on the project to introduce yourself and learn more Project listings should specify their office hours if they arenrsquot listed email the contact person

To help with your application the CURIS mentors are holding office hoursTuesdays 1-2pm - Book a slot at httpscalendlycomaabuhashcurisohTuesdays 4-5pm (drop-in) - Zoom Link

There will also be an information session with PhD students and prior CURIS participants which we will announce later this week

For any additional question please email curis-mentorscsstanfordedu

Apply for Internships

The Computer Forum will be holding their Winter Career Fair virtually on Career Fair Plus on January 27 from 1100am ndash 400pm This fair is only for Stanford students

Computer Forum Career Fair

Date Wednesday January 27

Time 1100am ndash 400pm PT

Student registration is now open and you can book your meetings with companies HERE

Here is a helpful article on how to prepare for the career fair

In addition you can read this article on how to book meeting on the web portal

lecturepop()

Queue

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

137 42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

42 271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

271 314

Queue

A Queue is a data structure representing a waiting line

Objects can be enqueued to the back of the line or dequeued from the front of the line

No other objects in the queue are visible Example A checkout counter

314

An Application Looper

Loopers

A looper is a device that records sound or music then plays it back over and over again (in a loop)

These things are way too much fun especially if yoursquore not a very good musician 128515

Letrsquos make a simple looper using a Queue

Building our Looper

Our looper will read data files like the one shown to the left

Each line consists of the name of a sound file to play along with how many milliseconds to play that sound for

Wersquoll store each line using the SoundClip type which is defined in our C++ file

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

G2wav 690G2wav 230Bb2wav 230G2wav 460G2wav 460G2wav 460G2wav 230Bb2wav 230G2wav 230F2wav 460

Building our Looper

Building our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1Clip 2 Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1

Clip 2

Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2Clip 3 Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

119135 119136119134

Building our Looper

Clip 1 Clip 2

Clip 3

Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Building our Looper

Clip 1 Clip 2 Clip 3Clip 4 Clip 5

front

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Enjoying Our Looper

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Feeling musical Want to contribute a loop for the next iteration of CS106B Send me your loop file and wersquoll add it

to our collection

Changing our Looper

Changing our Looper

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

QueueltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = loopdequeue() playSound(toPlayfilename toPlaylength)

loopenqueue(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Make a prediction ndash what are yougoing to hear when we use this

version of the looper

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5 119135 119136119134

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Changing our Looper

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

StackltSoundClipgt loop = loadLoop( hellip ) while (true) SoundClip toPlay = looppop() playSound(toPlayfilename toPlaylength)

looppush(toPlay)

Clip 1

Clip 1Clip 2

Clip 1Clip 3

Clip 4

Clip 5

Your Action Items

Read Chapter 52 and 53 These sections cover more about the Stack and Queue type and theyrsquore great resources to check out

Attend your first section How exciting

Finish Assignment 1 Read the style guide up on the course website for

more information about good programming style Review the Assignment Submission Checklist to

make sure your code is ready to submit

Next Time

Associative Containers Data sets arenrsquot always linear

Maps and Sets Two ways to organize information

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Slide 50
  • Slide 51
  • Slide 52
  • Slide 53
  • Slide 54
  • Slide 55
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Slide 60
  • Slide 61
  • Slide 62
  • Slide 63
  • Slide 64
  • Slide 65
  • Slide 66
  • Slide 67
  • Slide 68
  • Slide 69
  • Slide 70
  • Slide 71
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85
  • Slide 86
  • Slide 87
  • Slide 88
  • Slide 89
  • Slide 90
  • Slide 91
  • Slide 92
  • Slide 93
  • Slide 94
  • Slide 95
  • Slide 96
  • Slide 97
  • Slide 98
  • Slide 99
  • Slide 100
  • Slide 101
  • Slide 102
  • Slide 103
  • Slide 104
  • Slide 105
  • Slide 106
  • Slide 107
  • Slide 108
  • Slide 109
  • Slide 110
  • Slide 111
  • Slide 112
  • Slide 113
  • Slide 114
  • Slide 115
  • Slide 116
  • Slide 117
  • Slide 118
  • Slide 119
  • Slide 120
  • Slide 121
  • Slide 122
  • Slide 123
  • Slide 124
  • Slide 125
  • Slide 126
  • Slide 127
  • Slide 128
  • Slide 129
  • Slide 130
  • Slide 131
  • Slide 132
  • Slide 133
  • Slide 134
  • Slide 135
  • Slide 136
  • Slide 137
  • Slide 138
  • Slide 139
  • Slide 140
  • Slide 141
  • Slide 142
  • Slide 143
  • Slide 144
  • Slide 145
  • Slide 146
  • Slide 147
  • Slide 148
  • Slide 149
  • Slide 150
  • Slide 151
  • Slide 152
  • Slide 153
  • Slide 154
  • Slide 155
  • Slide 156
  • Slide 157
  • Slide 158
  • Slide 159
  • Slide 160
  • Slide 161