Problem 1: Building and testing your own linked indexed list

Similar documents
CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

CSCI 200 Lab 3 Using and implementing sets

Problem 1: Building the BookCollection application

CSCI 200 Lab 1 Implementing and Testing Simple ADTs in Java

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems. COMP Week 8 & 9 1

CSCI 200 Lab 11 A Heap-Based Priority Queue

CSIS 10B Lab 2 Bags and Stacks

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Project 3: Implementing a List Map

Lecture Notes on Queues

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Binary Trees Due Sunday March 16, 2014

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Homework #10 due Monday, April 16, 10:00 PM

Lecture 10 Notes Linked Lists

Chapter 4 Defining Classes I

Answer Key. 1. General Understanding (10 points) think before you decide.

List ADT. B/W Confirming Pages

Introduction to Computing II (ITI 1121) Final Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Soda Machine Laboratory

Server: Discrete Event-Driven Simulations

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Lecture Notes on Queues

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Computer Science 62. Midterm Examination

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lecture 10 Notes Linked Lists

Compuer Science 62 Assignment 10

6.170 Laboratory in Software Engineering Java Style Guide. Overview. Descriptive names. Consistent indentation and spacing. Page 1 of 5.

UNIVERSITY REGULATIONS

CS 10, Fall 2015, Professor Prasad Jayanti

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Data Abstraction and Specification of ADTs

Tips from the experts: How to waste a lot of time on this assignment

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

ITI Introduction to Computing II

+ Abstract Data Types

Comp215: More lists. Dan S. Wallach (Rice University) Copyright 2015, Dan S. Wallach. All rights reserved.

COMP-520 GoLite Tutorial

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

CSC 1052 Algorithms & Data Structures II: Queues

Classes, interfaces, & documentation. Review of basic building blocks

CS112 Lecture: Defining Instantiable Classes

Programming Exercise 14: Inheritance and Polymorphism

EE 422C HW 6 Multithreaded Programming

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

CS61BL Summer 2013 Midterm 2

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

Lecture 10 Linked Lists

Initial Coding Guidelines

PRACTICE MIDTERM EXAM #2

ArrayList. Introduction. java.util.arraylist

ITI Introduction to Computing II

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

Documentation Requirements Computer Science 2334 Spring 2016

CS 216 Exam 1 Fall SOLUTION

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

Stacks and Queues. Gregory D. Weber. CSCI C243 Data Structures

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Csci 102: Sample Exam

Created by Julie Zelenski, with edits by Jerry Cain, Keith Schwarz, Marty Stepp, Chris Piech and others.

CS 6353 Compiler Construction Project Assignments

CS18000: Programming I

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

10 Java Collections; Equality, JUnit

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Student Performance Q&A:

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

Lecture 18 Restoring Invariants

Section 05: Solutions

Recursion. CSCI 112: Programming in C

Introduction to Computing II (ITI 1121) Final Examination

Topic 14. The BinaryTree ADT

STACKS AND QUEUES. Problem Solving with Computers-II

Some Extra Information on Graph Search

Lab 3: Call to Order CSCI 2101 Fall 2018

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

COMP 250 Winter 2011 Reading: Java background January 5, 2011

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

16 Multiple Inheritance and Extending ADTs

Induction and Semantics in Dafny

Lab 3: Call to Order CSCI 2101 Fall 2017

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

Our second exam is Thursday, November 10. Note that it will not be possible to get all the homework submissions graded before the exam.

CS61BL: Data Structures & Programming Methodology Summer 2014

Transcription:

