Implementing an Iterator to a Doubly Linked List - java

I have been having trouble trying to figure out how to add an iterator to this, I am really confused how to start here is the code. I imported the Iterator but I have no idea where I would begin to add it in here
public class DoublyLinkedList<E> implements Iterator <E> {
/**
* Node of a doubly linked list, which stores a reference to its
* element and to both the previous and next node in the list.
*/
private static class Node<E> {
/** The element stored at this node */
private E element; // reference to the element stored at this node
/** A reference to the preceding node in the list */
private Node<E> prev; // reference to the previous node in the list
/** A reference to the subsequent node in the list */
private Node<E> next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* #param e the element to be stored
* #param p reference to a node that should precede the new node
* #param n reference to a node that should follow the new node
*/
public Node(E e, Node<E> p, Node<E> n) {
element = e;
prev = p;
next = n;
}
// public accessor methods
/**
* Returns the element stored at the node.
* #return the element stored at the node
*/
public E getElement() {
return element;
}
/**
* Returns the node that precedes this one (or null if no such node).
* #return the preceding node
*/
public Node<E> getPrev() {
return prev;
}
/**
* Returns the node that follows this one (or null if no such node).
* #return the following node
*/
public Node<E> getNext() {
return next;
}
// Update methods
/**
* Sets the node's previous reference to point to Node n.
* #param p the node that should precede this one
*/
public void setPrev(Node<E> p) {
prev = p;
}
/**
* Sets the node's next reference to point to Node n.
* #param n the node that should follow this one
*/
public void setNext(Node<E> n) {
next = n;
}
}
// instance variables of the DoublyLinkedList
/** Sentinel node at the beginning of the list */
private Node<E> header; // header sentinel
/** Sentinel node at the end of the list */
private Node<E> trailer; // trailer sentinel
/** Number of elements in the list (not including sentinels) */
private int size = 0; // number of elements in the list
/** Constructs a new empty list. */
public DoublyLinkedList() {
header = new Node<>(null, null, null); // create header
trailer = new Node<>(null, header, null); // trailer is preceded by header
header.setNext(trailer); // header is followed by trailer
}
// public accessor methods
/**
* Returns the number of elements in the linked list.
* #return number of elements in the linked list
*/
public int size() {
return size;
}
/**
* Tests whether the linked list is empty.
* #return true if the linked list is empty, false otherwise
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Returns (but does not remove) the first element of the list.
* #return element at the front of the list (or null if empty)
*/
public E first() {
if (isEmpty()) return null;
return header.getNext().getElement(); // first element is beyond header
}
/**
* Returns (but does not remove) the last element of the list.
* #return element at the end of the list (or null if empty)
*/
public E last() {
if (isEmpty()) return null;
return trailer.getPrev().getElement(); // last element is before trailer
}
// public update methods
/**
* Adds an element to the front of the list.
* #param e the new element to add
*/
public void addFirst(E e) {
addBetween(e, header, header.getNext()); // place just after the header
}
/**
* Adds an element to the end of the list.
* #param e the new element to add
*/
public void addLast(E e) {
addBetween(e, trailer.getPrev(), trailer); // place just before the trailer
}
/**
* Removes and returns the first element of the list.
* #return the removed element (or null if empty)
*/
public E removeFirst() {
if (isEmpty()) return null; // nothing to remove
return remove(header.getNext()); // first element is beyond header
}
/**
* Removes and returns the last element of the list.
* #return the removed element (or null if empty)
*/
public E removeLast() {
if (isEmpty()) return null; // nothing to remove
return remove(trailer.getPrev()); // last element is before trailer
}
// private update methods
/**
* Adds an element to the linked list in between the given nodes.
* The given predecessor and successor should be neighboring each
* other prior to the call.
*
* #param predecessor node just before the location where the new element is inserted
* #param successor node just after the location where the new element is inserted
*/
private void addBetween(E e, Node<E> predecessor, Node<E> successor) {
// create and link a new node
Node<E> newest = new Node<>(e, predecessor, successor);
predecessor.setNext(newest);
successor.setPrev(newest);
size++;
}
/**
* Removes the given node from the list and returns its element.
* #param node the node to be removed (must not be a sentinel)
*/
private E remove(Node<E> node) {
Node<E> predecessor = node.getPrev();
Node<E> successor = node.getNext();
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return node.getElement();
}
/**
* Produces a string representation of the contents of the list.
* This exists for debugging purposes only.
*/
public String toString() {
StringBuilder sb = new StringBuilder("(");
Node<E> walk = header.getNext();
while (walk != trailer) {
sb.append(walk.getElement());
walk = walk.getNext();
if (walk != trailer)
sb.append(", ");
}
sb.append(")");
return sb.toString();
}
#Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
#Override
public E next() {
// TODO Auto-generated method stub
return null;
}
} //----------- end of DoublyLinkedList class -----------

First, your List should not implement the Iterator interface. Iterator is capable to traverse only once. Usually lists implement Iterable interface and have iterator() method which can create a new iterator for this list. There are several similar questions answered already. Check this for example.

