I am having trouble implementing my linkedlist. When I run the code it just doesn't produce anything and keeps running.
UPDATE: I edited the toString to look like this:
LinearNode<T> current = head;
String result = "";
while (current != null) {
result = result + (current.getElement()).toString() + "\n";
current = current.next;
}
return result;
Now it works and prints so the error must be in the iterator.
Here is LinkedList class:
import java.util.*;
import java.util.Iterator;
public class LinkedList10Handout<T> implements ListADT<T>
{
protected int count;
protected LinearNode<T> head, tail, current, previous;
//===========================================================
// Creates an empty list.
//===========================================================
public LinkedList10Handout()
{
count = 0;
head = tail = null;
current = null;
} // constructor List
/**************************************************************
// Precondition: head is the head reference of a linked list.
// The list might be empty or it might be non-empty.
// PostCondition: One node has been added to the list.
//**************************************************************/
public void InsertFirst(T addData)//*********************************
{
LinearNode<T> newnode = new LinearNode<T>(addData, null);
if (isEmpty())
{
head = newnode;
tail = newnode;
}
else
{
newnode.next = head;
head = newnode;
}
if (current == head.next)
previous = head;
count++;
// LIST REQUIREMENTS:
// if the list was initially empty, put tail on the list
//if current was initially at the first node, we must now put previous behind it
}
/**************************************************************
// this method calls insertAfter method after it finds the node it wants to insert a node after
/**************************************************************/
public boolean Insert(T target, T addData)//*************************
{
LinearNode<T> find = find(target);
boolean found = false;
if (find != null)
{
found = true;
insertAfter(addData);
}
// calls find
// if target is found
//the method calls insertafter(addData) WHICH DOES THE ACTUAL INSERT
return found; // this is just here to allow it to compile
}
/*****************************************************************
// inserts a node at the end of the tail
// CHECKS IF THE LIST IS EMPTY FIRST AND CALLS INSERTFIRST
// ELSE INSERTS ON THE END OF THE LIST
/******************************************************************/
public void insertTail(T target )//******************
{
if (isEmpty())
InsertFirst(target);
else
{
LinearNode<T> newnode = new LinearNode<T> (target, null);
tail.next = newnode;
tail = newnode;
}
count++;
// INSERT NODE ON THE TAIL
}
//===========================================================
// Precondition: The List is not empty
// Removes the first element in the list and returns a reference
// to it. Throws an EmptyListException if the list is empty.
//===========================================================
public T removeFirst() throws EmptyCollectionException //*************************
{
LinearNode<T> removed = head;
if (isEmpty())
throw new EmptyCollectionException("List");
else
head = head.next;
// delete the first node
// return the data;
count--;
return removed.getElement(); // this is to get it to compile - change it
} // method removeFirst
/**************************************************************
*Inserts node with newData after the node current points to. The current
*node is the same AFTER THE NODE IS INSERTED
*Should not be used with an empty list. Should not be
*used when the current node IS NULL.
/*******Before insertAfter is called current must be pointing to A node on the list
**************************************************************/
public void insertAfter(T newData)//*****************
{
LinearNode<T> newnode = new LinearNode<T>(newData, current.next);
current.next = newnode;
if (current == tail)
tail.next = newnode;
count++;
}// close method
/*********************************************************
Finds the first node containing the target data, and returns a
reference to that node. If key is not in the list, null is returned.
*********************************************************/
public LinearNode<T> find(T target) // WE DID THIS IN CLASS
{
if (!isEmpty())
{
for (current = head; current.getElement() != target; current = current.next)
// set up a for loop with current initialed to point to what head is pointing to.
{
previous = previous.next;
return current;
}
// inside the loop compare the element current is presently pointing to, to target
// if they are equal return current.element
// advance previous so that it follows current
}
return null;
} // close method
//===========================================================
// Precondition: Throws an EmptyListException if the list is empty.
// Throws a NoSuchElementException if the specified element is not found on the list.
// PostCondition:Removes the first instance of the specified element from the
// list if it is found in the list and returns a reference to it.
// SEVERAL SITUATIONS MUST BE HANDLED AS LISTED BELOW
//===========================================================
public T remove (T target) throws EmptyCollectionException, ElementNotFoundException //**************
{
if (isEmpty())
throw new EmptyCollectionException ("List");
LinearNode<T> find = find(target);
if (find == null)
throw new ElementNotFoundException ("List");
LinearNode<T> removed = new LinearNode<T> (target, null);
// MAKE SURE TARGET IS ON THE LIST
// conditions you have to address:
//(1) if there is only one node on the list
if (size() == 1)
head = tail = null;
else
if (find == head)
removeFirst();
else
if (find == tail)
removeLast();
else
{
previous.next = current.next;
current = current.next;
}
//(2) if (current points to head node and there is more than one node on the list
// (3) else if current equals tail - we are at the last node - HINT - USE PREVIOUS
//(4)IF NONE OF THE ABOVE IS TRUE we are somewhere in the middle of the list
// where current is pointing to node to be deleted
// and previous is right behind it - delete THAT node
count--;
// return the removed element
return removed.getElement();// this is just to get it to compile
} // method remove
//===========================================================
// Removes the last element in the list and returns a reference
// to it. Throws an EmptyListException if the list is empty.
// CALLS FIND TO LOCATE CURRENT ON THE TAIL NODE
// DETERMINES FIRST IF THE THERE IS ONLY ONE NODE AND THEN DELETES THAT
//===========================================================
public T removeLast() throws EmptyCollectionException //*******************************
{
// remove last node
if (isEmpty())
throw new EmptyCollectionException("List");
find(tail.getElement());
previous.next = current.next;
current = current.next;
count--;
return current.getElement();// was result
} // method removeLast
//===========================================================
// Finds the first instance of the specified element from the
// list if it is found in the list and returns true.
// Returns false otherwise. Calls the find method to find target
//===========================================================
public boolean contains (T targetElement) throws EmptyCollectionException //****************
{
if (isEmpty())
throw new EmptyCollectionException ("List");
boolean found = false;
LinearNode<T> find = find(targetElement);
if (find != null)
found = true;
return found;
} // method contains
//===========================================================
// Returns true if the list is empty and false otherwise
//===========================================================
public boolean isEmpty()
{
return (count == 0);
} // method isEmpty
//===========================================================
// Returns the number of elements in the list.
//===========================================================
public int size() //*******************
{
// put in code
return count; // CHANGE THIS< IT IS TO GET IT TO COMPILE
} // method size
//===========================================================
// Returns ...
//===========================================================
public Iterator<T> iterator()
{
return new LinkedIterator<T>(head, count);
} // method elements
//===========================================================
// Returns a string representation of the list.
//===========================================================
public String toString() //*******************************
{
String result = "";
Iterator<T> iter = iterator();
while (iter.hasNext())
{
result += iter + "\n";
// traverse the list and accumulate the elements in result as you did in DatingService with hobbies
}
return result;
} // method toString
//===========================================================
// Returns the first element of the list.
//===========================================================
public T first() //***************************
{
return head.getElement(); // this is to get it to compile - change it
} // method firstElement
//===========================================================
// Returns the last element of the list.
//===========================================================
public T last()//********************
{
// put in code
return tail.getElement(); // this is to get it to compile - change it
} // method lastElement
} // class LinkedList
class EmptyCollectionException extends RuntimeException
{
//-----------------------------------------------------------------
// Sets up this exception with an appropriate message.
//-----------------------------------------------------------------
public EmptyCollectionException (String collection)
{
super ("The " + collection + " is empty.");
}
}
class LinkedIterator<T> implements Iterator
{
private int count; // the number of elements in the collection
private LinearNode<T> current; // the current position
//-------------------------------------------------------------
// Sets up this iterator using the specified items.
//-------------------------------------------------------------
public LinkedIterator (LinearNode<T> collection, int size)
{
current = collection;
count = size;
}
//-------------------------------------------------------------
// Returns true if this iterator has at least one more element
// to deliver in the iteraion.
//-------------------------------------------------------------
public boolean hasNext()
{
return (current!= null);
}
//-------------------------------------------------------------
// Returns the next element in the iteration. If there are no
// more elements in this itertion, a NoSuchElementException is
// thrown.
//-------------------------------------------------------------
public T next() //*********************************************
{
if (! hasNext())
throw new NoSuchElementException();
// put in code
return current.next.getElement(); // this is to get it to compile - change it
}
//-------------------------------------------------------------
// The remove operation is not supported.
//-------------------------------------------------------------
public void remove() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
}
class ElementNotFoundException extends RuntimeException
{
//-----------------------------------------------------------------
// Sets up this exception with an appropriate message.
//-----------------------------------------------------------------
public ElementNotFoundException (String collection)
{
super ("The target element is not in this " + collection);
}
}
interface ListADT<T>
{
// Removes and returns the first element from this list
public T removeFirst ();
// Removes and returns the specified element from this list
public T remove (T element);
// Returns a reference to the first element on this list
public T first ();
// Returns a reference to the last element on this list
public T last ();
// Returns true if this list contains the specified target element
public boolean contains (T target);
// Returns true if this list contains no elements
public boolean isEmpty();
// Returns the number of elements in this list
public int size();
// Returns an iterator for the elements in this list
public Iterator<T> iterator();
// Returns a string representation of this list
public String toString();
}
//************************************************************
// LinearNode.java
//
// Represents a node in a linked list.
//************************************************************
// YOU MUST PUT IN MORE SETTERS and GETTERS
class LinearNode<T>
{
public LinearNode<T> next;
private T element;
//---------------------------------------------------------
// Creates an empty node.
//---------------------------------------------------------
public LinearNode()
{
next = null;
element = null;
}
//---------------------------------------------------------
// Creates a node storing the specified element.
//---------------------------------------------------------
public LinearNode (T elem)
{
element = elem;
}
//---------------------------------------------------------
// Creates a node storing the specified element and link
//---------------------------------------------------------
public LinearNode (T elem, LinearNode<T> next)
{
element = elem;
this.next = next;
}
// put in setter and getters for next and element
//---------------------------------------------------------
// Returns the element stored in this node.
//---------------------------------------------------------
public T getElement()
{
return element;
}
//---------------------------------------------------------
// Set the element stored in this node.
//---------------------------------------------------------
public void setElement (T data)
{
element = data;
}
//---------------------------------------------------------
// Returns the next node.
//---------------------------------------------------------
public LinearNode<T> next()
{
return next;
}
//---------------------------------------------------------
// Sets the next node.
//---------------------------------------------------------
public void next (LinearNode<T> node)
{
next = node;
}
}
Here is the tester class:
public class ProjectTest10
{
public static void main(String args[])
{
String B = "B";
String E = "E";
String J = "J";
LinkedList10Handout list = new LinkedList10Handout();
// Code a linked list as pictured in the project specification which contains String B, E and J and perform the following operations.
// After each operation print out the list:
// do the appropriate System.out printlns for each operation
// 1. insert B into the list
LinearNode NodeB = new LinearNode (B);
list.InsertFirst(NodeB.getElement());
System.out.println("This is a list with B " + list);
System.out.println();
}}
There is more to tester class but doing just this stops it. The problem is at insertFirst, I believe since when I delete that line it prints out the line without the node obviously.
in your LinkedList10Handout, check your toString() function..
I think you want something like:
while (iter.hasNext())
{
result += iter.next() + "\n";
}
but, your iterator() function also has a problem..
you need to define your iterator like:
new LinkedIterator<T>(beforeHead, count);
instead of:
new LinkedIterator<T>(head, count);
to do this, you'll need to modify your code a bit.. good luck =)
UPDATE: just make an additional root node that will always exist (even though the list is empty) as the start point of the iterator..
ADDITION:
okay, here's an example:
import java.util.*;
import java.util.Iterator;
public class LinkedList10Handout<T> implements ListADT<T> {
protected int count;
protected LinearNode<T> rootNode;
protected LinearNode<T> head, tail, current, previous;
//===========================================================
// Creates an empty list.
//===========================================================
public LinkedList10Handout() {
rootNode = new LinearNode();
count = 0;
head = tail = null;
current = null;
} // constructor List
/**
* ************************************************************
* // Precondition: head is the head reference of a linked list. // The list
* might be empty or it might be non-empty. // PostCondition: One node has
* been added to the list.
//*************************************************************
*/
public void InsertFirst(T addData)//*********************************
{
LinearNode<T> newnode = new LinearNode<T>(addData, null);
if (isEmpty()) {
head = newnode;
rootNode.next = head;
tail = newnode;
} else {
newnode.next = head;
head = newnode;
}
if (current == head.next) {
previous = head;
}
count++;
// LIST REQUIREMENTS:
// if the list was initially empty, put tail on the list
//if current was initially at the first node, we must now put previous behind it
}
/**
* ************************************************************
* // this method calls insertAfter method after it finds the node it wants
* to insert a node after
/*************************************************************
*/
public boolean Insert(T target, T addData)//*************************
{
LinearNode<T> find = find(target);
boolean found = false;
if (find != null) {
found = true;
insertAfter(addData);
}
// calls find
// if target is found
//the method calls insertafter(addData) WHICH DOES THE ACTUAL INSERT
return found; // this is just here to allow it to compile
}
/**
* ***************************************************************
* // inserts a node at the end of the tail // CHECKS IF THE LIST IS EMPTY
* FIRST AND CALLS INSERTFIRST // ELSE INSERTS ON THE END OF THE LIST
/*****************************************************************
*/
public void insertTail(T target)//******************
{
if (isEmpty()) {
InsertFirst(target);
} else {
LinearNode<T> newnode = new LinearNode<T>(target, null);
tail.next = newnode;
tail = newnode;
}
count++;
// INSERT NODE ON THE TAIL
}
//===========================================================
// Precondition: The List is not empty
// Removes the first element in the list and returns a reference
// to it. Throws an EmptyListException if the list is empty.
//===========================================================
public T removeFirst() throws EmptyCollectionException //*************************
{
LinearNode<T> removed = head;
if (isEmpty()) {
throw new EmptyCollectionException("List");
} else {
head = head.next;
}
// delete the first node
// return the data;
count--;
return removed.getElement(); // this is to get it to compile - change it
} // method removeFirst
/**
* ************************************************************
* Inserts node with newData after the node current points to. The current
* node is the same AFTER THE NODE IS INSERTED Should not be used with an
* empty list. Should not be used when the current node IS NULL.
* /*******Before insertAfter is called current must be pointing to A node
* on the list
*************************************************************
*/
public void insertAfter(T newData)//*****************
{
LinearNode<T> newnode = new LinearNode<T>(newData, current.next);
current.next = newnode;
if (current == tail) {
tail.next = newnode;
}
count++;
}// close method
/**
* *******************************************************
* Finds the first node containing the target data, and returns a reference
* to that node. If key is not in the list, null is returned.
********************************************************
*/
public LinearNode<T> find(T target) // WE DID THIS IN CLASS
{
if (!isEmpty()) {
for (current = head; current.getElement() != target; current = current.next) // set up a for loop with current initialed to point to what head is pointing to.
{
previous = previous.next;
return current;
}
// inside the loop compare the element current is presently pointing to, to target
// if they are equal return current.element
// advance previous so that it follows current
}
return null;
} // close method
//===========================================================
// Precondition: Throws an EmptyListException if the list is empty.
// Throws a NoSuchElementException if the specified element is not found on the list.
// PostCondition:Removes the first instance of the specified element from the
// list if it is found in the list and returns a reference to it.
// SEVERAL SITUATIONS MUST BE HANDLED AS LISTED BELOW
//===========================================================
public T remove(T target) throws EmptyCollectionException, ElementNotFoundException //**************
{
if (isEmpty()) {
throw new EmptyCollectionException("List");
}
LinearNode<T> find = find(target);
if (find == null) {
throw new ElementNotFoundException("List");
}
LinearNode<T> removed = new LinearNode<T>(target, null);
// MAKE SURE TARGET IS ON THE LIST
// conditions you have to address:
//(1) if there is only one node on the list
if (size() == 1) {
head = tail = null;
} else if (find == head) {
removeFirst();
} else if (find == tail) {
removeLast();
} else {
previous.next = current.next;
current = current.next;
}
//(2) if (current points to head node and there is more than one node on the list
// (3) else if current equals tail - we are at the last node - HINT - USE PREVIOUS
//(4)IF NONE OF THE ABOVE IS TRUE we are somewhere in the middle of the list
// where current is pointing to node to be deleted
// and previous is right behind it - delete THAT node
count--;
// return the removed element
return removed.getElement();// this is just to get it to compile
} // method remove
//===========================================================
// Removes the last element in the list and returns a reference
// to it. Throws an EmptyListException if the list is empty.
// CALLS FIND TO LOCATE CURRENT ON THE TAIL NODE
// DETERMINES FIRST IF THE THERE IS ONLY ONE NODE AND THEN DELETES THAT
//===========================================================
public T removeLast() throws EmptyCollectionException //*******************************
{
// remove last node
if (isEmpty()) {
throw new EmptyCollectionException("List");
}
find(tail.getElement());
previous.next = current.next;
current = current.next;
count--;
return current.getElement();// was result
} // method removeLast
//===========================================================
// Finds the first instance of the specified element from the
// list if it is found in the list and returns true.
// Returns false otherwise. Calls the find method to find target
//===========================================================
public boolean contains(T targetElement) throws EmptyCollectionException //****************
{
if (isEmpty()) {
throw new EmptyCollectionException("List");
}
boolean found = false;
LinearNode<T> find = find(targetElement);
if (find != null) {
found = true;
}
return found;
} // method contains
//===========================================================
// Returns true if the list is empty and false otherwise
//===========================================================
public boolean isEmpty() {
return (count == 0);
} // method isEmpty
//===========================================================
// Returns the number of elements in the list.
//===========================================================
public int size() //*******************
{
// put in code
return count; // CHANGE THIS< IT IS TO GET IT TO COMPILE
} // method size
//===========================================================
// Returns ...
//===========================================================
public Iterator<T> iterator() {
return new LinkedIterator<T>(rootNode, count);
} // method elements
//===========================================================
// Returns a string representation of the list.
//===========================================================
public String toString() //*******************************
{
String result = "";
Iterator<T> iter = iterator();
while (iter.hasNext()) {
result += iter.next().toString() + "\n";
// traverse the list and accumulate the elements in result as you did in DatingService with hobbies
}
return result;
} // method toString
//===========================================================
// Returns the first element of the list.
//===========================================================
public T first() //***************************
{
return head.getElement(); // this is to get it to compile - change it
} // method firstElement
//===========================================================
// Returns the last element of the list.
//===========================================================
public T last()//********************
{
// put in code
return tail.getElement(); // this is to get it to compile - change it
} // method lastElement
} // class LinkedList
class EmptyCollectionException extends RuntimeException {
//-----------------------------------------------------------------
// Sets up this exception with an appropriate message.
//-----------------------------------------------------------------
public EmptyCollectionException(String collection) {
super("The " + collection + " is empty.");
}
}
class LinkedIterator<T> implements Iterator {
private int count; // the number of elements in the collection
private LinearNode<T> current; // the current position
//-------------------------------------------------------------
// Sets up this iterator using the specified items.
//-------------------------------------------------------------
public LinkedIterator(LinearNode<T> collection, int size) {
current = collection;
count = size;
}
//-------------------------------------------------------------
// Returns true if this iterator has at least one more element
// to deliver in the iteraion.
//-------------------------------------------------------------
public boolean hasNext() {
return (current.next != null);
}
//-------------------------------------------------------------
// Returns the next element in the iteration. If there are no
// more elements in this itertion, a NoSuchElementException is
// thrown.
//-------------------------------------------------------------
public T next() //*********************************************
{
if (!hasNext()) {
throw new NoSuchElementException();
}
current = current.next;
// put in code
return current.getElement(); // this is to get it to compile - change it
}
//-------------------------------------------------------------
// The remove operation is not supported.
//-------------------------------------------------------------
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
}
class ElementNotFoundException extends RuntimeException {
//-----------------------------------------------------------------
// Sets up this exception with an appropriate message.
//-----------------------------------------------------------------
public ElementNotFoundException(String collection) {
super("The target element is not in this " + collection);
}
}
interface ListADT<T> {
// Removes and returns the first element from this list
public T removeFirst();
// Removes and returns the specified element from this list
public T remove(T element);
// Returns a reference to the first element on this list
public T first();
// Returns a reference to the last element on this list
public T last();
// Returns true if this list contains the specified target element
public boolean contains(T target);
// Returns true if this list contains no elements
public boolean isEmpty();
// Returns the number of elements in this list
public int size();
// Returns an iterator for the elements in this list
public Iterator<T> iterator();
// Returns a string representation of this list
public String toString();
}
//************************************************************
// LinearNode.java
//
// Represents a node in a linked list.
//************************************************************
// YOU MUST PUT IN MORE SETTERS and GETTERS
class LinearNode<T> {
public LinearNode<T> next;
private T element;
//---------------------------------------------------------
// Creates an empty node.
//---------------------------------------------------------
public LinearNode() {
next = null;
element = null;
}
//---------------------------------------------------------
// Creates a node storing the specified element.
//---------------------------------------------------------
public LinearNode(T elem) {
element = elem;
}
//---------------------------------------------------------
// Creates a node storing the specified element and link
//---------------------------------------------------------
public LinearNode(T elem, LinearNode<T> next) {
element = elem;
this.next = next;
}
// put in setter and getters for next and element
//---------------------------------------------------------
// Returns the element stored in this node.
//---------------------------------------------------------
public T getElement() {
return element;
}
//---------------------------------------------------------
// Set the element stored in this node.
//---------------------------------------------------------
public void setElement(T data) {
element = data;
}
//---------------------------------------------------------
// Returns the next node.
//---------------------------------------------------------
public LinearNode<T> next() {
return next;
}
//---------------------------------------------------------
// Sets the next node.
//---------------------------------------------------------
public void next(LinearNode<T> node) {
next = node;
}
}
list.InsertFirst(NodeB.getElement());
Are you trying to insert the Node itself or the Node.getElement?
Related
I am currently trying to reverse a linked list, I have an unexpected problem where it doesn't print correctly. And when I try and access the values of the linked list I get an error. I would greatly appreciate an expert eye to see what I am missing. Thanks in advance.
public class SinglyLinkedList<E> implements Cloneable, Iterable<E>, List<E> {
//---------------- nested Node class ----------------
/**
* Node of a singly linked list, which stores a reference to its
* element and to the subsequent node in the list (or null if this
* is the last node).
*/
private static class Node<E> {
private E data; // reference to the element stored at this node
private Node<E> nextNode;
//methods for accessing variables
public Node<E> getNextNode() { return nextNode; }
public E getData() { return data; }
// Modifier methods
public void setNext(Node<E> n) { nextNode = n; }
public void setData(E n) { data = n; }
public Node(E e, Node<E> n) {
nextNode = n;
data = e;
}
// reference to the subsequent node in the list// TODO
} //----------- end of nested Node class -----------
// instance variables of the SinglyLinkedList
private int size = 0; // number of nodes in the list
private Node<E> head = null; // head node of the list (or null if empty)
public SinglyLinkedList() {
} // constructs an initially empty list
// access methods
/**
* Returns the number of elements in the linked list.
*
* #return number of elements in the linked list
*/
public void addFirst(E e) {
head = new Node<E>(e, head); // create the new node and link new node
size++;
}
/**
* Produces a string representation of the contents of the list.
* This exists for debugging purposes only.
*/
public String toString() {
StringBuilder temporaryString = new StringBuilder();
temporaryString.append("[");
for(Iterator<E> iterator = iterator(); iterator.hasNext();){
temporaryString.append(iterator.next()).append(", ");
}
temporaryString.deleteCharAt(temporaryString.length() - 1);
temporaryString.deleteCharAt(temporaryString.length() - 1);
temporaryString.append("]");
return temporaryString.toString();
}
private class SinglyLinkedListIterator implements Iterator<E> {
private Node<E> current;
public SinglyLinkedListIterator() {
current = head;
}
#Override
public boolean hasNext() {
return current != null;
}
#Override
public E next() {
if(!hasNext()) throw new RuntimeException("No such element");
E res = current.getData();
current = current.getNextNode();
return res;
}
}
public Iterator<E> iterator() {
return new SinglyLinkedListIterator();
}
The reverse Linked List method:
public Node<E> reverseLinkedList(SinglyLinkedList sll) {
Node<E> previous = null;
Node<E> current = sll.head;
while (current != null) {
Node<E> nextElement = current.getNextNode();
current.setNext(previous);
previous = current;
current = nextElement;
}
return previous;
}
The main method:
public static void main(String[] args) {
SinglyLinkedList<Integer> sll2 = new SinglyLinkedList<Integer>();
sll2.addFirst(1);
sll2.addFirst(2);
sll2.addFirst(3);
System.out.println(sll2.toString());
sll2.reverseLinkedList(sll2);
System.out.println(sll2.toString());
}
The output:
[3, 2, 1]
//i should expect to get 1,2,3
[3]
As you are mutating ("rewiring") the given linked list in the reverseLinkedList function, you are not actually producing a new linked list. So to have it return something is actually contradictory. Either it should return a completely new linked list without mutating the given one, or it should mutate the given linked list and not return anything.
From your main code I see that you actually expect to mutate the given linked list, as you don't use the returned value and print the result based on sll. So make your function a void one, and drop the return statement.
The core problem now is that you never change the head member: it still references what used to be the first node in the list, but is now the last one. You should assign the node to head that previously was the tail node.
So the last statement in your function should be:
sll.head = previous;
I am new in java. I have been trying to do some tests while teaching myself the language. Now I am on a linked list implementation. I chucked the code from test samples online. There are two files, LinkedList and LinkedListIterator. I am alright understanding the implementation. However, I would like to add to methods to the LinkedList class. One method (getString()) will be used to display a concatenation of all the string variables in the linkedlist. The second method getSize() will be used to display the size of the list. I have trouble getting the current instance of the linkedlist so I can iterate and get the strings and size. Can someone please assist? Help will be really appreciated.
The two files are as below:
import java.util.NoSuchElementException;
public class LinkedList
{
//nested class to represent a node
private class Node
{
public Object data;
public Node next;
}
//only instance variable that points to the first node.
private Node first;
// Constructs an empty linked list.
public LinkedList()
{
first = null;
}
// Returns the first element in the linked list.
public Object getFirst()
{
if (first == null)
{
NoSuchElementException ex
= new NoSuchElementException();
throw ex;
}
else
return first.data;
}
// Removes the first element in the linked list.
public Object removeFirst()
{
if (first == null)
{
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else
{
Object element = first.data;
first = first.next; //change the reference since it's removed.
return element;
}
}
// Adds an element to the front of the linked list.
public void addFirst(Object element)
{
//create a new node
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
//change the first reference to the new node.
first = newNode;
}
// Returns an iterator for iterating through this list.
public ListIterator listIterator()
{
return new LinkedListIterator();
}
public String toString(){
}
public int getSize(){
return this.size();
}
//nested class to define its iterator
private class LinkedListIterator implements ListIterator
{
private Node position; //current position
private Node previous; //it is used for remove() method
// Constructs an iterator that points to the front
// of the linked list.
public LinkedListIterator()
{
position = null;
previous = null;
}
// Tests if there is an element after the iterator position.
public boolean hasNext()
{
if (position == null) //not traversed yet
{
if (first != null)
return true;
else
return false;
}
else
{
if (position.next != null)
return true;
else
return false;
}
}
// Moves the iterator past the next element, and returns
// the traversed element's data.
public Object next()
{
if (!hasNext())
{
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else
{
previous = position; // Remember for remove
if (position == null)
position = first;
else
position = position.next;
return position.data;
}
}
// Adds an element before the iterator position
// and moves the iterator past the inserted element.
public void add(Object element)
{
if (position == null) //never traversed yet
{
addFirst(element);
position = first;
}
else
{
//making a new node to add
Node newNode = new Node();
newNode.data = element;
newNode.next = position.next;
//change the link to insert the new node
position.next = newNode;
//move the position forward to the new node
position = newNode;
}
//this means that we cannot call remove() right after add()
previous = position;
}
// Removes the last traversed element. This method may
// only be called after a call to the next() method.
public void remove()
{
if (previous == position) //not after next() is called
{
IllegalStateException ex = new IllegalStateException();
throw ex;
}
else
{
if (position == first)
{
removeFirst();
}
else
{
previous.next = position.next; //removing
}
//stepping back
//this also means that remove() cannot be called twice in a row.
position = previous;
}
}
// Sets the last traversed element to a different value.
public void set(Object element)
{
if (position == null)
{
NoSuchElementException ex = new NoSuchElementException();
throw ex;
}
else
position.data = element;
}
} //end of LinkedListIterator class
}
LinkedListIterator class:
public interface ListIterator
{
//Move Moves the iterator past the next element.
Object next();
// Tests if there is an element after the iterator position.
boolean hasNext();
// Adds an element before the iterator position
// and moves the iterator past the inserted element.
void add(Object element);
// Removes the last traversed element. This method may
// only be called after a call to the next() method.
void remove();
// Sets the last traversed element to a different value.
void set(Object element);
}
Error when I try the implmentation of getSize():
Exception in thread "main" java.lang.StackOverflowError
at assignment10.LinkedList.size(LinkedList.java:84)
at assignment10.LinkedList.size(LinkedList.java:84)
at assignment10.LinkedList.size(LinkedList.java:84)
at assignment10.LinkedList.size(LinkedList.java:84)
The getSize() could be
public int getSize(){
int size = 0;
ListIterator iterator = listIterator();
while(iterator.hasNext()) {
iterator.next();
size++;
}
return size;
}
But it is not efficient to iterate over list each time to knowe it's size. The better solution is to store size as class variable and increase or decrease in methods modifying list.
The idea of toString() method is the same, iterate over the list and append each item to the result string
public String toString(){
StringBuilder sb = new StringBuilder();
ListIterator iterator = listIterator();
while(iterator.hasNext()) {
sb.append(String.valueOf(iterator.next())).append(",");
}
return sb.toString;
}
I'm creating a Stack Data Structure and am implementing it using a Linked List. I am using 3 java files - stack.java, stacktest.java and linkedList.java. The linked list works fine when I test it but the stack test is giving me the following errors
Is Empty?: true
Is Empty?: false
Exception in thread "main" java.lang.NullPointerException
at Stack.Peek(Stack.java:56)
at StackTest.main(StackTest.java:12)
Here is my stack.java file
import java.util.EmptyStackException;
public class Stack{
linkedList list;
int count;
public Stack()
{
list = new linkedList();
count = 0;
}
/**
* Adds a node to the head of the list
* #param c character to add
*/
public void Push(char c)
{
if(list.isEmpty())
{// If the list is empty
list.addFirst(c); // Add first
count++;
}
else
{
list.addAtEnd(c); // To the end (tail acts as head)
count++;
}
}
/**
* Removes a node from the head of the list
* #return char removed node
*/
public char Pop() throws EmptyStackException
{
if (!isEmpty())
{
return list.removeLast();
}
else
{
throw new EmptyStackException();
}
}
/**
* Returns the char from the head of the list
* #return char from top of the list
*/
public char Peek() throws EmptyStackException
{
if (!isEmpty())
{
return list.getTail().ch;
}
else
{
throw new EmptyStackException();
}
}
/**
* Is the list empty?
* #return true=yes false=no
*/
public boolean isEmpty()
{
return list.isEmpty();
}
/**
* Counts number of nodes within the list
* #return int # nodes in list
*/
public int getCount()
{
int counter = 0;
Node temp = list.getHead(); // Get head pointer.
while(temp.next != null) // Iterate until tail is reached.
counter++; // Increment counter on each node
return counter;
}
public void printStack()
{
list.printList();
}
}
My stacktest.java
import java.io.IOException;
public class StackTest {
public static void main(String args[]) throws IOException
{
Stack stackList = new Stack();
System.out.println("Is Empty?: " + stackList.isEmpty());
stackList.Push('A');
System.out.println("Is Empty?: " + stackList.isEmpty());
stackList.Pop();
stackList.Peek();
stackList.isEmpty();
stackList.getCount();
stackList.printStack();
}
}
And my linkedList.java
class Node
{
protected char ch;
protected Node next;
protected Node previous;
/**
* Construct a node with the given character value
* #param c - The character
*/
public Node (char c)
{
this.ch = c;
this.next = null;
this.previous = null;
}
}
public class linkedList
{ /** A reference to the head of the list */
protected Node head;
protected Node tail;
/**
* Construct a new empty list
*/
public linkedList()
{
head=null;
tail=null;
}
public Node getHead()
{
return head;
}
public Node getTail()
{
return tail;
}
/**
*Set c as first node in the list
*#param c The character to be inserted
*/
public void addFirst(char c)
{
Node newNode = new Node(c);
head=newNode; // Adding new element.
tail=newNode; // Head and tail will be the same.
}
/**
*Add a character to the end of the list
*#param c The character to be inserted
*/
public void addAtEnd(char c)
{
Node nod = new Node(c);
Node temp = head;
while (temp.next != null) // cycle until at end of list.
temp = temp.next;
temp.next=nod; // last element is new node.
nod.previous=temp; // linking last node with rest of list.
tail=nod; // new node is the tail.
}
/**
*Add a character in alphabetical order into the list
*#param c The character to be inserted
*/
public void addInOrder(char c)
{
Node n= new Node(c);
if(isEmpty())
{
addFirst(c);
}
else
{
Node pre=head;
Node succ= head.next;
if (n.ch < pre.ch)
{
n.next =head;// join node n to the list at the head.
head = n;// head is reading from n.
}
else
{
while(succ!=null && n.ch > succ.ch)
{ // find the position to insert the node
pre = succ;
succ = pre.next;
} //rearrange pointers
n.next = succ;
pre.next = n;
}
}
}
/**
*Test to see if this list is empty
*#returns true or false
*/
public boolean isEmpty()
{
return (head == null && tail == null);
}
/**
*removes a node from the head of the list
*#returns c The character from the removed node
*/
public char removeFirst()
{
if(!isEmpty())
{
// a temporary pointer to enable return of the char being removed
Node temp = head;
head = head.next; // move head pointer along the list
return temp.ch;
}
else
{
System.out.println("List is empty");
return '?'; //to indicate that the list is empty
}
}
/**
* removes a node from the tail of the list
* #return c The character from the removed node
*/
public char removeLast()
{
Node t = getTail(); // Retrieve tail
tail = t.previous; // Set tail to previous node
return t.ch; // return character
}
/**
*prints the characters in the list
*/
public void printList()
{
Node temp=head;
while(temp!=tail.next)
{
System.out.print(temp.ch + " ");
temp=temp.next;
}
System.out.println(); // After print, goes to new line.
}
}
I understand that I am using a variable which is null but can someone explain to me where I am going wrong
When you call addFirst(), it sets both the head and the tail to the new node. However, when you removeLast(), it only changes tail to null and leaves head set to the node you popped.
Then, when you call isEmpty(), since head is not null, it doesn't recognize that the list is empty and returns false.
You need to modify removeLast() to check whether you're removing the only element in the list, and update the head accordingly.
Once you have popped out your only element of the stack, the inner list is empty. Therefore list.getTail() will return null, and you cannot peek into your stack anymore.
In StackTest.java you are inserting one element into the stack, then popping it and then trying to peek into the stack. Your pop function is using removeLast method of linkedList.java. While you are correctly pointing the tail to tail.previous, you need to also check if the result of this leads to tail becoming null (which means you have removed the last element in your linked list). You should check for this in removeLast and make head = tail if this is the case:
public char removeLast()
{
Node t = getTail(); // Retrieve tail
tail = t.previous; // Set tail to previous node
if(tail == null){
head = tail;
}
return t.ch; // return character
}
This way isEmpty() method will always return true if you have popped out the last element in the list. You will have to make a similar change to removeFirst() as follows:
public char removeFirst()
{
if(!isEmpty())
{
// a temporary pointer to enable return of the char being removed
Node temp = head;
head = head.next; // move head pointer along the list
if(head == null){
tail = head;
}
return temp.ch;
}
else
{
System.out.println("List is empty");
return '?'; //to indicate that the list is empty
}
}
After this change your peek() method will now throw an EmptyStackException (which is desirable) instead of a NPE when you try to peek into an empty stack.
Might I also suggest that you don't need to traverse the whole list to add at the end of the list (on addAtEnd()). Since you already have the tail you can just append to the end of the tail pointer.
This is where I add the object to the list:
public void add (T element)
{
LinearNode<T> node = new LinearNode<T> (element);
if (size() == 0) {
this.last = node; // last node
this.list = node; // first node
this.count++;
}//end if
else
if (!(contains(element)))
{
last.setNext(node);
last = node;
count++;
} //end if
}
I have to create a method which returns the last object in this list. Could anyone help?
Presumably LinearNode<T> has a method getValue() which returns the T instance stored in that node. You already have a last reference in your LinkedList class, so it should be as simple as
public T getLast()
{
return last.getValue();
}
This is just a skeleton, and needs to check for empty list, etc.
For computer Science I'm coding a program that keeps the "tails" end of head, a Node so that I can add and remove from the Node/LinkedList. This isn't entirely difficult, but I've run into an issue that makes me cry. I am running into java heap space ( Out of Memory ).
Here is my code:
import java.util.NoSuchElementException;
/**
A linked list is a sequence of nodes with efficient
element insertion and removal. This class
contains a subset of the methods of the standard
java.util.LinkedList class.
*/
public class LinkedList
{
private Node head;
private int currentSize;
private Node tails;
/**
Constructs an empty linked list.
*/
public LinkedList()
{
head = null;
tails = null;
currentSize = 0;
}
/**
Adds an element to the front of the linked list.
#param element the element to add
*/
public void addFirst(Object element)
{
Node newNode = new Node();
if(head == null){
tails = newNode;
}
newNode.data = element;
currentSize++;
newNode.next = head;
head = newNode;
}
/**
Removes the head element in the linked list.
#return the removed element
*/
public Object removeFirst(){
if (head == null)
throw new NoSuchElementException();
Object temp = head.data;
head = head.next;
if(tails.next == null)
tails.next = head;
currentSize--;
return temp;
}
/**
Returns the head element in the linked list.
#return the head element in the linked list
*/
public Object getFirst()
{
if (head == null)
throw new NoSuchElementException();
return head.data;
}
/**
Returns the element at a given position in the linked list.
#param index the element position
#return the element at the given position
*/
Object get(int index)
{
if (index < 0)
throw new NoSuchElementException();
Node current = head;
int i = 0;
while (current != null && i < index)
{
current = current.next;
i++;
}
if (current == null)
throw new NoSuchElementException();
return current.data;
}
/**
Computes the size of the linked list
#return the number of elements in the list
*/
public int size()
{
//to be completed for lab 7.1 #2
return currentSize; // rewrite this, just makes it compile
}
/**
Reverses all elements in a linked list.
*/
public void reverse(){
Node currentNode, nextNode, loopNode;
if(head==null)
return;
currentNode=head;
nextNode= head.next;
loopNode = null;
while(nextNode != null){
currentNode.next = loopNode;
loopNode= currentNode;
currentNode=nextNode;
nextNode =nextNode.next;
}
head = currentNode;
head.next = loopNode;
}
/**
Adds an element to the end of the linked list.
#param element the element to add
*/
public void add(Object element){
Node current = new Node();
current.data = element;
if(tails.next == null)
tails.next = current;
tails = head;
currentSize ++;
}
/**
Returns an iterator for iterating through this list.
Allows the use of the iterator outside of this class.
#return an iterator for iterating through this list
*/
public ListIterator listIterator()
{
return new LinkedListIterator();
}
/**
Returns a string representation of this list in brackets
separated by commas.
#return a string of list elements.
*/
public String toString()
{
StringBuilder temp = new StringBuilder();
ListIterator it = listIterator();
while(it.hasNext()){
temp.append(it.next());
if(it.hasNext())
temp.append(", ");
}
return temp.toString();
}
private static class Node
{
private Object data;
private Node next;
}
private class LinkedListIterator implements ListIterator
{
private Node position;
private Node previous;
/**
Constructs an iterator that points to the front
of the linked list.
*/
public LinkedListIterator()
{
position = null;
previous = null;
}
/**
Moves the iterator past the next element.
#return the traversed element
*/
public Object next()
{
if (!hasNext())
throw new NoSuchElementException();
previous = position; // Remember for remove
if (position == null)
position = head;
else
position = position.next;
return position.data;
}
/**
Tests if there is an element after the iterator
position.
#return true if there is an element after the iterator
position
*/
public boolean hasNext()
{
if (position == null)
return head != null;
else
return position.next != null;
}
/**
Adds an element before the iterator position
and moves the iterator past the inserted element.
#param element the element to add
*/
public void add(Object element)
{
if (position == null)
{
addFirst(element);
position = head;
tails = head;
}
else{
Node newNode = new Node();
newNode.data = element;
newNode.next = position.next;
position.next = newNode;
position = newNode;
previous = position;
tails = newNode;
}
currentSize ++;
}
/**
Removes the last traversed element. This method may
only be called after a call to the next() method.
*/
public void remove()
{
if (previous == position)
throw new IllegalStateException();
if (position == head)
{
removeFirst();
currentSize --;
tails = previous;
}
else
{
previous.next = position.next;
currentSize --;
tails = previous;
}
position = previous;
}
/**
Sets the last traversed element to a different
data.
#param element the element to set
*/
public void set(Object element)
{
if (position == null)
throw new NoSuchElementException();
position.data = element;
}
}
}
The error is at removeFirst().
Any help?
Edits:
I'm attempting to store the last referenced node so I can play with it else where.
Usually that error implies infinite recursion. I suggest you look at the following code section.
while(it.hasNext()){
temp.append(it.next());
if(it.hasNext())
temp.append(", ");
}
Your code is quite bloated. Take a look at this:
How do I create a Linked List Data Structure in Java?
If you wan't extra marks try and add generics:
http://docs.oracle.com/javase/tutorial/java/generics/