![Page 1: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/1.jpg)
CS 106INTRODUCTION TO DATA STRUCTURES
SPRING 2020
PROF. SARA MATHIESONHAVERFORD COLLEGE
![Page 2: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/2.jpg)
ADMIN• Midterm 1 in-class on Thursday
• Create one-page (front & back) “cheat-sheet”
• Office Hours TODAY! 4:30-6pm (H110)
• Remind me to hand back Handout 12 and Lab 2
• We DO have lab this week and attendance is still required. You do NOT need to do anything in advance though, you may begin Lab 4 during lab.
• Lab 4 will be posted Wed or Thurs
![Page 3: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/3.jpg)
• Queues (theory and implementation)
• Review arrays and ArrayLists
• Review Nodes and Linked Lists
• Practice Problems
MAR 3 OUTLINE
![Page 4: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/4.jpg)
• Queues (theory and implementation)
• Review arrays and ArrayLists
• Review Nodes and Linked Lists
• Practice Problems
MAR 3 OUTLINE
![Page 5: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/5.jpg)
QUEUES
How would you want a data structure to work for waiting in line at a store?
What is the rate of input is different than the rate of output?Example: people show up to the DMV at random times, but processing takes about the same time for each person
Define an abstract data type (ADT).
![Page 6: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/6.jpg)
THE QUEUE ADT
Insertions and deletions are First In First Out (FIFO)-Insert at the back-Delete from the front
Operations:• enqueue(Object)• Object dequeue()• Object first()• int size()• boolean isEmpty()
![Page 7: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/7.jpg)
IMPLEMENTING A QUEUEBrainstorm: using the data structures we know about, how could we implement this ADT?
![Page 8: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/8.jpg)
IMPLEMENTING A QUEUEBrainstorm: using the data structures we know about, how could we implement this ADT?
Many ways to implement a Queue! Underneath, we can use:* Arrays* Lists* Stacks
![Page 9: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/9.jpg)
ARRAY-BASED QUEUE IMPLEMENTATIONAn array of size n in a circular fashionTwo ints to track front and size
• f: index of the front element• size: number of stored elements
Q
0 1 2 rf
normal configuration
Q
0 1 2 fr
wrap-around configuration (circular)
![Page 10: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/10.jpg)
EXAMPLE
0 1 2 3 4
Size: 0
Arrow is front
![Page 11: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/11.jpg)
EXAMPLE
30 1 2 3 4
Size: 1
Arrow is front
![Page 12: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/12.jpg)
EXAMPLE
3 40 1 2 3 4
Size: 2
Arrow is front
![Page 13: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/13.jpg)
EXAMPLE
3 4 50 1 2 3 4
Size: 3
Arrow is front
![Page 14: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/14.jpg)
EXAMPLE
3 4 5 20 1 2 3 4
Size: 4
Arrow is front
![Page 15: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/15.jpg)
EXAMPLE
3 4 5 2 10 1 2 3 4
Size: 5
Arrow is front
![Page 16: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/16.jpg)
EXAMPLE
4 5 2 10 1 2 3 4
Size: 4
Arrow is front
![Page 17: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/17.jpg)
EXAMPLE
5 2 10 1 2 3 4
Size: 3
Arrow is front
![Page 18: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/18.jpg)
EXAMPLE
-9 5 2 10 1 2 3 4
Size: 4
Arrow is front
![Page 19: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/19.jpg)
EXAMPLE
-9 2 10 1 2 3 4
Size: 3
Arrow is front
![Page 20: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/20.jpg)
EXAMPLE
-9 10 1 2 3 4
Size: 2
Arrow is front
![Page 21: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/21.jpg)
EXAMPLE
-90 1 2 3 4
Size: 1
Arrow is front
![Page 22: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/22.jpg)
EXAMPLE
0 1 2 3 4
Size: 0
Arrow is front
![Page 23: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/23.jpg)
EXAMPLE
-80 1 2 3 4
Size: 1
Arrow is front
![Page 24: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/24.jpg)
QU
EU
E W
ITH
C
IRC
ULA
R A
RR
AY
![Page 25: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/25.jpg)
QU
EU
E W
ITH
C
IRC
ULA
R A
RR
AYTwo constructors (allow
user to select capacity, or use default.
![Page 26: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/26.jpg)
QU
EU
E W
ITH
C
IRC
ULA
R A
RR
AY
![Page 27: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/27.jpg)
QU
EU
E W
ITH
C
IRC
ULA
R A
RR
AY
![Page 28: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/28.jpg)
QU
EU
E W
ITH
C
IRC
ULA
R A
RR
AY
![Page 29: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/29.jpg)
QU
EU
E W
ITH
C
IRC
ULA
R A
RR
AY
![Page 30: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/30.jpg)
QU
EU
E W
ITH
C
IRC
ULA
R A
RR
AY
![Page 31: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/31.jpg)
QU
EU
E W
ITH
C
IRC
ULA
R A
RR
AY
![Page 32: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/32.jpg)
QU
EU
E W
ITH
C
IRC
ULA
R A
RR
AY
![Page 33: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/33.jpg)
DESIGNING DATA STRUCTURES1. Make a Course object that can store a name and list of
students. Include relevant constructors, getters, and setters.
2. Make a LimitedEnrollmentCourse that has a cap on the number of students who can enroll. Have it inherit from Course.
3. Make addStudent, removeStudent, and getEnrolledmethods that correctly handle limited versus unlimited enrollment.
Extra practice!
![Page 34: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/34.jpg)
![Page 35: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/35.jpg)
• Queues (theory and implementation)
• Review arrays and ArrayLists
• Review Nodes and Linked Lists
• Practice Problems
MAR 3 OUTLINE
![Page 36: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/36.jpg)
ARRAYS• Fixed length
• Pro: all operations O(1)• Con: cannot resize or move around elements easily
• Declare
• Initialize (allocate)
![Page 37: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/37.jpg)
ARRAYS• Fixed length
• Pro: all operations O(1)• Con: cannot resize or move around elements easily
• Declare
• Initialize (allocate)
• “set”:
• “get”:
• length
![Page 38: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/38.jpg)
ARRAYS• Fixed length
• Pro: all operations O(1)• Con: cannot resize or move around elements easily
• Declare
• Initialize (allocate)
• “set”:
• “get”:
• length
Q: what is happening here??
![Page 39: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/39.jpg)
ARRAY LISTS• We allow the size to change, but we don’t copy over elements
every time a new element doesn’t fit
• Use the idea of doubling the size of the array to get an average creation time of O(n) for length n array
• Declare/Initialize
• add:
![Page 40: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/40.jpg)
ARRAY LISTS• We allow the size to change, but we don’t copy over elements
every time a new element doesn’t fit
• Use the idea of doubling the size of the array to get an average creation time of O(n) for length n array
• Declare/Initialize
• add:
• get:
• set:
• size:
![Page 41: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/41.jpg)
• Queues (theory and implementation)
• Review arrays and ArrayLists
• Review Nodes and Linked Lists
• Practice Problems
MAR 3 OUTLINE
![Page 42: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/42.jpg)
NODE OBJECTS
• Doubly linked list
• Singly linked list
Match the constructor to the type of list that would contain such Nodes
![Page 43: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/43.jpg)
NODE OBJECTS
• Doubly linked list
• Singly linked list
Match the constructor to the type of list that would contain such Nodes
![Page 44: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/44.jpg)
LINKED LISTS
• Singly linked list
• Singly linked list with tail pointer
• Doubly linked list
• Doubly linked list with tail pointer
• Doubly linked with sentinels
Match the constructor to the appropriate type(s) of list.
![Page 45: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/45.jpg)
LINKED LISTS
• Singly linked list
• Singly linked list with tail pointer
• Doubly linked list
• Doubly linked list with tail pointer
• Doubly linked with sentinels
Match the constructor to the appropriate type(s) of list.
![Page 46: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/46.jpg)
LINKED LISTS
• Singly linked list
• Singly linked list with tail pointer
• Doubly linked list
• Doubly linked list with tail pointer
• Doubly linked with sentinels
Match the constructor to the appropriate type(s) of list.
![Page 47: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/47.jpg)
LINKED LISTS
• Singly linked list
• Singly linked list with tail pointer
• Doubly linked list
• Doubly linked list with tail pointer
• Doubly linked with sentinels
Match the constructor to the appropriate type(s) of list.
![Page 48: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/48.jpg)
What is the issue with the following code?
What is printed?
![Page 49: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/49.jpg)
What is the issue with the following code?
What is printed?
Only “107”!
![Page 50: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/50.jpg)
![Page 51: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/51.jpg)
• Queues (theory and implementation)
• Review arrays and ArrayLists
• Review Nodes and Linked Lists
• Practice Problems
MAR 3 OUTLINE
![Page 52: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/52.jpg)
WORK WITH A PARTNER!
• Question 1: focus on understanding the code and thinking about what type of loops to use (don’t rewrite the code now)
• Question 2: skip (about Queues)
![Page 53: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/53.jpg)
QUESTION 1
![Page 54: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/54.jpg)
![Page 55: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/55.jpg)
![Page 56: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/56.jpg)
![Page 57: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/57.jpg)
QUESTION 3There are many ways to do this! Two are shown below. What
are the runtimes of these two algorithms?
1)
2)
![Page 58: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/58.jpg)
QUESTION 4a) get & set (only public methods)
b) Could be a LinkedList or an ArrayList
c) Less efficient than an ArrayList, same efficiency as a LinkedList
d) No! the details are abstracted away
Visualization:
Assume that we have stack A with elements 5, 11, -1, 3 (where 3 is on the top). What happens when we call get(2)? Draw the stacks A and B and see what happens.
![Page 59: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &](https://reader033.vdocument.in/reader033/viewer/2022042211/5eb263e41f16424f3f66823e/html5/thumbnails/59.jpg)
QUESTION 5
a) Pseudocode: tail = tail.prevtail.next = null
b) Hints: there are 6 pointers that need to be rearranged (2 for A, 2 for B, and head/tail)
d) See notes from Lecture 11