You do not now how to implement next() and hasNext() methods?
You can store a "current" node element in a field (initialized by first()), and in hasNext() method compare it with last(). In next() method you should assign current node the getNext() of it an return the node.

Related

Linked List Stack/Queue implementation emptying for no apparent reason?

I've been trying to program a LinkedList implementation of a Stack and of a Queue. Whenever I run them however, I get an error letting me know that the list is empty when I try and pop/dequeue anything. The thing is, if I call .toString() on the list, even RIGHT before the pop/dequeue command, I can see that they're not. I can't myself see anything wrong with my pop/dequeue implementation, but perhaps you fine folks can help me out.
LinkedList.java
package jsjf;
import jsjf.exceptions.*;
import java.util.*;
/**
* LinkedList represents a linked implementation of a list.
*
* #author Java Foundations
* #version 4.0
*/
public class LinkedList<T> implements ListADT<T>, Iterable<T>
{
protected int count;
protected LinearNode<T> head, tail;
protected int modCount;
/**
* Creates an empty list.
*/
public LinkedList()
{
count = 0;
head = tail = null;
modCount = 0;
}
/**
* Adds a new element to the end of the list.
*
* #param element the element to add.
*/
public void add(T element) {
// If the list is empty...
if(this.tail == null){
// Creates a new node for the new element.
LinearNode<T> newElement = new LinearNode(element);
// Assigns the new node to be the head and the tail.
this.tail = this.head = newElement;
}
// Otherwise...
else {
// Temporary caches the old tail.
LinearNode<T> temp = this.tail;
// Creates a new node for the new element.
LinearNode<T> newElement = new LinearNode(element);
// Assigns the new node to follow the old tail.
temp.setNext(newElement);
// Assigns the old tail to precede the new node.
newElement.setPrevious(temp);
// Assigns the new node to be the tail.
this.tail = newElement;
}
}
/**
* Adds a new element at a specific index, bumping the current element at
* that index to the next index.
*
* #param index the index to add to.
* #param element the element to add.
*/
public void add(int index, T element) {
// Find the node at the index requested.
LinearNode<T> desiredIndex = this.head;
for(int i = 1; i <= index; i++){
desiredIndex = desiredIndex.getNext();
}
// Hold the previous element of the old element.
LinearNode<T> prev = desiredIndex.getPrevious();
// Create a new node for the new element.
LinearNode<T> newElement = new LinearNode(element);
// Assign prev to precede the new element.
newElement.setPrevious(prev);
// Assign the old element to follow the new element.
newElement.setNext(desiredIndex);
// Assign the new element to precede the old element.
desiredIndex.setPrevious(newElement);
}
/**
* Returns an element from a specific index, without removing it.
*
* #param index the index to check.
* #return the element at that index.
*/
public T get(int index) {
// Find the node at the index requested.
LinearNode<T> desiredIndex = this.head;
for(int i = 1; i <= index; i++){
desiredIndex = desiredIndex.getNext();
}
// Return that element.
return desiredIndex.getElement();
}
/** Removes the first element in this list and returns a reference
* to it. Throws an EmptyCollectionException if the list is empty.
*
* #return a reference to the first element of this list
* #throws EmptyCollectionException if the list is empty
*/
public T removeFirst() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("list");
T result = head.getElement();
head = head.getNext();
count--;
if(isEmpty())
tail = null;
modCount++;
return result;
}
/**
* Removes the last element in this list and returns a reference
* to it. Throws an EmptyCollectionException if the list is empty.
*
* #return the last element in this list
* #throws EmptyCollectionException if the list is empty
*/
public T removeLast() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("list");
T result = tail.getElement();
tail = tail.getPrevious();
count--;
if(isEmpty())
tail = null;
modCount++;
return result;
}
/**
* Removes the first instance of the specified element from this
* list and returns a reference to it. Throws an EmptyCollectionException
* if the list is empty. Throws a ElementNotFoundException if the
* specified element is not found in the list.
*
* #param targetElement the element to be removed from the list
* #return a reference to the removed element
* #throws EmptyCollectionException if the list is empty
* #throws ElementNotFoundException if the target element is not found
*/
public T remove(T targetElement) throws EmptyCollectionException,
ElementNotFoundException
{
if (isEmpty())
throw new EmptyCollectionException("LinkedList");
boolean found = false;
LinearNode<T> previous = null;
LinearNode<T> current = head;
while (current != null && !found)
if (targetElement.equals(current.getElement()))
found = true;
else
{
previous = current;
current = current.getNext();
}
if (!found)
throw new ElementNotFoundException("LinkedList");
if (size() == 1) // only one element in the list
head = tail = null;
else if (current.equals(head)) // target is at the head
head = current.getNext();
else if (current.equals(tail)) // target is at the tail
{
tail = previous;
tail.setNext(null);
}
else // target is in the middle
previous.setNext(current.getNext());
count--;
modCount++;
return current.getElement();
}
/**
* Changes the element at a specific index.
*
* #param index the index to change.
* #param element the element to change to.
*/
public void set(int index, T element) {
// Find the node at the index requested.
LinearNode<T> desiredIndex = this.head;
for(int i = 1; i <= index; i++){
desiredIndex = desiredIndex.getNext();
}
// Change the element at that index.
desiredIndex.setElement(element);
}
/**
* Returns the first element in this list without removing it.
*
* #return the first element in this list
* #throws EmptyCollectionException if the list is empty
*/
public T first() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("list");
T result = head.getElement();
return result;
}
/**
* Returns the last element in this list without removing it.
*
* #return the last element in this list
* #throws EmptyCollectionException if the list is empty
*/
public T last() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("list");
T result = tail.getElement();
return result;
}
/**
* Returns true if the specified element is found in this list and
* false otherwise. Throws an EmptyCollectionException if the list
* is empty.
*
* #param targetElement the element that is sought in the list
* #return true if the element is found in this list
* #throws EmptyCollectionException if the list is empty
*/
public boolean contains(T targetElement) throws
EmptyCollectionException
{
if(isEmpty())
throw new EmptyCollectionException("list");
boolean found = false;
LinearNode<T> previous = null;
LinearNode<T> current = head;
while (current != null && !found)
if (targetElement.equals(current.getElement()))
found = true;
else
{
previous = current;
current = current.getNext();
}
if (!found)
throw new ElementNotFoundException("LinkedList");
return found;
}
/**
* Returns true if this list is empty and false otherwise.
*
* #return true if the list is empty, false otherwise
*/
public boolean isEmpty()
{
return (count == 0);
}
/**
* Returns the number of elements in this list.
*
* #return the number of elements in the list
*/
public int size()
{
return count;
}
/**
* Returns a string representation of this list.
*
* #return a string representation of the list
*/
public String toString()
{
String result = "";
LinearNode current = head;
while (current != null)
{
result = result + current.getElement() + "\n";
current = current.getNext();
}
return result;
}
/**
* Returns an iterator for the elements in this list.
*
* #return an iterator over the elements of the list
*/
public Iterator<T> iterator()
{
return new LinkedListIterator();
}
/**
* LinkedIterator represents an iterator for a linked list of linear nodes.
*/
private class LinkedListIterator implements Iterator<T>
{
private int iteratorModCount; // the number of elements in the collection
private LinearNode<T> current; // the current position
/**
* Sets up this iterator using the specified items.
*
* #param collection the collection the iterator will move over
* #param size the integer size of the collection
*/
public LinkedListIterator()
{
current = head;
iteratorModCount = modCount;
}
/**
* Returns true if this iterator has at least one more element
* to deliver in the iteration.
*
* #return true if this iterator has at least one more element to deliver
* in the iteration
* #throws ConcurrentModificationException if the collection has changed
* while the iterator is in use
*/
public boolean hasNext() throws ConcurrentModificationException
{
if (iteratorModCount != modCount)
throw new ConcurrentModificationException();
return (current != null);
}
/**
* Returns the next element in the iteration. If there are no
* more elements in this iteration, a NoSuchElementException is
* thrown.
*
* #return the next element in the iteration
* #throws NoSuchElementException if the iterator is empty
*/
public T next() throws ConcurrentModificationException
{
if (!hasNext())
throw new NoSuchElementException();
T result = current.getElement();
current = current.getNext();
return result;
}
/**
* The remove operation is not supported.
*
* #throws UnsupportedOperationException if the remove operation is called
*/
public void remove() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
}
}
LinkedListQueue.java
package jsjf;
public class LinkedListQueue<T> implements QueueADT<T> {
LinkedList queue = new LinkedList();
/**
* Adds the specified element to the back of this queue.
*
* #param element generic element to be pushed onto queue.
*/
public void enqueue(T element) {
queue.add(element);
}
/**
* Removes the element from the front of the queue and returns it.
*
* #return the element from the front of the queue.
*/
public T dequeue() {
return (T) queue.removeFirst();
}
/**
* Returns the front element, without removing it.
*
* #return the element at the front of the queue.
*/
public T first() {
return (T) queue.first();
}
/**
* Returns true if the queue is empty.
*
* #return the boolean value of this queue being empty.
*/
public boolean isEmpty() {
if(queue.isEmpty())
return true;
return false;
}
/**
* Returns the number of elements contained in the queue.
*
* #return the number of elements contained in the queue.
*/
public int size() {
return queue.size();
}
/**
* Returns the queue as a string.
*
* #return the queue as a string.
*/
public String toString() {
return queue.toString();
}
}
LinkedListStack.java
package jsjf;
public class LinkedListStack<T> implements StackADT<T> {
LinkedList stack = new LinkedList();
/**
* Adds the specified element to the top of this stack.
*
* #param element generic element to be pushed onto stack.
*/
public void push(T element) {
stack.add(element);
}
/**
* Removes the element from the top of the stack and returns it.
*
* #return the element from the top of the stack.
*/
public T pop() {
return (T) stack.removeLast();
}
/**
* Returns the top element, without removing it.
*
* #return the element at the top of the stack.
*/
public T peek() {
return (T) stack.last();
}
/**
* Returns true if the stack is empty.
*
* #return the boolean value of this stack being empty.
*/
public boolean isEmpty() {
if(stack.isEmpty())
return true;
return false;
}
/**
* Returns the number of elements contained in the stack.
*
* #return the number of elements contained in the stack.
*/
public int size() {
return stack.size();
}
/**
* Returns the stack as a string.
*
* #return the stack as a string.
*/
public String toString() {
return stack.toString();
}
}
Driver.java
package jsjf;
public class Driver {
public static void main(String[] args) {
// Instantiate the array based structures.
ArrayListQueue arrayQueue = new ArrayListQueue();
ArrayListStack arrayStack = new ArrayListStack();
// Instantiate the link based structures.
LinkedListQueue linkedQueue = new LinkedListQueue();
LinkedListStack linkedStack = new LinkedListStack();
// An integer to hold reference to a data piece to be pushed to the structures.
int data;
// Randomly generate a piece of data, and pass it to the structures.
for(int i = 0; i < 25; i++){
data = (int)(Math.random() * 50);
arrayQueue.enqueue(data);
arrayStack.push(data);
linkedQueue.enqueue(data);
linkedStack.push(data);
}
System.out.print("Array Queue: ");
for(int i = 0; i < 25; i++){
System.out.print(arrayQueue.dequeue() + " ");
}
System.out.println();
System.out.print("Array Stack: ");
for(int i = 0; i < 25; i++){
System.out.print(arrayStack.pop() + " ");
}
System.out.println();
System.out.print("\n\nLinked Stack as string: " + linkedStack.toString() + "\n\n");
System.out.print("Linked Stack: ");
for(int i = 0; i < 25; i++){
System.out.print(linkedStack.pop() + " ");
}
System.out.println();
System.out.print("Linked Queue: " + linkedQueue.toString());
System.out.print("Linked Queue: ");
for(int i = 0; i < 25; i++){
System.out.print(linkedQueue.dequeue() + " ");
}
System.out.println();
}
}
When I run Driver.java, I get the following output.
Array Queue: 45 12 25 40 31 32 14 16 14 26 3 25 22 26 29 6 13 12 30 10 46 10 11 3 11
Array Stack: 11 3 11 10 46 10 30 12 13 6 29 26 22 25 3 26 14 16 14 32 31 40 25 12 45
Linked Stack as string: 45
12
25
40
31
32
14
16
14
26
3
25
22
26
29
6
13
12
30
10
46
10
11
3
11
Linked Stack: Exception in thread "main" jsjf.exceptions.EmptyCollectionException: The list is empty.
at jsjf.LinkedList.removeLast(LinkedList.java:135)
at jsjf.LinkedListStack.pop(LinkedListStack.java:24)
at jsjf.Driver.main(Driver.java:46)
Looks like you don't increment count in your add methods for the linked list, but are using count to determine if its empty or not.

