CS201 - Spring 2014 - Class 21
administrative
- no class on Friday
look at
StackVQueue code
- what will testStack and testQueue print out?
- testStack
daikon
carrot
banana
apple
(LIFO: last in first out)
- testQueue
apple
banana
carrot
daikon
(FIFO: first in first out)
- What types of objects can we pass to testStack?
- Anything that implements the Stack interface (ArrayListStack or LinkedStack)
- Will the answer change depending on what we pass in?
- Not if we've implemented them correctly :)
- They both are stacks, just different ways of representing them
- What types of objects can we pass to testLinear?
- Anything that implements the Linear interface
- All of our stacks and queue implement this interface
- this allows us to pass either a stack OR a queue!
stacks
- Last In First Out (LIFO)
- two basic operations: push and pop
- push adds another item on to the top of the stack
- pop removes the item on the top of the stack
- how can we implement them?
- ArrayList
- Singly linked list
- what are they used for?
- run-time (or call) stack example
- we can write a simple method sum and follow it through the call-stack using the debugger
public static int sum(int n){
String temp = "Num: " + n;
System.out.println(temp);
if( n <= 1 ){
return 1;
}else{
return n + sum(n-1);
}
}
- searching
- parsing (linguistics, code)
- java.util.Stack (
http://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
)
queues
- if you're from the UK, you call a "line" (like waiting in line) a queue
- First In First Out (FIFO)
- two basic operations: enqueue and dequeue
- enqueue adds another item on the the end of the queue
- dequeue removes an item from the front of the queue
- notice that like a stack the only way we manipulate the data is by adding and removing items, the difference is where we add and remove the items
- how can we implement them?
- ArrayList
- LinkedList
- Array?
- what if we wanted to implement it using an array, with the additional knowledge that we know the largest capacity required?
- keep track of where our head is and where our tail is and have the data wrap around at the end of the array
- always add at the tail
- always remove from the head
- is there an easy way to calculate indices?
- use modular arithmetic!
- if we want to increment the head, for example:
- instead of: headIndex++
- headIndex = (headIndex + 1) % data.length
- what are they used for?
- scheduling tasks
- which process should run next
- modeling real world phenomena (lines show up in lots of places)
- searching
search
- basic search framework
- we have places/items
- given an item, we can find out what items are adjacent
- what types of things might we search?
- maps
- web
- social networks (six degrees of Kevin Bacon game)
- look at the basic search method in
Search code
- keep track of two sets of items
- pending: those those things that we know about, but haven't visited yet
- visited: those that we have already visited (why do we need this?)
- the only requirement we have for the items we're traversing is that they specify their neighbors
- look at Searchable interface
- Basic algorithm:
as long as we still have items to visit
get the next item from pending
if we haven't visited it already:
add it to visited
add all of it's neighbors
- why do we have to check in two places if visited contains the item?
- what happens if we use a stack?
- depth first search (DFS)
- we go further and further out, until we reach an ending and only then do we go back and try the next immediate neighbor from our starting point
- what happens if we use a queue?
- breadth first search (BFS)
- we explore all of the immediate neighbors first, before going any further
- can be seen as exploring one level out at a time
- if we're looking for something, then it will find the shortest path