tutorial12

tutorial12 - CSC2100B CSC2100B DataStructures Tutorial12

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CSC 2100B CSC 2100B Data Structures Data Structures Tutorial 12  Tutorial 12  Answers to Sample Questions Answers to Sample Questions 17 th  April 2007
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 CSC2100B Practice Exam CSC2100B Practice Exam Q. 1 Q. 1 Tom correctly implements the ADTs of stacks of integer and  queues of integers. The ADT interface classes he provides are as  follow. // Stack.java   public interface Stack {   public void push(int element);   public int pop();   public int stackDepth();   public boolean isEmpty();  // Queue.java public interface Queue {   public void enqueue(int c);   public int dequeue();   public int queueLength();   public boolean isEmpty();   }  Tom also provides the implementation classes  Stack_imp  and  Queue_imp  respectively.
Background image of page 2
3 Q. 1 Q. 1 (10 marks)  Using Tom’s implementation, write a method public Queue reverseQueue(Queue q) that returns a Queue object that is the reverse of the argument. You should make use ONLY ONE stack The contents of the argument queue should NOT have been  changed.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Q. 1  Q. 1  public Queue reverseQueue(Queue queue) {     Stack stack  = new Stack_imp();      Queue revQueue = new Queue_imp();     char value; if (queue.isEmpty()) return revQueue; while (!queue.isEmpty()) stack.push(queue.dequeue()); while (!stack.isEmpty()) {      value = (char) stack.pop();      revQueue.enqueue(value);       queue.enqueue(value); } while (!queue.isEmpty())  stack.push(queue.dequeue()); while (!stack.isEmpty())  queue.enqueue(stack.pop());   return revQueue; }
Background image of page 4
5 Q. 2 Q. 2 2. Jenny correctly implements the ADT of priority queues of integers and the  ADT of lists of integers. The  priority-dequeue  operation Jenny implements  always returns the largest  integer in the priority queue. Jenny correctly uses a  heap to implement the priority queue. She provides the following two  interfaces public interface  PriorityQueue  {   public void priorityEnqueue(int N);   public int priorityDequeue();   public boolean isEmpty();  public interface  List    public int head();  public List tail();  public boolean isEmpty(); 
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

tutorial12 - CSC2100B CSC2100B DataStructures Tutorial12

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online