CS 126 Lecture P4: An Example Program Outline Introduction - - PowerPoint PPT Presentation

cs 126 lecture p4 an example program outline
SMART_READER_LITE
LIVE PREVIEW

CS 126 Lecture P4: An Example Program Outline Introduction - - PowerPoint PPT Presentation

CS 126 Lecture P4: An Example Program Outline Introduction Program - Data structures - Code Conclusions CS126 5-1 Randy Wang Goals Gain insight of how to put together a large program Learn how to read a large


slide-1
SLIDE 1

CS 126 Lecture P4: An Example Program

slide-2
SLIDE 2

CS126 5-1 Randy Wang

Outline

  • Introduction
  • Program
  • Data structures
  • Code
  • Conclusions
slide-3
SLIDE 3

CS126 5-2 Randy Wang

Goals

  • Gain insight of how to put together a “large” program
  • Learn how to read a “large” program
  • Appreciate the central role played by data structures
  • Master the manipulation of linked lists (pointers)
slide-4
SLIDE 4

CS126 5-3 Randy Wang

Central Role of Data Structures

  • How to choose data structure
  • Ease of programming
  • Time efficient
  • Space efficient
  • Design of algorithms is largely design of data structures
  • Data structures largely determine the algorithms
slide-5
SLIDE 5

CS126 5-4 Randy Wang

Outline

  • Introduction
  • Program
  • Data structures
  • Code
  • Conclusions
slide-6
SLIDE 6

CS126 5-5 Randy Wang

Represent A Single Card

card % 13: face value card / 13: kind

slide-7
SLIDE 7

CS126 5-6 Randy Wang

Represent the Decks

  • Why linked lists?
  • We want you to learn linked lists :)
  • Little need for fast random access of the deck, mostly at the top

and bottom of the stack

link element type declare pointer type declare declare pointer variables dereferencing

slide-8
SLIDE 8
slide-9
SLIDE 9

CS126 5-8 Randy Wang

Outline

  • Introduction
  • Program
  • Data structures
  • Code
  • Conclusions

WAR code main play shuffle deal

slide-10
SLIDE 10

CS126 5-9 Randy Wang

main()

  • Revisiting the concept of top-down design
  • Revisit how to read code
  • All your functions should be this short and readable

(although the lecture notes don’t always practice this) returns a stack of cards

slide-11
SLIDE 11

time start with sorted cards exchange index of the card to exchange with Goal: create a linked list of random cards

slide-12
SLIDE 12

shuffle array build linked list

fill array with sorted cards for each card in the array pick a random card in front of it swap this and the random card start the deck with the first card for each card in the array add this card to the bottom of deck mark the end of the deck

slide-13
SLIDE 13

CS126 5-12 Randy Wang

Demo Part of shuffle()

slide-14
SLIDE 14

CS126 5-13 Randy Wang

Outline

  • Introduction
  • Program
  • Data structures
  • Code
  • Conclusions

WAR code main play shuffle deal

slide-15
SLIDE 15

“move” one card from deck to A pile “move” one card from deck to B pile As long as the deck is not empty move one more from deck to A stop if the deck is empty move one more from deck to B end of piles are marked

slide-16
SLIDE 16

CS126 5-15 Randy Wang

Demo deal()

slide-17
SLIDE 17

Take one card from each of the A, B piles and form a 2-card stack (Ttop, Tbot). Put the 2-card stack at the bottom of the A pile

slide-18
SLIDE 18

CS126 5-17 Randy Wang

Demo play()

slide-19
SLIDE 19

move a number of cards from A pile to T pile peek at top of A pile

slide-20
SLIDE 20
slide-21
SLIDE 21

CS126 5-20 Randy Wang

Outline

  • Introduction
  • Program
  • Data structures
  • Code
  • Conclusions
slide-22
SLIDE 22
slide-23
SLIDE 23
slide-24
SLIDE 24

CS126 5-23 Randy Wang

Stuff We Have Learned in This Lecture

  • The process of constructing a “complex” program in a top-

down fashion

  • Reading a “complex” program to trace its top-down

structure

  • Judicious algorithm design starts with judicious choice of

data structures

  • Good examples of linked list (and pointer) manipulation
  • Draw pictures to read and write pointer codes