BuildDLL.java
public class BuildDLL {
DoubleLinkedNode front, rear;
private static char[] letters = new char[] {'K', 'T', 'E', 'N', 'P', 'A', 'L'};
public BuildDLL () {
build();
}
public void remove (Character elem) {
// Add code in here to remove the node with the given value.
}
private void build () {
DoubleLinkedNode pnode, node;
node = new DoubleLinkedNode(letters[0]);
pnode = front = node;
for (int i = 1; i < 7; i++) {
node = new DoubleLinkedNode(letters[i]);
pnode.setNext(node);
node.setPrevious(pnode);
pnode = node;
}
rear = node;
}
public DoubleLinkedNode getFront () {
return front;
}
public DoubleLinkedNode getRear () {
return rear;
}
public void printF (DoubleLinkedNode node) {
DoubleLinkedNode curr = front;
while (curr != null) {
System.out.print(curr.getElement() + " ");
curr = curr.getNext();
}
System.out.print("\n");
}
public static void main (String[] args) {
BuildDLL dll = new BuildDLL();
System.out.println("Original List:");
dll.printF(dll.getFront());
System.out.println("***");
System.out.println("Removing an internal node:");
dll.remove('N');
dll.printF(dll.getFront());
System.out.println("***");
System.out.println("Removing the front node:");
dll.remove('K');
dll.printF(dll.getFront());
System.out.println("***");
System.out.println("Removing the rear node:");
dll.remove('L');
dll.printF(dll.getFront());
System.out.println("***");
}
}
BuildLinkedList.java
/**
* Build a linked list of integers from 1 to 10
*
* @author CS1027
*/
public class BuildLinkedList {
/*
* Print the information stored in all the nodes of the list whose first node is
* referenced by front
*/
private static void printList(LinearNode front) {
LinearNode current = front;
for (int i = 1; i <= 10; i++) {
System.out.println(current.getElement());
current = current.getNext();
}
}
public static void main(String[] args) {
// create a linked list that holds 1, 2, ..., 10
// by starting at 10 and adding each node at head of list
LinearNode front = null; // create empty linked list
LinearNode intNode;
for (int i = 10; i >= 1; i--) {
// create a new node for i
intNode = new LinearNode(new Integer(i));
// add it at the head of the linked list
intNode.setNext(front);
front = intNode;
}
printList(front);
}
}
DoubleLinkedNode.java
public class DoubleLinkedNode {
private DoubleLinkedNode next;
private DoubleLinkedNode previous;
private E element;
public DoubleLinkedNode(){
next = null;
previous = null;
element = null;
}
public DoubleLinkedNode (E elem){
next = null;
previous = null;
element = elem;
}
public DoubleLinkedNode getNext(){
return next;
}
public DoubleLinkedNode getPrevious(){
return previous;
}
public void setNext (DoubleLinkedNode node){
next = node;
}
public void setPrevious (DoubleLinkedNode node){
previous = node;
}
public E getElement(){
return element;
}
public void setElement (E elem){
element = elem;
}
}
LinearNode.java
/**
* LinearNode represents a node in a linked list.
*
* @author Dr. Lewis
* @author Dr. Chase
* @version 1.0, 08/13/08
*/
public class LinearNode
{
private LinearNode next;
private E element;
/**
* Creates an empty node.
*/
public LinearNode()
{
next = null;
element = null;
}
/**
* Creates a node storing the specified element.
*
* @param elem the element to be stored within the new node
*/
public LinearNode (E elem)
{
next = null;
element = elem;
}
/**
* Returns the node that follows this one.
*
* @return the node that follows the current one
*/
public LinearNode getNext()
{
return next;
}
/**
* Sets the node that follows this one.
*
* @param node the node to be set to follow the current one
*/
public void setNext (LinearNode node)
{
next = node;
}
/**
* Returns the element stored in this node.
*
* @return the element stored in this node
*/
public E getElement()
{
return element;
}
/**
* Sets the element stored in this node.
*
* @param elem the element to be stored in this node
*/
public void setElement (E elem)
{
element = elem;
}
}
Person.java
/**
* Class that represents a person with attributes name, email address
* @author CS1027
*
*/
public class Person {
/* Attribute declarations */
private String lastName; // last name
private String firstName; // first name
/**
* Constructor initializes the person's name and email address
*/
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
/**
* getName method returns the person's full name
* @return first name followed by last name, blank separated
*/
public String getName () {
return firstName + " " + lastName;
}
/**
* toString method returns a string representation of the person
* @return string with first name and last name, email address
*/
public String toString() {
String s = firstName + " " + lastName;
return s;
}
}
TestReverse.java
public class TestReverse {
public static void main (String[] args) {
String[] arr1 = new String[] {"atom", "breeze", "clock", "daydream", "energy"};
ReversibleArray revArr1 = new ReversibleArray(arr1);
System.out.println(revArr1.toString());
revArr1.reverse();
System.out.println(revArr1.toString());
Integer[] arr2 = new Integer[] {11, 22, 33, 44, 55, 66, 77};
ReversibleArray revArr2 = new ReversibleArray(arr2);
System.out.println(revArr2.toString());
revArr2.reverse();
System.out.println(revArr2.toString());
Person p1 = new Person("Jerry", "Seinfeld");
Person p2 = new Person("Salma", "Hayek");
Person p3 = new Person("Reese", "Witherspoon");
Person p4 = new Person("Vince", "Vaughn");
Person[] arr3 = new Person[] {p1, p2, p3, p4};
ReversibleArray revArr3 = new ReversibleArray(arr3);
System.out.println(revArr3.toString());
revArr3.reverse();
System.out.println(revArr3.toString());
}
}