Académique Documents
Professionnel Documents
Culture Documents
1
LinkedList
1. The following program builds on Worksheet A15.1, ArrayList that uses an ArrayList to store and
perform geometric calculations on a set of coordinate points – using the Point2D.Double class. In
this problem, we will use a LinkedList instead of an ArrayList to store the points. This program
uses L istNode.java and S inglyLinkedList.java from the lesson, as well as the class below.
The following classes are used with the classes described above. The output from this code is
identical to that of Worksheet A15.1 -- except for one difference. Read through the code and
determine how the output differs. Use the same input from the keyboard that was used in Worksheet
A15.1: (1.5, 2.1), (9.7, 2.1), (9.7, 7.3), (1.5, 7.3) and don’t forget to enter the points in consecutive
order!
W.A.30.1(Page 1)
import java.util.Scanner;
import java.awt.geom.*; // for Point2D.Double
import java.util.*; // for ArrayList
class LinkedRectangle {
MySinglyLinkedList myList;
public LinkedRectangle(){
myList = new MySinglyLinkedList();
}
Output
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.Scanner;
import java.awt.geom.*; // for Point2D.Double
import java.util.*; // for ArrayList
class LinkedRectangle {
MySinglyLinkedList myList;
public LinkedRectangle(){
myList = new MySinglyLinkedList();
}
W.A.30.1(Page 3)
for (char ch = 'A'; ch <= 'D'; ch++){
System.out.print("Give me the x coordinate for point " + ch + ": ");
double x = console.nextDouble();
System.out.print("Give me the y coordinate for point " + ch + ": ");
double y = console.nextDouble();
Point2D.Double myPoint = new Point2D.Double(x,y);
myList.addFirst(myPoint);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.NoSuchElementException;
/**
* Implementation of lists, using singly linked elements.
*
* @author G. Peck
* @created April 27, 2002
*/
public class SinglyLinkedList{
private ListNode first; // first element
/**
* Constructor for the SinglyLinkedList object
* Generates an empty list.
*/
W.A.30.1(Page 4)
public SinglyLinkedList(){
first = null;
}
/**
* Returns the first element in this list.
*
* @return the first element in the linked list.
*/
public Object getFirst(){
if (first == null){
throw new NoSuchElementException();
}
else
return first.getValue();
}
/**
* Inserts the given element at the beginning of this list.
*
* @param value the element to be inserted at the beginning of this list.
*/
public void addFirst(Object value){
// note the order that things happen:
// head is parameter, then assigned
first = new ListNode(value, first);
}
/**
* Print the contents of the entire linked list
*/
public void printList(){
ListNode temp = first;// start from the first node
while (temp != null){
System.out.print(temp.getValue() + " ");
temp = temp.getNext();// go to next node
}
}
/**
* Returns a string representation of this list. The string
* representation consists of the list's elements in order,
* enclosed in square brackets ("[]"). Adjacent elements are
* separated by the characters ", " (comma and space).
*
W.A.30.1(Page 5)
* @return string representation of this list
*/
public String toString(){
String s = "[";
/**
* Constructs a new element with object initValue,
* followed by next element
*
* @param initValue New element object
* @param initNext Reference to next element
*/
public ListNode(Object initValue, ListNode initNext){
value = initValue;
next = initNext;
}
/**
* Constructs a new tail of a list with object initValue
*
W.A.30.1(Page 6)
* @param initValue New element object
*/
public ListNode(Object initValue){
this(initValue, null);
}
/**
* Sets the value attribute of the ListNode object
*
* @param theNewValue value attribute of the ListNode object
*/
public void setValue(Object theNewValue){
value = theNewValue;
}
/**
* Sets reference to new next value
*
* @param theNewNext The new next value
*/
public void setNext(ListNode theNewNext){
next = theNewNext;
}
/**
* Returns value associated with this element
*
* @return The value associated with this element
*/
public Object getValue(){
return value;
}
/**
* Returns reference to next value in list
*
* @return The next value in the list
*/
public ListNode getNext(){
return next;
}
}
W.A.30.1(Page 7)