



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A handout for problem set 6 in cs230 data structures course at wellesley college. The purpose of the assignment is to give students practice with implementing collections via linear structures, specifically priority queues as arrays and circular queues. Instructions for each problem, including the goal, submission requirements, and methods to be implemented. Students are required to submit their final versions of maxpqarray.java and minpqarray.java for problem 1, and their final version of queuecircular.java for problem 2, along with testing transcripts. The document also includes hints and suggestions for testing the implementation.
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




CS230 Data Structures Handout # 18 Prof. Lyn Turbak March 19, 2004 Wellesley College
Overview: The purpose of this assignment is to give you practice with implementing collections via linear structures.
Working Together: If you worked with a partner on a previous problem set and want to work with a partner on this assignment, you are encourage to choose a different partner. However, you may also work with someone you worked with in the first half of the semester.
Submission: Each team should turn in a single hardcopy submission packet for all problems by slipping it under Lyn’s office door by 6pm on the due date. The packet should include:
Each team should also submit a single softcopy (consisting of your final ps5 directory) to the drop directory ~cs230/drop/ps5/username, where username is the username of one of the team members (indicate which drop folder you used on your hardcopy header sheet). To do this, execute the following commands in Linux in the account of the team member being used to store the code.
cd /students/username/cs cp -R ps5 ~cs230/drop/ps5/username/
Problem 1 [55]: Representing Priority Queues as Arrays We saw in class that arrays can be used to efficiently implement arbitrary sized collections as long as a size instance variable is used to hold the number of collection elements in the array stored in the elts instance variable. Slots with indices 0 through elts.size - 1 hold the elements of the collection, while slots size through elts.length hold “garbage elements” that can safely be ignored. When size = elts.length, any attempt to add a new element to the collection must increase the size of the array. As discussed in class, it’s a good idea to double the size of the array in these cases to make such events rare. In this problem, you will be implementing priority queues as arrays. This is very similar to the exercise you had in lab of implementing priority queues in terms of vectors. Indeed, vectors themselves are usually implemented as arrays in the manner described above.
a. [45]: MaxPQArray In this problem, you are to flesh out an implementation of MaxPQ that represents a priority queue using three instance variables:
Your goal in this part is to flesh out the skeleton of the MaxPQArray class in the file MaxPQArray.java. This class implements the MaxPQ interface. You will need to flesh out the following methods:
Constructor Method:
public MaxPQArray (Comparator c); public MaxPQArray ();
Instance Methods:
public void enq (Object x); public Object deq (); public Object first(); public int size(); public boolean isEmpty(); public void clear(); public Object clone(); public Enumeration elements(); public ObjectList toList();
Notes:
b. [10]: MinPQArray
It is possible to implement a MinPQ using the same three instance variables as above. Rather than creating such a class from scratch, it is possible to leverage off the exisiting MaxPQArray class by making MinPQArray a subclass of MaxPQArray that implements MinPQ. The file MinPQArray.java contains the skeleton of an implementation of the MinPQArray class defined in this way. Your goal is to flesh out the skeleton with the minimal number of methods that will make it correct. Hint: Study the files MaxPQList.java and MinPQList.java, which contain implementations of queues related in a similar way.
Test your implementation by invoking the main method via java MinPQArray. You should turn in a transcript of this invocation.
In this problem, you will implement queues in terms of the circular list representation sketched above. Using the static methods for mutable and immutable object lists, implement the following queue operations in the destructive interface to queues:
Constructor Method:
public QueueCircular ();
Instance Methods:
public boolean isEmpty(); public void enq (Object x); public Object deq (); public Object first(); public int size(); public void clear(); public Object clone(); public ObjectList toList();
The file QueueCircular.java contains code skeletons for these methods that you should flesh out. You can test your implementation by invoking java QueueCircular. You should submit a transcript of this invocation.
Notes:
public static int length (ObjectMList L); Returns the number of nodes in L. public static ObjectMList lastNode (ObjectMList L); Returns the last node of a non-cyclic list L. Raises an exception if L is empty. public static ObjectMList copy (ObjectMList L); Returns a shallow copy of L. The result consists of brand new ObjectMList nodes that share the same elements as L. public static ObjectList toObjectList (ObjectMList L); Returns an ObjectList that has the same elements as L. public static ObjectMList fromObjectList (ObjectList L); Returns an ObjectMList that has the same elements as L.
You are welcome to use either of the above two strategies in your size, clone, and toList methods. You can use different strategies for different methods if you like.