CSCI 200 Lab 8 Implementing a Linked Indexed List In this lab, you will be constructing a linked indexed list. You ll then use your list to build and test a new linked queue implementation. Objectives: to use existing software components to support building new software to gain insight into using linked structures to build a list data structure to use your own list ADT in order to build a new queue ADT Problem 1: Building and testing your own linked indexed list Part 1.1: Creating the <FOO>LinkedIndexedList class Get the lab files, and keep them in your lab folder. Make a new Java file in your JavaPackages/<foo>structures directory and name it <FOO>LinkedIndexedList.java, replacing <FOO> as usual with your uppercase initials. You will create the new list class by completing this file. Open the new file in DrJava, and start with a class declaration that implements the ZHIndexedList<ElementType> interface. Open the Javadoc page for the zhstructures package API, and select the ZHIndexedList interface page. Add method declarations for all the methods described in the interface documentation, and make them stub methods by adding dummy return statements for the methods that return a value, as we have done before. Since this new class is very similar to the <FOO>LinkedStack class you implemented in Lab 5, you should open the code for the stack in a separate DrJava window. Like your linked stack and queue classes from before, your linked indexed list should include an instance variable of an inner class (we ll call it ListNode this time) called firstnode to store a reference to the first node of the linked indexed list. In addition, you ll need to maintain the usual size integer instance variable. Therefore, your class should contain the following two private instances variables and only these two: firstnode and size. Add a default constructor to your class to initialize size to zero and firstnode to be a new empty ListNode. The last node in your list should always be an empty, terminal node, which firstnode refers to whenever the list is empty. Copy the StackNode inner class from your <FOO>LinkedStack class into your linked indexed list class, and modify it to be: protected class ListNode extends ZHOneWayListNode<ElementType, ListNode> with the same two constructors, renamed accordingly. Please take some time to remind yourself of how the class ZHOneWayListNode works by referring to its documentation; remember that the ListNode class inherits all public and protected methods from the ZHOneWayListNode class. Now consider the Javadoc comments in this file. You need an appropriate class header comment that reflects the interface it implements and includes the information that it is a

linked list implementation. Include a @version tag with the date of this lab, and make sure the @author tag lists you and your lab partner. Add appropriate comments to the constructors. The remaining comments are all method comments, and all these methods are present by inheritance from the interface. It turns out that Javadoc method comments follow an inheritance rule that if the method has no Javadoc comment, the Javadoc utility will look for a Javadoc comment for the method in the super-interfaces, if any, or else in the super-classes. We can invoke this inheritance by converting any Javadoc comments for methods in this class into ordinary comments (preferable) or by omitting them altogether. Note that you will need to write actual Javadoc comments for any public or protected elements that you add to the class, including its constructors. Make sure your class compiles without errors, even though you haven t actually implemented any of the methods yet.

Part 1.2: Completing the methods in the <FOO>LinkedIndexedList class You can implement several of the methods in the outer class by using the corresponding methods in the <FOO>LinkedStack class. Keep a copy of the code for that class open so you can refer to it and even copy code from it. However, be careful not to make accidental changes in that file while you work if you use your original. There are four different categories of outer class methods, based on how they interact with the internal chain of nodes. The write-up lists the methods in each category, but you will also need to determine which category to put one of the methods into. Here are the categories: Some methods operate directly on the size instance variable (for efficiency) and don t reference the nodes at all; these methods include isempty and size. You should complete these methods first; note that the versions in your stack class should work with little or no change. Some methods can call inherited methods of the ListNode inner-class on the firstnode instance variable. For example, the ListNode class inherits contains and iterator methods from the ZHOneWayListNode class, so the corresponding outer-class methods can just call the inner-class methods on the firstnode instance variable. Again, the existing outer-class methods in the linked stack should work with little or no modification. Several methods that access the nodes, including get, set, addelementat, removeelementat, indexof, and lastindexof, lend themselves to a two-pronged recursive approach: outer-class (master) methods call similarly named recursive (auxiliary) methods in the inner class ListNode on the firstnode instance variable. The outer class methods may to do a little work before the recursive call, such as checking for out-of-bounds indices; you should compare them to the similar stack methods for ideas on how to implement them. Finally, some methods that access the nodes may be easier to implement iteratively (that is, without recursion); in this case, you should implement the method completely in the outer class. You should implement the sublist and containsduplicates methods iteratively. In general, if you can implement the method both iteratively and recursively, you should choose the more efficient implementation; if the implementations are about equally efficient, choose the one that has simpler code, which will usually be the recursive version. Do not add instance variables or other methods to either the outer class or the inner class, unless the write-up clearly calls for them. Start by implementing outer-class methods in the first two categories, and then turn to methods in the third category. These methods will also use the firstnode instance variable to make corresponding calls to inner-class methods with the same names; add these methods to the inner-class ListNode. For example, the get method in the outer class should return the result of calling firstnode.get, which requires us to define a get method in the ListNode class, since it doesn t already exist there, unlike the contains method, which is inherited from the parent class ZHOneWayListNode. You are mostly on your own here; however, you must use recursion rather than iteration via loops. In addition, please keep in mind that any index-based method (that is, a method based on location of elements in the list) may need an additional parameter representing the index