cannot be resolved to a type error when trying to implement a linked binary tree in java

So I am required to implement a decision tree, and to do that I need to properly implement a linked binary tree. In my linked binary tree code, I get many errors saying the ArrayUnorderedList<T> cannot be resolved to a type. How can I make this work? My code for the linked binary tree is below.
Also, my code also shows errors saying when I use the method max(), it is only defined for integers not parameters other types of objects, and it says the method used called recSize() is not even defined. This portion of the code was already given, so it is supposed to work. What could I be missing here?
import java.util.*;
/**
* LinkedBinaryTree implements the BinaryTreeADT interface
*
* #author Java Foundations
* #version 4.0
*/
public class LinkedBinaryTree<T> implements BinaryTreeADT<T>, Iterable<T>
{
protected BinaryTreeNode<T> root;
protected int modCount;
/**
* Creates an empty binary tree.
*/
public LinkedBinaryTree()
{
root = null;
}
/**
* Creates a binary tree with the specified element as its root.
*
* #param element the element that will become the root of the binary tree
*/
public LinkedBinaryTree(T element)
{
root = new BinaryTreeNode<T>(element);
}
/**
* Creates a binary tree with the specified element as its root and the
* given trees as its left child and right child
*
* #param element the element that will become the root of the binary tree
* #param left the left subtree of this tree
* #param right the right subtree of this tree
*/
public LinkedBinaryTree(T element, LinkedBinaryTree<T> left,
LinkedBinaryTree<T> right)
{
root = new BinaryTreeNode<T>(element);
root.setLeft(left.root);
root.setRight(right.root);
}
/**
* Returns a reference to the element at the root
*
* #return a reference to the specified target
* #throws EmptyCollectionException if the tree is empty
*/
public T getRootElement() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("LinkedBinaryTree");
return root.getElement();
}
/**
* Returns a reference to the node at the root
*
* #return a reference to the specified node
* #throws EmptyCollectionException if the tree is empty
*/
protected BinaryTreeNode<T> getRootNode() throws EmptyCollectionException
{
if (root == null)
throw new EmptyCollectionException("LinkdeBinaryTree");
return root;
}
/**
* Returns the left subtree of the root of this tree.
*
* #return a link to the left subtree fo the tree
*/
public LinkedBinaryTree<T> getLeft()
{if (root == null)
throw new EmptyCollectionException("getLeft failed - the tree is empty");
LinkedBinaryTree <T> result = new LinkedBinaryTree<T> ();
result.root = root.getLeft();
return result;// To be completed as a Programming Project
}
/**
* Returns the right subtree of the root of this tree.
*
* #return a link to the right subtree of the tree
*/
public LinkedBinaryTree<T> getRight()
{
if (root == null)
throw new EmptyCollectionException("getRight failed - the tree is empty");
LinkedBinaryTree <T> result = new LinkedBinaryTree<T> ();
result.root = root.getRight();
return result;
}
/**
* Returns true if this binary tree is empty and false otherwise.
*
* #return true if this binary tree is empty, false otherwise
*/
public boolean isEmpty()
{
return (root == null);
}
/**
* Returns the integer size of this tree.
*
* #return the integer size of the tree
*/
public int size()
{
return recSize(root);
}
/**
* Returns the height of this tree.
*
* #return the height of the tree
*/
public int getHeight()
{
return height(root);
}
/**
* Returns the height of the specified node.
*
* #param node the node from which to calculate the height
* #return the height of the tree
*/
private int height(BinaryTreeNode<T> node)
{
if (node == null)
return 0;
else
return 1 + max(height(node.getLeft()), height(node.getRight()));
}
/**
* Returns true if this tree contains an element that matches the
* specified target element and false otherwise.
*
* #param targetElement the element being sought in this tree
* #return true if the element in is this tree, false otherwise
*/
public boolean contains(T targetElement)
{
return findNode(targetElement, root) != null;
}
/**
* Returns a reference to the specified target element if it is
* found in this binary tree. Throws a ElementNotFoundException if
* the specified target element is not found in the binary tree.
*
* #param targetElement the element being sought in this tree
* #return a reference to the specified target
* #throws ElementNotFoundException if the element is not in the tree
*/
public T find(T targetElement) throws ElementNotFoundException
{
BinaryTreeNode<T> current = findNode(targetElement, root);
if (current == null)
throw new ElementNotFoundException("LinkedBinaryTree");
return (current.getElement());
}
/**
* Returns a reference to the specified target element if it is
* found in this binary tree.
*
* #param targetElement the element being sought in this tree
* #param next the element to begin searching from
*/
private BinaryTreeNode<T> findNode(T targetElement,
BinaryTreeNode<T> next)
{
if (next == null)
return null;
if (next.getElement().equals(targetElement))
return next;
BinaryTreeNode<T> temp = findNode(targetElement, next.getLeft());
if (temp == null)
temp = findNode(targetElement, next.getRight());
return temp;
}
/**
* Returns a string representation of this binary tree showing
* the nodes in an inorder fashion.
*
* #return a string representation of this binary tree
*/
public String toString()
{
ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
inOrder(root, tempList);
return tempList.toString();
}
/**
* Returns an iterator over the elements in this tree using the
* iteratorInOrder method
*
* #return an in order iterator over this binary tree
*/
public Iterator<T> iterator()
{
return iteratorInOrder();
}
/**
* Performs an inorder traversal on this binary tree by calling an
* overloaded, recursive inorder method that starts with
* the root.
*
* #return an in order iterator over this binary tree
*/
public Iterator<T> iteratorInOrder()
{
ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
inOrder(root, tempList);
return new TreeIterator(tempList.iterator());
}
/**
* Performs a recursive inorder traversal.
*
* #param node the node to be used as the root for this traversal
* #param tempList the temporary list for use in this traversal
*/
protected void inOrder(BinaryTreeNode<T> node,
ArrayUnorderedList<T> tempList)
{
if (node != null)
{
inOrder(node.getLeft(), tempList);
tempList.addToRear(node.getElement());
inOrder(node.getRight(), tempList);
}
}
/**
* Performs an preorder traversal on this binary tree by calling
* an overloaded, recursive preorder method that starts with
* the root.
*
* #return a pre order iterator over this tree
*/
public Iterator<T> iteratorPreOrder()
{
ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
preOrder(root, tempList);
return new TreeIterator(tempList.iterator());
}
/**
* Performs a recursive preorder traversal.
*
* #param node the node to be used as the root for this traversal
* #param tempList the temporary list for use in this traversal
*/
protected void preOrder(BinaryTreeNode<T> node,
ArrayUnorderedList<T> tempList)
{
if (node != null)
{
tempList.addToRear(node.getElement());
preOrder(node.getLeft(), tempList);
preOrder(node.getRight(), tempList);
}
}
/**
* Performs an postorder traversal on this binary tree by calling
* an overloaded, recursive postorder method that starts
* with the root.
*
* #return a post order iterator over this tree
*/
public Iterator<T> iteratorPostOrder()
{
ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
postOrder(root, tempList);
return new TreeIterator(tempList.iterator());
}
/**
* Performs a recursive postorder traversal.
*
* #param node the node to be used as the root for this traversal
* #param tempList the temporary list for use in this traversal
*/
protected void postOrder(BinaryTreeNode<T> node,
ArrayUnorderedList<T> tempList)
{
if (node != null)
{
tempList.addToRear(node.getElement());
postOrder(node.getLeft(), tempList);
postOrder(node.getRight(), tempList);
}
}
/**
* Performs a levelorder traversal on this binary tree, using a
* templist.
*
* #return a levelorder iterator over this binary tree
*/
public Iterator<T> iteratorLevelOrder()
{
ArrayUnorderedList<BinaryTreeNode<T>> nodes = new ArrayUnorderedList<BinaryTreeNode<T>>();
ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
BinaryTreeNode<T> current;
nodes.addToRear(root);
while (!nodes.isEmpty())
{
current = nodes.removeFirst();
if (current != null)
{
tempList.addToRear(current.getElement());
if (current.getLeft() != null)
nodes.addToRear(current.getLeft());
if (current.getRight() != null)
nodes.addToRear(current.getRight());
}
else
tempList.addToRear(null);
}
return new TreeIterator(tempList.iterator());
}
/**
* Inner class to represent an iterator over the elements of this tree
*/
private class TreeIterator implements Iterator<T>
{
private int expectedModCount;
private Iterator<T> iter;
/**
* Sets up this iterator using the specified iterator.
*
* #param iter the list iterator created by a tree traversal
*/
public TreeIterator(Iterator<T> iter)
{
this.iter = iter;
expectedModCount = modCount;
}
/**
* Returns true if this iterator has at least one more element
* to deliver in the iteration.
*
* #return true if this iterator has at least one more element to deliver
* in the iteration
* #throws ConcurrentModificationException if the collection has changed
* while the iterator is in use
*/
public boolean hasNext() throws ConcurrentModificationException
{
if (!(modCount == expectedModCount))
throw new ConcurrentModificationException();
return (iter.hasNext());
}
/**
* Returns the next element in the iteration. If there are no
* more elements in this iteration, a NoSuchElementException is
* thrown.
*
* #return the next element in the iteration
* #throws NoSuchElementException if the iterator is empty
*/
public T next() throws NoSuchElementException
{
if (hasNext())
return (iter.next());
else
throw new NoSuchElementException();
}
/**
* The remove operation is not supported.
*
* #throws UnsupportedOperationException if the remove operation is called
*/
public void remove()
{
throw new UnsupportedOperationException();
}
}
}

