新人 关于Ordered Linked list的问题

05-05-09 ccie307
Ordered Linked list

===================

Below you can find simple implementation of linked list located in class

LinkedList. Current code doens't know how to keep list items in sorted

order. Your job is to write class OrderedLinked list that must *extend*

LinkedList and override the addItem() method so that the list keeps items

in *increasing* order.

Items placed in your list will all implement Sortable interface, so you

can use their isGreater(Sortable s) method to find out the correct order.

Your addItem should take one Sortable object as it's argument.

Do not make any modifications to LinkedList, LinkedListNode, Sortable or

LinkedListAssignment classes/interfaces.

You have to also *implement* Sortable interface in IntegerData class.

(Modify the class, don't extend it)

Typical interaction

===================

Enter numbers to add to the list.

-1 terminates.

1

5

3

6

2

-1

List:

1

2

3

5

6

.

(NOTE: '.' character denotes the end of the list!)

*/

import java.io.*;

/* Do your modifications below */

class OrderedLinkedList extends LinkedList {

}

/**************************************************************

End of your part

Don't modify below this line!

**************************************************************/

class LinkedList {

public LinkedListNode firstNode;

public LinkedList() {

firstNode=null;

}

public void addItem(Object o) {

LinkedListNode lln = new LinkedListNode(o);

if(firstNode==null)

firstNode=lln;

else {

lln.setNext(firstNode.getNext());

firstNode.setNext(lln);

}

}

public LinkedListNode getFirstNode() {

return firstNode;

}

protected void setFirstNode(LinkedListNode n) {

firstNode=n;

}

public String toString() {

LinkedListNode temp = firstNode;

String s="List:\n";

while(temp!=null) {

s=s+temp.getData().toString()+"\n";

temp=temp.getNext();

}

s=s+".";

return s;

}

}

/* DON'T MODIFY THIS CLASS */

class LinkedListNode {

private Object data;

private LinkedListNode next;

public LinkedListNode(Object o) {

data=o;

}

public Object getData(){

return(data);

}

public void setData(Object o) {

data=o;

}

public LinkedListNode getNext() {

return(next);

}

public void setNext(LinkedListNode l) {

next=l;

}

}

/* DON'T MODIFY THIS INTERFACE */

interface Sortable {

/* Returns true if the object should be placed after the

object s. Should return false if values are equal. If tested

against null, should return false.*/

public boolean isGreater(Sortable s);

}

/* DON'T MODIFY THIS CLASS */

public class prog64 {

public static void main (String args[]) throws IOException {

BufferedReader stdin = new BufferedReader(

new InputStreamReader(System.in));

System.out.println("Enter numbers to add to the list.");

System.out.println("-1 terminates.");

int n;

OrderedLinkedList oll = new OrderedLinkedList();

n=Integer.parseInt(stdin.readLine());

while (n!=-1) {

oll.addItem(new IntegerData(n));

n=Integer.parseInt(stdin.readLine());

}

System.out.println(oll);

}

}

猜你喜欢