Title: CS1102 Tut 4 Stacks, Queues, Recursion and The Big Oh
1CS1102 Tut 4 Stacks, Queues, Recursion and The
Big Oh
- Max Tan
- tanhuiyi_at_comp.nus.edu.sg
- COM1-01-09 Tel65164364http//www.comp.nus.edu.s
g/tanhuiyi
2First 15 minutes
- Any questions regarding tutorial/lecture
materials?
3First 15 minutes
- No review, we will go through tutorial quickly
and then quickly go through the midterm answers
4Question 1
7
5
6
5
7
5
6
7
5
6
5Question 1 solution
6Question 2
- Two stacks in a single array
- Just grow the stack from both ends of the array!
7Question 2
stack.pushStack1(o) pushStack1(Object o)
arraytop1 o top1
top1
top2
8Question 2
stack.pushStack2(o) pushStack2(Object o)
arraytop2 o top2--
top1
top2
9Question 2
- How to determine when to duplicate the array?
10Question 2
stack.pushStack2(o) pushStack2(Object o)
arraytop2 o top2--
top1
top2
When array is full, top 1 gt top 2!
11Question 2
Duplicate the array and copy the elements to the
end of the array
top1
top2
12Question 3
- Reversing a LinkedList recursively
- An easy way to think of recursion is imagine that
the recursive function already works for
sub-cases. - For example, if you have to use recursion to
solve a factorial function F(n), we can imagine
it already works for F(n-1) and less. - Since it works for F(n-1), to make it work for
F(n), we simply multiply n to F(n-1) for F(n)!
13Question 3
- The idea is that if we can call recursive reverse
for the rest of the list, our task is to make it
work for just one node!
curr
14Question 3
- Set the current node to point to the previous
node, and call recursiveReverse on the remaining
nodes!
curr
recursiveReverse()
15Question 3
- But how does recursiveReverse know what the
previous node is? It is a singly linkedlist, so
you have to pass the reference to the previous
node!
curr
recursiveReverse()
16Question 3
- public void recursiveReverse(ListNodeltIntegergt
curr, - ListNodeltIntegergt prev)
-
- ListNodeltIntegergt next curr.next
- //Update the new next pointer
- curr.next prev
- //Base case
- if(next null)
- this.head curr
- else
- recursiveReverse(next, curr)
-
17Question 4 - Overview
- Modified version of the Stack problem
- Pushing a stack onto another stack
- What happens if you pop and push one by one?
1
2
3
Ordering is reversed!
4
4
3
2
1
18Question 4
- If you noticed, if you pop the elements and push
it into another stack, the ordering is reversed - The most obvious solution is to use a temporary
Stack
19Question 4
20Question 4
- Attempt one
- public E pushRecursive(StackltEgt s)
- E element //base case if(s.empty() return
null else element s.pop() - push(element) pushRecursive(s) retur
n element -
What happens here if we push onto the stack first
before recursive calling pushRecursive? The stack
does not maintain its order!
21Question 4
- Attempt two
- public E pushRecursive(StackltEgt s)
- E element //base case if(s.empty() return
null else element s.pop() - pushRecursive(s)
- push(element) return element
-
What we actually want is to push the LAST element
of the stack first before pushing the FIRST
element so that we can maintain order!
22Question 4
- What about the efficiency?
- Not good because it requires the use of another
stack, and requires the movement of all elements
in the stack - If we view it as a linked list
23Question 4
public E push(StackltEgt s) this.tail.next
s.head tail s.tail
tail2
tail1
4
3
2
Of course we cant do this on a Java LinkedList as
the implementation of head and tail nodes are
hidden from us!
1
head1
head2