Linked List Implementation with Interface

What i need to do in this assignment, is to implement the given methods from the interface. If you could check and give me some feedback on what i've delivered so far, would be very much appreciated.
I am still missing one method unfortunately, public T getNext8thElementOf(int pos). Does anyone has an idea on how to implement it?
/**
* A simple list interface
* #param <T> The type of list element
*/
public interface ISpeedList<T> {
/**
* Returns the current number of elements in the list
*
* #return Current number of elements in the list
*/
public int size();
/**
* Inserts an element at the beginning of the list
*
* #param item Item to be inserted
*/
public void prepend(T item);
/**
* Returns the element at the specified position in the list
*
* #param pos The position of the element in the list starting from 0
*
* #return The specified element in the list
*
* #throws IndexOutOfBoundsException If the requested element is out of
* range
*/
public T getElementAt(int pos);
/**
* Returns the next 8th element of the specified element in the list
*
* #param pos The position of the specified element in the list starting
* from 0
*
* #return The next 8th element of the specified element
*
* #throws IndexOutOfBoundsException If the requested element is out of
* range
*/
public T getNext8thElementOf(int pos);
}
public class SpeedList<T> implements ISpeedList<T> {
/**
* Doubly-linked node class, completely private to the List class,
* as clients don't care about the implementation of the list.
*/
private class Node {
T item;
Node next;
Node previous;
Node(T item, Node next, Node previous) {
this.item = item;
this.next = next;
this.previous = previous;
}
}
/**
* The list itself maintains only a reference to its "header" node.
* The header is a node that does not store any data. Its 'next'
* field points to the first item in the list and its 'previous'
* field points to the last item. This makes all insertions and
* deletions uniform, even at the beginning and the end of the list!
*/
private Node header = new Node(null, null, null);
/**
* The number of items in the list, stored to make size() O(1).
*/
private int size = 0;
/**
* Returns the number of items in the list.
*/
#Override
public int size() {
return size;
}
/**
* Inserts <code>item</code> as the new first item.
*/
#Override
public void prepend(T item) {
addBefore(item, header.next);
}
/**
* Returns the item at the given index position.
*
* #throws IndexOutOfBoundsException
* if index not in [0,size).
*/
#Override
public T getElementAt(int pos) {
return nodeAt(pos).item;
}
#Override
public T getNext8thElementOf(int pos) {
// TODO Auto-generated method stub
return null;
}
//
// PRIVATE HELPER METHODS
//
private Node nodeAt(int pos) {
if (pos < 0 || pos >= size) {
throw new IndexOutOfBoundsException(pos + " for size " + size);
}
Node n = header;
for (int i = 0; i <= pos; i++) {
n = n.next;
}
return n;
}
private void addBefore(T o, Node n) {
Node newNode = new Node(o, n, n.previous);
newNode.previous.next = newNode;
newNode.next.previous = newNode;
size++;
}
}
Is that what you want?
#Override
public T getNext8thElementOf(int pos) {
int eight = 8;
Node nodo = this.nodeAt(pos);
while(eight > 0) {
eight--;
nodo = nodo.next;
}
return nodo.item;
}

