Lecture 12:
Stacks
CS 136: Spring 2024
Katie Keith
π£ Announcements
Midterm material through (and including) March 14 lecture and Lab 4
Thurs March 20: Evening midterm
Lab periods are review Q&A sessions
π£ Announcements
Midterm details:
π£ Announcements
Class Poll
Choice 1:
Give us a different instructorβs CS 136 exam as a practice exam, even though the instructor may have covered different subset of material than us and written an exam with a different level of difficulty. Weβd have to sort through this on our own.
Choice 2:
To avoid the confusion, donβt release the practice exam which may not match Katieβs exam.
Midterm study tips
π― Todayβs Learning Objectives
πReadings
Review: ADT vs. Data Structures
Abstract Data Type (ADT) | Data Structure |
Defines a particular set of operations that can be performed on data, without describing how they are implemented. | The ADT implementation in a programming language (in CS 136: Java!) |
Example: A List is a linear ADT in which elements are arranged in a sequential order. Operations: Insertion, Deletion, Access, Traversal etc. | Multiple data structures can implement the same ADT. Example: (1) A list implemented with arrays (ArrayLists) (2) A list implemented with singly-linked lists |
Theoretical. The βwhat.β | Concrete. The βhow.β |
In Java: Interfaces | In Java: Classes that implement interfaces |
Review: LinkedLists
first
A linked list is a recursive data structure that is either empty (null) or a reference to a node.
Node x = Node();
x.data = "to";
x.next = y;
Node y = Node();
y.data = "be";
y.next = z;
first = x;
instance variable of outer class
LinkedList.java
π»
// Returns the data at the given index of the list
public String get(int index) {
if (index < 0 || index >= this.size()){
System.out.println("Index out of range");
return null;
}
Node current = this.first;
for (int i = 0; i < index; i++) {
current = current.data;
}
return current.data;
}
There are two mistakes in the Linked List method below. What are they and how would we fix them?
π‘Think-pair-share
Doubly Linked Lists (Preview Lab 4)
In a doubly linked list, each node stores a reference to both the next and previous nodes.
first
null
null
instance variable of outer class
last
instance variable of outer class
link to the previous node
ADTs for Collections of Objects
Abstract Data Type (ADT) | Data Structures |
List | (1) List implemented with arrays (2) List implemented with singly-linked lists |
Stack (Last-In-First-Out) | (1) Array-based stack (2) Stacks with Singly-Linked List |
Queue (First-In-First-Out) | (1) Array-based queue (2) Queue with Singly-Linked Lists (3) Queue with Doubly-Linked Lists |
Last week
Today!
This week!
Lab 4!
Lists, Stacks and Queues are all ADTs that involve the collection of objects. But they differ in the specification of which object is to be removed or examined next.
Stack
A stack is a linear collection of elements with operations based on a last-in-first-out (LIFO) policy. A push operation adds an element to the stack and a pop operation removes an element.
Stack Interface
public interface Stack<Item>{
// Pushes an item onto the stack
public abstract void push(Item item);
// Remove the most recently added item
public abstract Item pop();
// Returns true if the stack is empty
public abstract boolean isEmpty();
// Returns the number of items in the stack
public abstract int size();
}
ADTs limit operations on data
Stack<Integer> ourStack = new Stack<Integer>();
while(n>0){
ourStack.push(n % 2);
n = n/2;
}
while(!ourStack.isEmpty()){
System.out.print(ourStack.pop());
}
π‘Think-pair-share
β
π― Todayβs Learning Objectives
Example Stack Use: Back button when web browsing
Example Stack Use: Call Stack
A call stack tracks method calls during program execution
Example:
Call stack: LIFO, Last method called is the first to be completed and removed from the stack
Board work
public static int fib(int n) {
if (n == 0|| n == 1) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}
Stack Overflow
In Java, the size of the call stack is limited. Excessive recursion or deeply nested method calls can lead to a StackOverflowError.
Example Stack Use: Call Stack
A call stack tracks method calls during program execution
Example:
Call stack: LIFO, Last method called is the first to be completed and removed from the stack
Exercise for you!
private static int[] fibCache = new int[100];
public static int fib(int n){
if(n == 0|| n == 1){return n;}
if(fibCache[n] > 0){
return fibCache[n];
}
fibCache[n] = fib(n-1) + fib(n-2);
return fibCache[n];
}
0
0
0
0
β¦
fibCache
β
β
π― Todayβs Learning Objectives
StackWithLinkedLists.java
π»
β
β
β
π― Todayβs Learning Objectives