Homework 3 - /Part I: Page 175 #3 - 6 and Page 197 #1, 3 6...

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

View Full Document Right Arrow Icon
//Part I: Page 175 #3 - 6 and Page 197 #1, 3 – 6 //3.Suppose that you want an operation for the ADT list that adds an array of items //to the end of the list. Write an implementation of this method for the class LList. public void addAll(T[] items){ for(int index = 0 ; index < items.length; index ++){ this.add(items[index]); // add method : Segment 6.23 } } // end addAll //4.Suppose that you want an operation for the ADT list that returns the position //of a given object in the list. Write an implementation of this method for the class LList. public int getPosition(T anObject){ int position = 0, counter = 0; assert !isEmpty(); Node currentNode = firstNode; while (currentNode != null) { counter ++; if(anObject.equals(currentNode.data)) position =counter ; currentNode=currentNode.next; } // end while return position; } // end getPosition //5.Implement an equals method for the class LList that returns true when the entries //in one list equal the entries in a second list. public boolean equals(LList secondList) { boolean result=true; Node currentNode = firstNode; if(length == secondList.getLength()){ while (currentNode != null) {
Background image of page 1

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

View Full DocumentRight Arrow Icon
if(! currentNode.data.equals(secondList.currentNode.data)) result=false; currentNode=currentNode.next; } // end while } // end if }else { result=false; }// end i return result; } // end equals //6.Suppose that a list contains Comparable objects. Implement a method that returns //a new list of items that are less than some given item. //Write an implementation of this method for the class LList. public LList<T> getAllLessThan(Comparable<T> anObject) { LList<T> newList = new LList(); Node currentNode = firstNode; while (currentNode != null){ if(currentNode.data.compareTo(anObject) < 0 ) newList.add(currentNode.data); currentNode=currentNode.next; } // end while return newList; } // end getAllLessThan //1.Suppose that you want an operation for the ADT list that removes the first occurence of a given object from the list. //the method returns true of the if the list contained anObject and the object was removed. Write an implementation of //this method for the class LList. public boolean remove(T anObject) { boolean isSuccessful = true; int position=1; assert !isEmpty(); Node currentNode = firstNode; while (currentNode != null){ if(currentNode.data.equals(anObject)){
Background image of page 2
if (position == 1) // case 1: remove first entry { firstNode = firstNode.next; }// end if else // case 2: givenPosition > 1 { Node nodeBefore = getNodeAt(position - 1); Node nodeToRemove = nodeBefore.next; Node nodeAfter = nodeToRemove.next; nodeBefore.next = nodeAfter; // disconnect the node to be removed
Background image of page 3

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

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

This note was uploaded on 01/23/2012 for the course CS 111C taught by Professor Metzler during the Spring '11 term at City College of San Francisco.

Page1 / 9

Homework 3 - /Part I: Page 175 #3 - 6 and Page 197 #1, 3 6...

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

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