Checking if a linked List a Palindrome using a Node Class

I have a project issue where I need to check if a linked list is a palindrome, the same forward as it is backward. I was given a class titled a Node class that contains specific information that I am not allowed to change so I must use what I have in that class and cant write more there to help me. My problem is I can get through the list, but I don't know how to go backwards through and compare values.
Disclaimer: I don't want a complete answer just tips/ideas!
Here is the class I can't change and have to work with:
class Node {
/**
* The value stored in the node.
*/
public Character value;
/**
* A pointer to the next node in
* the list.
*/
public Node next;
/**
* A pointer to the previous node
* in the list. (Note: having this
* pointer makes this a "doubly linked"
* list.
*/
public Node prev;
/**
* A constructor to set up a
* node with a value.
*
* #param value the value to put in the node
*/
public Node(Character value) {
this.value = value;
}
/**
* A constructor to set up a
* node with a value and a pointer
* to the previous node (so I can
* append items easily in my test
* code).
*
* #param value the value to put in the node
* #param prev the previous node in the list
*/
public Node(Character value, Node prev) {
this.value = value;
this.prev = prev;
}
/**
* Sets the next node in the list.
*
* #param n the next node in the list
*/
public void setNext(Node n) {
next = n;
}
/**
* Sets the previous node in the list.
*
* #param n the previous node in the list
*/
public void setPrev(Node n) {
prev = n;
}
}
So far this is what I have and I am just trying whatever I can:
public static boolean listPalindrome(Node input) {
if(input == null)
{
throw new IllegalArgumentException();
}
int count = 0;
while(input.next != null)
{
count++;
}
Node holder = new Node(null, input.next);
for(int i = 0;i<count;i++)
{
if(input.next != null)
{
break;
}
else
{
}
}
return false; //replace this
}
Please help!
Thanks!
You have an infinite loop with count++. input.next will always be non-null since you aren't updating the value of input in the loop.
If you were to get past that there is no reason to declare a new Node in your code. The loop you have also will break on the first iteration assuming the linked list is a size > 0, you should instead check input.next == null but put it at the end of the loop.
Also noting that your a GMU student it would be an honor code violation for me to help you any more than this ;)

