CSC 311: Data Structures (Fall 2007)
Sample solution to Assignment 2: Chapters 5 and 6
Solution:
Algorithm:
cleanStack(Stack
s)
Input:
a stack s
Output:
Empty stack s
Procedure:
If s.isEmpty() return s;
s.pop();
cleanStack(s);
Solution:
Use
two queues Q1 and Q2, where Q1 stores elements and Q2 is used for auxiliary
boolean isEmpty() {
Q1.isEmpty()};
int size() { Q1.size()};
void push(Object o) { Q1.enqueue(o)
};
Object
pop() {
While (!Q1.isEmpty())
Q2.enqueue(
Q1.dequeue()) ;
Object o =
Q2.dequeue();
While (!Q2.isEmpty())
Q1.enqueue( Q2.dequeue() );
Return o;
}
Object
top(): similar to pop()
isEmpty, size, push: O(1)
pop, top: O(n)
To
implement the Stack ADT using two queues, Q1 and Q2, we can simply enqueue elements into Q1 whenever a push call is made. This
takes O(1) time to complete. For pop calls, we can dequeue all elements of Q1 and enqueue
them into Q2 except for the last element which we set aside in a temp variable.
We then return the elements to Q1 by dequeuing from Q2
and enqueuing into Q1. The last element that we set
aside earlier is then returned as the result of the pop. Thus, performing a pop
takes O(n) time.
C-5.11
Alice has two queues, S and T, which can store integers. Bob gives
Solution:
The
chance that Bob picks S is 0.5, where
The
chance that Bob pocks T is also 0.5, where
This gives her 0.5*(1+49/99) = 74/99 chance of winning.
Solution: Assume that
header and trailer point to the first and the last node.
Algorithm:
reverseDLL(DoublyLinkedList dll)
Input:
A doubly linked list dll
Output:
reversed dll
Procedure:
DoublyLinkedNode
pre=dll.header, cur=pre.next;
nxt=cur.next;
While (cur!=trailer)
{
cur.previous=nxt;
cur.next=pre;
pre=cur;
cur=nxt;
nxt=nxt.next;
}
swap(dll.header, dll.trailer);
Solution:
(i)
For the first
n push operations,
a.
total number
of pushes is n
b.
at the 21,
22, 2lognth pushes, need array copies, total copies: 20+
21+ 22+ ...+ 2logn =2logn+1 -1 = 2n +
1
(ii)
For the
second n pop operations,
a.
Total number
of pops is n
b. The array copies
When the size is n/4, need copies of n/4,
and the size is changed to n/2 --> n/22
When the size is n/2/4, need copies of
n/2/4, and the size is changed to n/4 --> n/23
When the size is n/4/4, need copies of n/4/4, and the size is changed to n/4/2 --> n/24
...
this will repeat at most logn
times, so the total copies is n/22+n/23+…+n/2long<n
Total
number of operations is at most n+2n+1+n+n=5n+1, that is O(n).
C-6.14 Describe how to implement an iterator for a circularly linked list. Since hasNext() will always return true in this case, describe how to perform hasNewNext(), which returns true if and only if the next node in the list has not previously had its element returned by its iterator.
Solution:
Implement an iterator
for a circularly linked list
o
Keep a current pointer
o
hasNext():
return true if current.next!=null
o
next():
ret = current; current=current.next; return ret
Implement an iterator
for a circularly linked list
o
Keep a pointer (say first)
point to the current node (whichever) when the iterator
is constructed
o
Initialize a pointer (say current)
point to the node pointed to by first
o
hasNext():
return true if current.next!=first, false
otherwise
o
next():
ret = current; current=current.next; return ret