4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO...
Transcript of 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO...
![Page 1: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/1.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
1"
4.3""Stacks,"Queues,"and"Linked"Lists"
Section(4.3(
![Page 2: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/2.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
2"
Data"Types"and"Data"Structures"Data"types:""Set"of"values"and"opera?ons"on"those"values."
• Some"are"built"into"the"Java"language:"int,"double[],"String,"…"
• Most"are"not:""Complex,"Picture,"Stack,"Queue,"ST,"Graph,"…"
"Data"structures:"• Represent"data"or"rela?onships"among"data."• Some"are"built"into"Java"language:"arrays."• Most"are"not:"linked"list,"circular"list,"tree,"sparse"array,"graph,"…"
this"lecture"
this"lecture"
Section(4.3(
![Page 3: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/3.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
3"
Collec?ons"Fundamental"data"types:"• Set"of"opera?ons"(add,"remove,"test"if"empty)"on"generic"data."• Intent"is"clear"when"we"insert."• Which"item"do"we"remove?"
Stack:""[LIFO"="last"in"first"out]"• Remove"the"item"most"recently"added.""• Ex:""Pez,"cafeteria"trays,"Web"surfing."
Queue:""[FIFO"="first"in,"first"out]"• Remove"the"item"least"recently"added."• Ex:""Line"for"help"in"TA"office"hours."
Symbol"table:"• Remove"the"item"with"a"given"key."• Ex:""Phone"book."
Harper’s"Bazaar"Assignment"
this"lecture"
Section(4.3(
![Page 4: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/4.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
4"
Stack"API"
pop"
push"
Section(4.3(
![Page 5: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/5.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
6"
Stack"Client"Example"1:""Reverse"public class Reverse { public static void main(String[] args) { StackOfStrings stack = new StackOfStrings(); while (!StdIn.isEmpty()) { String s = StdIn.readString(); stack.push(s); } while (!stack.isEmpty()) { String s = stack.pop(); StdOut.println(s); } } } % more tiny.txt
it was the best of times % java Reverse < tiny.txt times of best the was it
stack"contents"when"standard"input"is"empty"
Section(4.3(
![Page 6: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/6.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
10"
Linked"Lists"
Section(4.3(
![Page 7: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/7.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
12"
From"the"point"of"view"of"a"par?cular"object:"all"of"these"structures"look"the"same!"
"
Mul?plyglinked"data"structures:""Many"more"possibili?es."
SinglygLinked"Data"Structures"
sequen?al"(this"lecture)"
parentglink"tree" rho" general"case"
circular"
Section(4.3(
![Page 8: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/8.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
13"
Linked"list:"• A"recursive"data"structure."• An"item"plus"a"pointer"to"another"linked"list" "(or"empty"list)."
– Unwind"recursion:""linked"list"is"a"sequence"of"items."
Node"data"type:"• A"reference"to"a"String."• A"reference"to"another"Node."
Linked"Lists"
public class Node { public String item; public Node next; }
Alice Bob Carol
first
item next special"pointer"value"null"terminates"list"
null
![Page 9: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/9.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
14"
Building"a"Linked"List"
Node third = new Node(); third.item = "Carol"; third.next = null; Node second = new Node(); second.item = "Bob"; second.next = third; Node first = new Node(); first.item = "Alice"; first.next = second;
"Carol" null
C0 C1
- -
C2 C3
"Alice" CA
C4 C5
- -
C6 C7
- -
C8 C9
"Bob" C0
CA CB
- -
CC CD
- -
CE CF
Value"addr"
Carol null item next
third
C0 third
main"memory"
Bob
second
CA second
Alice
first
C4 first
![Page 10: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/10.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
16"16"
Stack"Push:""Linked"List"Implementa?on"
Node second = first;
first.item = "of"; first.next = second;
first = new Node();
best the was it
first
of
second
best the was it
first second
best the was it
first
second
best the was it
first
Section(4.3(
![Page 11: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/11.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
17"17"
Stack"Pop:""Linked"List"Implementa?on"
first = first.next;
return item;
best the was it
first
of
best the was it
first
best the was it
first
of
garbagegcollected"
String item = first.item;
"of"
Section(4.3(
![Page 12: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/12.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
18"
Stack:""Linked"List"Implementa?on"public class LinkedStackOfStrings { private Node first = null; private class Node { private String item; private Node next; } public boolean isEmpty() { return first == null; } public void push(String item) { Node second = first; first = new Node(); first.item = item; first.next = second; } public String pop() { String item = first.item; first = first.next; return item; } }
"inner"class""
stack"and"linked"list"contents"arer"4th"push"opera?on"
![Page 13: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/13.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
19"
Linked"List"Stack:""Test"Client"Trace"push"
pop"
Section(4.3(
![Page 14: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/14.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
21"
Stack"Data"Structures:""Tradeoffs"Two"data"structures"to"implement"Stack"data"type."
Array:"• Every"push/pop"opera?on"take"constant"?me."• But…""must"fix"maximum"capacity"of"stack"ahead"of"?me."
"
Linked"list:"• Every"push/pop"opera?on"takes"constant"?me."• Memory"is"propor?onal"to"number"of"items"on"stack."• But…""uses"extra"space"and"?me"to"deal"with"references."
to be or not 0 1 2 3 4 5 6 7 8 9
a[]
N
![Page 15: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/15.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
22"
List"Processing"Challenge"1"What"does"the"following"code"fragment"do?"
for (Node x = first; x != null; x = x.next) { StdOut.println(x.item); }
Alice Bob Carol null item next
first
Section(4.3(
![Page 16: 4.3Stacks,Queues,andLinkedLists - University of …cis110/15sp/lectures/43linkedlists.pdf · LOGO STYLE GUIDE Schools within the University 19 6" Stack"Client"Example"1:""Reverse"](https://reader034.fdocuments.in/reader034/viewer/2022051721/5a91f6547f8b9a8b5d8bca9c/html5/thumbnails/16.jpg)
Schools within the UniversityLOGO STYLE GUIDE
19
23"
List"Processing"Challenge"2"
Node last = new Node(); last.item = StdIn.readString(); last.next = null; Node first = last; while (!StdIn.isEmpty()) { last.next = new Node(); last = last.next; last.item = StdIn.readString(); last.next = null; }
Alice Bob Carol null item next
first last
What"does"the"following"code"fragment"do?"
Section(4.3(