c340s01t2 - CSc 340 Test 2 SUGGESTED ANSWERS 1. 2. clients...

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

View Full Document Right Arrow Icon
CSc 340 Test 2 Wednesday 21 March 2001 SUGGESTED ANSWERS 1. Write the specifications of an ADT for a Queue of ints. See Fig. 2.16, p. 90 of the text. 2. For the Queue ADT of question 1: a. Write out the implementation of enqueue and dequeue as clients of the IntList ADT. I assume that instances of Queue have a public data member q of type IntList. There are (at least) two ways to write the two functions, depending on where you add and remove the entries. I choose to add to the front and remove from the rear of the IntList. void enqueue(Queue aQueue, int x) aQueue.q=IntList.cons(x,aQueue.q) //Precondition: aQueue is not empty void dequeue(Queue aQueue) aQueue.q=removeLast(aQueue.q) //see definition of removeLast //below //Precondition: list is not empty IntList removeLast(IntList list) IntList shorter if(IntList.rest(list))==IntList.nil) shorter = IntList.nil else shorter = IntList.cons(IntList.first(list), removeLast(IntList.rest(list))) return shorter b. Assume each IntList operation is O(1) and determine how long
Background image of page 1

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

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

This note was uploaded on 02/29/2008 for the course CSC 340 taught by Professor Varies during the Spring '01 term at Lehigh University .

Page1 / 2

c340s01t2 - CSc 340 Test 2 SUGGESTED ANSWERS 1. 2. clients...

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

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