Java- Error concerning Generics

I have a class & and inner class that work together to make a linked list. The data held by the nodes, is Generic, and I somewhere along the line of my code I screwed up involving generics but I don't know where. I get 5 errors, the first one says "type LinkedList does not take parameters" which i think is weird because LinkedList is just an interface that i'm implementing. The rest of them are phrased "Object cannot be converted to E". I marked where they're occurring. I figured my assignment was right I don't know what's wrong. If you'd like me to post the interface LinkedList let me know.
public class SinglyLinkedList<E> implements LinkedList<E> {
private Node head;
private Node tail;
private int size;
public SinglyLinkedList(){
this.head = null;
this.tail = null;
this.size = 0;
}
public E get(int index) {
//Hold the data of the cur node
//and keep iterating until you hit the
//right index
int hit = 0;
Node pos = this.head;
E found = this.head.data;//////ERROR HERE
while (hit < index) {
pos = pos.next;
found = pos.data;//////ERROR HERE
hit++;
}
return found;
}
public void add (E data) {
Node last = new Node(data, null);
//If the list is empty.
if (this.head == null) {
this.head = last;
}
//Make the cur tail's next the new node
//and set the new node as the new tail.
this.tail.next = last;
this.tail = last;
this.size++;
}
public boolean add(int index, E data) {
//Cannot add if index is not valid
if ((index >= this.size) || (index < 0) ) {
return false;
}
//If index is 0, add at beginning
Node insert = new Node(data, null);
if (index == 0) {
insert.next = this.head;
this.head = insert;
this.size++;
return true;
} else {
//Else, go until you reach desired index then
//Set whatever was at that index to the new Node's
//next value.
int hit = 1;
Node cur = this.head;
while (hit < index) {
cur = cur.next;
hit++;
}
insert.next = cur;
this.size++;
return false;
}
}
public void addAll(Collection<? extends E> c) {
for (E item: c) {
this.add(item);
}
}
public boolean contains(E data) {
int hit = 0;
Node cur = this.head;
Node move;
while (hit < this.size) {
if (data.equals(cur.data)) {
return true;
} else {
move = cur.next;
cur = move;
}
}
return false;
}
public boolean containsAll(Collection<? extends E> c) {
for (E item: c) {
if (!(this.contains(item))) {
return false;
}
}
return true;
}
public boolean remove(E data) {
Node prev = this.head;
Node cur = this.head.next;
int hit = 1;
if (!(this.contains(data))) {
return false;
} else if (data.equals(head.data)) {
this.head = cur;
return true;
this.size--;
}
while (hit < this.size) {
if (data.equals(cur.data)) {
prev.next = cur.next;
return true;
this.size--;
}
hit++;
}
}
public E remove(int index) {
int hit = 1;
E data;
Node prev = this.head;
Node cur = this.head.next;
if (index == 0) {
data = head.data; //////ERROR HERE
this.head = cur;
return data;
this.size--;
} else {
while (hit < this.size) {
if ( hit == index) {
data = cur.data; //////ERROR HERE
prev.next = cur.next;
return data;
this.size--;
} else {
prev = cur;
cur = prev.next;
hit++;
}
}
}
}
public boolean removeAll(Collection<? extends E> c) {
int prevSize = this.size;
for (E item: c) {
remove(item);
}
return (prevSize < this.size);
}
public int size() {
return this.size;
}
public boolean isEmpty() {
return (this.head == null);
}
public void clear() {
this.head = null;
this.tail = null;
size = 0;
}
private class Node<E> {
private E data;
private Node next;
private Node(E data, Node next) {
this.data = data;
this.next = next;
}
}
}
This is the interface class
/**
* The LinkedList interface defines a set of standard operations that are
* typically associated with linked list, such as adding, removing, and checking
* to see if the list contains an item.
*
* Note that while your linked list implementation should make use of a Node
* class, the methods below take in and return instances of the generic type,
* not the nodes themselves.
*
* #author Justin Nieto
* #version 1.1
* #param <E> the type of the elements to store in the linked list
*/
public interface LinkedList<E> {
/**
* Returns the element in the linked list at the specified index.
* Does not change the contents of the list in any way. If the given
* index is negative or greater than the maximum possible index, returns
* null.
*
* #param index of element to be retrieved
* #return the element at the given index or null if index out of bounds
*/
E get(int index);
/**
* Adds the specified piece of data to the end of the linked list.
* This method should execute in O(1) (constant) time. This means that
* you should not iterate over the whole list to add the item to the end
* (we will check for this).
*
* #param data the object to be added to the linked list
*/
void add(E data);
/**
* Adds given piece of data to the linked list at the given index.
* All items that were originally at the index or after the index should
* be shifted down by one. If the index specified is not valid, returns
* false. Otherwise, returns true.
*
* If the index specified is 0 or if it is one larger than the maximum
* current index (ie if index is equal to the size of the linked list),
* then this method should execute in O(1) (constant) time. This means that
* you should not iterate over the entire list to add the element, as it
* is unnecessary to do so.
*
* #param index the index at which to add the item
* #param data the item to be added to te linked list
* #return true if the data could be added at the given index
*/
boolean add(int index, E data);
/**
* Adds each element in the Collection to the end of the linked list.
*
* #param c the collection of items to add to the end of the linked list
*/
void addAll(Collection<? extends E> c);
/**
* Determines whether or not the given piece of data is in the linked list.
*
* #param data the item to check
* #return true if the linked list contains the item, false otherwise
*/
boolean contains(E data);
/**
* Determines whether or not every element of the given Collection is
* in the linked list.
*
* #param c the Collection of elements to check
* #return true if list contains every element in the Collection
*/
boolean containsAll(Collection<? extends E> c);
/**
* Finds the first element of the list equal to the given piece of data
* and removes it from the list. Returns false if the given piece of data
* is not in the list and therefore cannot be removed.
*
* #param data the piece of data to be removed from the list
* #return true if the item was removed, false if list does not contain it
*/
boolean remove(E data);
/**
* Removes and returns the item in the list at the given index.
* All items at indices after the given index are shifted down by one.
*
* #param index the index of the item to remove from the linked list
* #return the removed item
*/
E remove(int index);
/**
* Removes each element in the given collection from the linked list.
*
* #param c the Collection of items to remove
* #return true if each element in the Collection was removed.
*/
boolean removeAll(Collection<? extends E> c);
/**
* Returns the number of elements in the linked list. This method
* should execute in O(1) (constant) time. This means that you should not
* iterate over the entire list to count the number of items, but rather
* you should maintain a size variable that you can just return here.
*
* #return the number of elements in the linked list
*/
int size();
/**
* Returns true if the linked list has no elements.
*
* #return true if the list is empty, false otherwise
*/
boolean isEmpty();
/**
* Removes all elements from the list. After calling this method,
* the isEmpty method should return true.
*/
void clear();
}
Node is an inner class of SinglyLinkedList, so it already shares the type parameter E. Therefore you could make Node a non-generic class.
private class Node {
private E data;
private Node next;
private Node(E data, Node next) {
this.data = data;
this.next = next;
}
}
That removes many of the problems, because before Node was a generic class and you were using Node without a type parameter.
An alternative solution is to make Node a static nested class. This means that an instance of Node does not belong to an instance of SinglyLinkedList. Because of this, a static version of Node would need its own type parameter.
private static class Node<E> {
private E data;
private Node<E> next;
private Node(E data, Node<E> next) {
this.data = data;
this.next = next;
}
}
Notice that in this version, Node is generic, so I had to add <E> after it every time it appeared. If you go with the static approach, you will have to add <E> after every occurrence of Node in the rest of the class. There are lots of these.
Finally, the message "LinkedList does not take parameters" is self-explanatory. Presumably it says interface LinkedList when it should say interface LinkedList<E>.

Categories