we re currently at in the list. Most methods in this category already include an index parameter except for the methods indexof and lastindexof. These last two methods are a little tricky, so we ll give you a little more guidance with them. The auxiliary indexof method in the inner-class needs to keep track of the node number (starting at zero) until it finds the desired item or gets to the end of the list. Here is the heading for this method: /** * Searches for parameter element starting at a given index in the * list and returns index of first match or -1 if no match is found. * * @param index to identify where to start the search, * which is the current location in the list * @param element what we re searching for * @return index of first occurrence of the element searched for * or -1 if the element is not in list */ protected int indexof(int index, ElementType element) The outer-class method returns the value of the call firstnode.indexof(0, element). As the auxiliary method makes recursive calls, it adds one to the value of the index parameter each time, insuring that index is always the position of the current node in the list. The recursion ends either when element is equal to the current node s element or when it reaches the final terminal node. The lastindexof method returns the position of the last occurrence of an element, so the auxiliary method must always search to the end of the list since the final occurrence may be yet to come. Here is the heading for this method: /** * Searches for parameter element starting at a given index in the * list and returns index of last match or -1 if no match is found. * * @param index to identify where to start the search, * which is the current location in the list * @param element what we re searching for * @return index of last occurrence of the element searched for * or -1 if the element is not in list */ protected int lastindexof(int index, ElementType element) Here, the outer-class method returns the value of the call firstnode.lastindexof(0, element). As with the previous method, the auxiliary lastindexof method increments the value of the index parameter with each recursive call. The recursion ends at the end of the list; as the recursion returns to non-empty nodes, if the returned value is 1, the current node checks its element against the search element and returns its index if they are equal; otherwise the current node returns the value returned by the recursive call. You may implement the last two methods (sublist and containsduplicates) either iteratively or using recursive auxiliary methods. Either way, you need to follow the documentation carefully to implement it correctly. If you choose to do an iterative implementation, note that you can

use the get method with a for-loop to iterate over the elements in the list by location or an enhanced for loop instead, since your class implements Iterable. This approach is likely to be rather inefficient, but it s probably the easiest way to code it, and it s completely acceptable for this lab. To do the iterative version efficiently, you need to work directly with the nodes and keep track of your position in the list manually, which can be very tricky. The recursive solution is probably the most elegant and efficient way to do it, but this time, we re leaving you on your own to work out the recursive thinking. Part 1.3: Testing the linked indexed list class The class IndexedListTest allows you to test your linked indexed list class once it compiles. Keep this class in your lab folder. If you haven t done so already, open the file and change the import <foo>structures.*; statement to your structures package, and again change all instances of <FOO> to the (capitalized) initials you are using for your structures. When you have finished testing and believe your implementation is correct, show it to the lab instructor or the TA.

Problem 2: A new queue class based on the linked indexed list That's right you actually can use an implementation you developed to develop another implementation! Isn t life just cool! Part 2.1: Creating a new queue class The linked queue implementation you built in an earlier lab uses a linked structure called QueueNode to store the nodes of the queue. Recall that we had to maintain the linked structure manually using the front and rear queue nodes. In this problem, you are to design and implement a new linked queue class that uses an instance variable of your <FOO>LinkedIndexedList class. This class will be a much easier to implement than your earlier linked queue class since your linked list instance variable already takes care of maintaining its own underlying linked structure. Start by creating an appropriately named <FOO>IndexedListQueue class in your JavaPackages/<foo>structures package. This class should implement the ZHQueue<ElementType> interface in the zhstructures package. It should contain the following and only the following instance variable: private ZHIndexedList<ElementType> innerlist; Since the instance variable innerlist is of type <FOO>IndexedList, it provides you with many functionalities, including those you can use to build a queue. Your new linked queue class constructor should initialize innerlist to be an empty <FOO>LinkedIndexedList. All methods in this class should make appropriate calls on this instance variable, so you should be able to complete the remainder of this queue class on your own. First, you need to decide which end to enqueue to and dequeue from, then decide how to use the indexed list methods to implement the queue methods. It should be possible to implement every queue method by a single call to an indexed list method. Part 2.2: Testing the new linked queue class The IndexedListQueueTest class allows you to test your class once it compiles. Note that this is essentially the same test class you used to test your previous queue implementation. Keep this class in your lab folder. If you haven t done so already, open the file and change the import <foo>structures.*; statement to your structures package, and again change all instances of <FOO> to the (capitalized) initials you are using for your structures. When you have finished testing and believe your implementation is correct, show it to the lab instructor or the TA.