Having trouble with my CircularList - java

Right now I am trying to create a circular list, where when I use hasNext() from an Iterator it should always return true. However right now it is returning that it is not a circular list, and I am also having problems printing out the values (in this example Strings) of the ArrayList. Here is the CircularList class I created, which has a inner Node class for the objects that are put into the list:
public class CircularList<E> implements Iterable{
private Node<E> first = null;
private Node<E> last = null;
private Node<E> temp;
private int size = 0;
//inner node class
private static class Node<E>{ //In this case I am using String nodes
private E data; //matching the example in the book, this is the data of the node
private Node<E> next = null; //next value
//Node constructors, also since in this case this is a circular linked list there should be no null values for previous and next
private Node(E data){
this.data = data;
}
}
//end of inner node class
public void addValue(E item){
Node<E> n = new Node<E>(item);
if(emptyList() == true){ //if the list is empty
//only one value in the list
first = n;
last = n;
}
else{ //if the list has at least one value already
//store the old first value
temp = first;
//the new first is the input value
first = n;
//next value after first is the old first value
first.next = temp;
//if after this there will be only two values in the list once it is done
if(size == 1){
last = temp;
}
//if the list is greater than one than the last value does not change, since any other values will be put before last in this case, and not replace it
//creating the circular part of the list
last.next = first;
}
size++;
}
public boolean emptyList(){
boolean result = false;
if(first == null && last == null){ //if there is no values at all
result = true;
}
return result;
}
#Override
public Iterator<E> iterator() {
// TODO Auto-generated method stub
return new CircularIterator<E>(); //each time this method is called it will be creating a new instance of my Iterator
}
}
Here is the Iterator class I am making:
public class CircularIterator<E> implements Iterator<E> {
#Override
public boolean hasNext() {
return false;
}
#Override
public E next() {
// TODO Auto-generated method stub
return null;
}
#Override
public void remove() {
// TODO Auto-generated method stub
}
}
and finally the Test class:
public class Test {
static CircularList<String> c = new CircularList<String>(); //in this case it is a string list
static Iterator it = c.iterator();
public static void main(String[]args){
c.addValue("Bob");
c.addValue("Joe");
c.addValue("Jaina");
c.addValue("Hannah");
c.addValue("Kelly");
Iterate();
for(String val : c){
System.out.println(val);
}
}
private static boolean Iterate(){
boolean result = false;
if(!it.hasNext()){
System.out.println("Not a circular list!");
}
else{
result = true;
}
return result;
}
}
Again I am trying to get it to always return true, I think the problem lies with my hasNext() method, but I am not completely sure.

The main problem with your approach is you are using static inner classes - this is not necessary. Making the outer class generic is sufficient. The generic parameter is then inherited by the inner classes and all sorts of issues disappear.
Implementing an Iterator properly is subtle.
public static class CircularList<E> implements Iterable<E> {
private Node first = null;
private Node last = null;
private int size = 0;
private class Node {
private E data;
private Node next = null;
private Node(E data) {
this.data = data;
}
}
public void addValue(E item) {
Node n = new Node(item);
if (emptyList()) {
//only one value in the list
first = n;
last = n;
} else { //if the list has at least one value already
//store the old first value
Node temp = first;
//the new first is the input value
first = n;
//next value after first is the old first value
first.next = temp;
//if after this there will be only two values in the list once it is done
if (size == 1) {
last = temp;
}
//if the list is greater than one than the last value does not change, since any other values will be put before last in this case, and not replace it
//creating the circular part of the list
last.next = first;
}
size++;
}
public boolean emptyList() {
boolean result = false;
if (first == null && last == null) { //if there is no values at all
result = true;
}
return result;
}
#Override
public Iterator<E> iterator() {
return new CircularIterator(); //each time this method is called it will be creating a new instance of my Iterator
}
private class CircularIterator implements Iterator<E> {
// Start at first.
Node next = first;
public CircularIterator() {
}
#Override
public boolean hasNext() {
// Stop when back to first.
return next != null;
}
#Override
public E next() {
if (hasNext()) {
E n = next.data;
next = next.next;
if (next == first) {
// We're done.
next = null;
}
return n;
} else {
throw new NoSuchElementException("next called after end of iteration.");
}
}
}
}
public void test() {
CircularList<String> c = new CircularList<>();
c.addValue("A");
c.addValue("B");
c.addValue("C");
c.addValue("D");
for (String s : c) {
System.out.println(s);
}
}
Your main code was essentially correct - all I did was remove the unnecessary generics parameters from the inner classes.
Note that the way you add node to the list means that the items come out backwards. You could adjust that in your addValue method quite easily.

You can simply use following for circular iteration. This Circular list behave as same as other java.util.Lists. But it's iteration is modified. You don't need to care about it's performance tuning additionally. Because it's super class (LinkedList) is already well tested and enough stronger to use.
`public class CircularList extends LinkedList {
#Override
public Iterator<E> iterator() {
return createIterator();
}
//create new iterator for circular process
private Iterator<E> createIterator() {
return new Iterator<E>() {
private int index = 0;
#Override
public boolean hasNext() {
//no elements when list is empty
return isEmpty();
}
#Override
public E next() {
E node = get(index);
//rotate index
index++;
if (index == size()) {
index = 0;
}
return node;
}
};
}
}`

Related

Failing J-unit test for Shallow Copy on a Linked Stack

I am trying to implement a shallow copy for a Linked Stack, but I am failing the J-unit test provided by my instructor.
I have tried to implement a for loop that will cycle through the stack top to bottom and create a reference for each node to the new list on the pass through. I've added a print statement and the data references seem to match up,but my test are still failing.
public class LinkedStack<E> implements Stack<E>{
private int size = 0;
// Unlike the book, we'll use an inner class for our Node.
// Its two data members can be accessed directly by the Stack
// code, so we don't need setters and getters.
protected class Node{
E data;
Node next;
}
protected Node top; // not public, but can still be seen by other classes in the
// csci211 package.
/** Create an empty stack.
*
*/
public LinkedStack(){
top = null;
}
#Override // see interface for comments.
public void push(E e){
//TODO 75
Node temp = new Node();
temp.data = e;
temp.next = top;
top = temp;
}
#Override // see interface for comments.
public E pop(){
if (top==null) {
throw new NoSuchElementException("Cannout pop an Empty Stack.");
}
E topvar;
topvar = top.data;
top = top.next;
return topvar;
}
#Override // see interface for comments.
public E peek() {
if (top == null) {
throw new NoSuchElementException("Cannout peek an Empty Stack.");
}
//E topvar;
//topvar = top.data;
return top.data;
}
/** Retrieve the number of elements on this stack.
*
* #return an int containing the number of elements
*/
public int size() {
return this.size;
}
/** An Iterator for our LinkedStack.
*
* #author rhodes
*
*/
class LinkedStackIterator implements Iterator<E> {
LinkedStack<E>.Node next; // the book calls this "current"
public LinkedStackIterator(LinkedStack<E> s){
next = s.top;
}
#Override
public boolean hasNext() {
return top != null;
//TODO 100
//return false;
}
#Override
public E next() {
if (!hasNext()) throw new NoSuchElementException();
E data = top.data;
top = top.next;
return data;
//TODO 100
//return null;
}
}
#Override
public void add(E element) {
push(element);
}
#Override
public void clear() {
this.top = null;
this.size = 0;
}
#Override
public List<E> shallowCopy() {
LinkedStack<E> newstack = new LinkedStack<E>();
ArrayList<E> Alist = new ArrayList<E>();
//Iterate through while we haven't hit the end of the stack
Node newtest = top;
while (newtest != null) {
Alist.add(newtest.data);
newtest = newtest.next;
//TODO 85
}
for(int i = Alist.size()-1;i>=0;i--) {
newstack.push(Alist.get(i));
}
return newstack;
}
#Override
public Iterator<E> iterator() {
return new LinkedStackIterator(this);
}
}
This is the Junit tests that I am failing
#Test
#SuppressWarnings("deprecation") // for Date.setHours(), Date.getHours()
public void shallowCopy1() {
// let's use Date, since it's mutable.
LinkedStack<Date> s = new LinkedStack<Date>();
Date d = new Date();
d.setHours(17);
s.push(d);
LinkedStack<Date> s2 =(LinkedStack<Date>) s.shallowCopy();
Date d2=s2.pop();
// The shallow copy should contain references to the same objects
// as the original.
assertTrue(d == d2);
// So, we can change the Date in the original list using the Date that
// came from the shallow copy.
d2.setHours(14);
assertTrue(d.getHours() == 14);
// I don't usually put two asserts in one test, but this seems like
// an instructive example.
}
#Test(expected=NoSuchElementException.class)
public void shallowCopy2() {
LinkedStack<Integer> s1 = new LinkedStack<Integer>();
for(int i=0; i<10; i++) {
s1.push(i);
}
LinkedStack<Integer> s2 =(LinkedStack<Integer>) s1.shallowCopy();
s2.push(10); // supposed to only affect s2
s2.push(11); // supposed to only affect s2
for(int i=0; i<10; i++) {
s1.pop();
}
int last = s1.pop(); // should throw
}
#Test
public void shallowCopy3() {
LinkedStack<Integer> q1 = new LinkedStack<Integer>();
for(int i=0; i<10; i++) {
q1.push(i);
}
LinkedStack<Integer> q2 =(LinkedStack<Integer>) q1.shallowCopy();
//Let's check that the order of elements is correct in the copy.
for(int i=0; i<10; i++) {
int v1=q1.pop();
int v2=q2.pop();
assertEquals(v1, v2);
}
}
If anyone could point me in the right direction I would appreciate it. This is a Homework Problem.
Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.
Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.
protected class Node{
E data;
Node next;
Node(Node node){
this.next = node.next;
this.data = node.data;
}
}
#Override
public List<E> shallowCopy() {
// LinkedStack<E> newStack = new LinkedStack<E>();
//Iterate through while we haven't hit the end of the stack
Node s = new Node(top);
while (top.next != null) {
s.next = new Node(top.next);
top = top.next;
s = s.next;
}
System.out.println("FINSHED!");
return (List<E>) s;
}
#Override
public List<E> shallowCopyWithoutUpdatingNodeClass() {
// LinkedStack<E> newStack = new LinkedStack<E>();
//Iterate through while we haven't hit the end of the stack
Node s = new Node(top);
while (top.next != null) {
s.next = new Node();
s.next.next = top.next;
s.next.data = top.data;
top = top.next;
s = s.next;
}
System.out.println("FINSHED!");
return (List<E>) s;
}
Answer Inspired by :- What is the difference between a deep copy and a shallow copy?
The original problem was the node data was just being overwritten not creating a new node. Then the stack was backwards. Finally I implement and array to reverse the stack.
#Override
public List<E> shallowCopy() {
LinkedStack<E> newstack = new LinkedStack<E>();
ArrayList<E> Alist = new ArrayList<E>();
//Iterate through while we haven't hit the end of the stack
Node newtest = top;
while (newtest != null) {
Alist.add(newtest.data);
newtest = newtest.next;
//TODO 85
}
for(int i = Alist.size()-1;i>=0;i--) {
newstack.push(Alist.get(i));
}
//System.out.println("FINSHED!");
return newstack;
}

Equals method of a linked list not working

I am having trouble implementing the remove method in the List class. I am first writing the objects out into a file, then I am retrieving those objects and putting them inside a linked list. However, when I try to check for equality by going over the entire linked list I get not matches even though I know for sure that object is in there. I can't even get .equals to work it seems.
package ProjectOne;
public class List<T> {
private LLNode<T> list;
private int numberOfNodes = 0;
private LLNode<T> location;
private LLNode<T> previous;
protected boolean found;
public List() {
list = null;
}
public void add(T element) {
if (numberOfNodes == 0) {
list = new LLNode<T>(element);
numberOfNodes++;
}
else {
LLNode<T> newNode = new LLNode<T>(element);
newNode.setLink(list);
list = newNode;
}
}
public void find(T target) {
location = list;
found = false;
while(location !=null) {
System.out.println(target.equals(location.getInfo()));
if(location.getInfo().equals(target)) {
found = true;
return;
}
else {
previous = location;
location = location.getLink();
}
}
}
public boolean remove(T element) {
this.find(element);
if(found) {
if(list == location) list = list.getLink();
else previous.setLink(location.getLink());
}
return found;
}
public LLNode<T> getList() {
return list;
}
public String toString() {
LLNode<T> currentNode = list;
String info = "";
while(currentNode !=null) {
info +=currentNode.getInfo();
currentNode = currentNode.getLink();
}
return info;
}
}
These are one of many objects I am writing out to the file
Patient p1 = new Patient("Alex", "1123 metropolitan", new Date("11/20/1997"));
p1.setFirstVisit(new Date("11/20/1997"));
p1.setHeight(72);
p1.setLastVisit(new Date("11/20/1997"));
p1.setWeight(200);
out.writeObject(p1);
Patient p2 = new Patient("John", "200 avenue of americas", new Date("12/20/1999"));
p2.setFirstVisit(new Date("11/11/2005"));
p2.setHeight(5);
p2.setLastVisit(new Date("11/21/2010"));
p2.setWeight(150);
out.writeObject(p2);
Patient p3 = new Patient("Sarah", "Park avenue", new Date("09/07/1960"));
p3.setFirstVisit(new Date("05/11/1977"));
p3.setHeight(75);
p3.setLastVisit(new Date("01/21/2017"));
p3.setWeight(110);
out.writeObject(p3);
Patient p4 = new Patient("Malcolm", "56street", new Date("05/28/1977"));
p4.setFirstVisit(new Date("01/11/1990"));
p4.setHeight(75);
p4.setLastVisit(new Date("8/21/2016"));
p4.setWeight(155);
out.writeObject(p4);
However, when I call System.out.println(list.remove(p1));
I get the output
false
false
false
false
false
with the last being the return result of the method and the first four just trying to debug.
Make sure that you override equals and hashcode methods in Patient class.

Parsing Through ListNodes in List

I'm having trouble figuring out why my code won't parse through the ListNodes in the Lists, in order to add a new String as a ListNode. I'm trying to write the function add(String s), to add a new ListNode to the List. If the list is empty, I just add the String as a ListNode, and if not, I parse through using node and myNext, and then if node.myNext is null, I replace it with the newly created ListNode. What is the reason this isn't working? It either does not throw an output or it says it is out of bounds.
public class List {
private ListNode myHead;
private int mySize;
public List() {
this.myHead = null;
this.mySize = 0;
}
public class ListNode {
public String myData;
public ListNode myNext;
public ListNode(String element, ListNode next) {
this.myData = element;
this.myNext = next;
}
public ListNode(String element) {
this(element, null);
}
public boolean isEmpty() {
return this.length() == 0;
}
public void add(String s) {
if(this.isEmpty() == true) {
this.addToFront(s);
}
else {
this.mySize++;
for(ListNode node = this.myHead; node.myData != null; node = node.myNext) {
if(node.myNext == null) {
ListNode lno = new ListNode(s, null);
node.myNext = lno;
}
else {
node.myData = node.myData;
}
}
}
}
In you ListNode you can't access methods and variables of your List class.
Assuming that you want to add the new String at the top of your List you should do something like this:
public class List {
private ListNode myHead;
private int mySize;
public List() {
this.myHead = null;
this.mySize = 0;
}
public boolean isEmpty() {
return this.mySize == 0;
}
public void add(String s) {
this.myHead = new ListNode(s, myHead);//add new String as head element
this.mySize++;
}
}
public class ListNode {
public String myData;
public ListNode myNext;
public ListNode(String element, ListNode next) {
this.myData = element;
this.myNext = next;
}
public ListNode(String element) {
this(element, null);
}
}
If you want to add it at the end of your List you can try it like this:
public void add(String s) {
if(this.isEmpty()){
this.myHead = new ListNode(s, myHead);//add new String as head element
}else{
ListNode node = this.myHead;
while (node.myNext != null){
node = node.myNext;
}
//now you hav the last node of your list
node.myNext = new ListNode(s,null);
}
this.mySize++;
}
The code you have pasted is not complete.
Also, If I am correct, your List is having the ListNodes and thus, it is your List where you should put methods to check if it is Empty (does not have any ListNodes in it) or add, delete, count, search etc. functions.
For isEmpty(), There is no length() defined, so simply check the size to be == 0.
For add(), if it is empty just point myHead to your new ListNode; If you have to add in end, iterate the myHead using a currentNode reference, till its next is null and add.
If it is to be in middle somewhere, you will need to check for ListNode myData to decide where it fits white moving from myHead towards null and once you find a place to insert, you will need to change the [PrevNode] -> new ListNode -> [nextNode]

What does it mean to return an iterator? Java

I have to write a class that implements the Iterable interface. I'm confused about what it means to return an iterator object. An iterator just goes through the elements of a list, so how would I return this as an object? Would I return a list that was able to be iterated through or what? How can an iterator be an object when all it does is go through or change data in other objects?
Here is an example of a very simplistic list. It represents the list as linked elements.
The iterator object is created as an anonymous inner class holding the current element as the state. Each call of iterator() creates a new iterator object.
import java.util.Iterator;
public class SimplisticList<T> implements Iterable<T> {
/*
* A list element encapsulates a data value and a reference to the next
* element.
*/
private static class Element<T> {
private T data;
private Element<T> next;
Element(T data) {
this.data = data;
next = null;
}
public T getData() {
return data;
}
public Element<T> getNext() {
return next;
}
public void setNext(Element<T> next) {
this.next = next;
}
}
// We only need a reference to the head of the list.
private Element<T> first = null;
// The list is empty if there is no first element.
public boolean isEmpty() {
return first == null;
}
// Adding a new list element.
// For an empty list we only have to set the head.
// Otherwise we have to find the last element to add the new element.
public void add(T data) {
if(isEmpty()) {
first = new Element<T>(data);
} else {
Element<T> current = first;
while(current.getNext() != null) {
current = current.getNext();
}
current.setNext(new Element<T>(data));
}
}
#Override
public Iterator<T> iterator() {
// Create an anonymous implementation of Iterator<T>.
// We need to store the current list element and initialize it with the
// head of the list.
// We don't implement the remove() method here.
return new Iterator<T>() {
private Element<T> current = first;
#Override
public boolean hasNext() {
return current != null;
}
#Override
public T next() {
T result = null;
if(current != null) {
result = current.getData();
current = current.getNext();
}
return result;
}
#Override
public void remove() {
// To be done ...
throw new UnsupportedOperationException();
}
};
}
}
Returing an iterator means returning an instance of a class that implements the Iterator interface. This class has to implement hasNext(),next() and remove(). The constructor of the class should initialize the instance in a way that next() would return the first element of the data structure you are iterating over (if it's not empty).
Here's a simple example of an iterator that goes through an String[] array:
public class MyIterator implements Iterator<String> {
private String[] arr;
private int index;
public MyIterator(String[] arr) {
this.arr = arr;
this.index = 0;
}
public boolean hasNext() {
return index < arr.length;
}
public String next() {
return arr[index++];
}
}
(You also need remove() but that would just throw an exception.) Note that when you construct one of these iterators with new MyIterator(myStringArray), you construct an object that has a reference to the array. The Iterator wouldn't be the array itself, or any part of it, but it has a private variable that refers to it. An Iterator for a list or for any other data structure (or even for things that aren't data structures) would follow a similar pattern.

Iterator on several sets

I am writing a Graph class,
I keep a HashMap in which ids of nodes (int values) are mapped to the associated nodes, and I'm using adjacency list approach to keep edges starting from a node (keeping them in form of a HashSet)
Note that : This graph is directed and unweighted,
I want to implement a method which returns an iterator over objects of class Edge :
When getting next on this iterator , one will get an object of class Edge which is created right when it's being traversed, and if there's no more neighbors for a node, it goes to the next node (order is not important) and if there's no more starting nodes (all are traversed), it finishes.
Any idea on how to implement this iterator on the edges without previously keeping the edges in Edge class objects ?
class Graph{
HashMap<Integer , GraphNode> nodes;
public Graph(){
nodes = new HashMap<Integer ,GraphNode>();
}
public boolean addEdge(GraphNode n1 , GraphNode n2){
if (!nodes.containsKey(n1) || !nodes.containsKey(n2))
return false;
return n1.addNeighbor(n2);
}
public boolean addNode(int id){
if (nodes.containsKey(id))
return false;
nodes.put(id , new GraphNode(id));
return true;
}
public boolean removeNode(GraphNode n1){
if (!nodes.containsKey(n1.content))
return false;
for (GraphNode m : n1.neighbors)
m.removeNeighbor(n1);
nodes.remove(n1);
return false;
}
public boolean removeEdge(GraphNode n1 , GraphNode n2){
if (!nodes.containsKey(n1) || !nodes.containsKey(n2))
return false;
return n1.removeNeighbor(n2);
}
public Iterator<GraphNode> NodeIterator(){
return nodes.values().iterator();
}
public Iterator<Edge> EdgeIterator(){
Iterator<GraphNode> itr = this.NodeIterator();
while (itr.hasNext){
GraphNode n = itr.next();
//......
}
}
}
class GraphNode{
HashSet<GraphNode> neighbors;
int content;
public GraphNode(int content){
this.content = content;
neighbors = new HashSet<GraphNode>();
}
boolean addNeighbor(GraphNode n){
if (neighbors.contains(n))
return false;
neighbors.add(n);
return true;
}
boolean removeNeighbor(GraphNode n){
if (!neighbors.contains(n))
return false;
neighbors.remove(n);
return true;
}
}
class Edge{
Node start , end;
public Edge(Node start , Node end){
this.start = start;
this.end = end;
}
}
I think something like this might work :
public Iterator<Edge> EdgeIterator(){
Iterator <Edge> edgeIter = new Iterator<Edge>() {
private Iterator<GraphNode> itr = this.NodeIterator();
private GraphNode currentNode;
... // additional private members as required
public void remove()
{
// you don't have to implement this method if you don't need to support
// this operation
}
public Edge next()
{
if (!hasNext())
throw new NoSuchElementException ();
return new Edge (x , y); // where you find x & y based on the current state
// of the iterator (kept in the private members of
// this instance)
}
public boolean hasNext()
{
return ?; // you return a boolean value based on the current state
// of the iterator (kept in the private members of
// this instance)
}
};
return edgeIter;
}
The EdgeIterator method creates an Iterator<Edge> and defines the methods of the Iterator interface (I left the implementation of these methods to you). The Iterator instance contains an instance of Iterator<GraphNode>, which it uses to iterate over the nodes.
You should add to the iterator some additional private members that keep track of the current node (the last node returned by the node iterator) and the current edge you are iterating on. Whenever you finish iterating over the edges of a node, you get the next node using itr.next() (after checking there is a next node available). next() of the edge iterator can construct the next Edge based on those private members.
As Eran said , I completed the code of the iterator methods ,
Do you think this one works ?
public Iterator<Edge> EdgeIterator(){
Iterator<Edge> edgeIter = new Iterator<Edge>() {
private Iterator<GraphNode> node_itr = NodeIterator();
private Iterator<GraphNode> neighbor_itr;
private GraphNode current_node;
private GraphNode current_neighbor;
public void remove()
{
if (current_node == null || current_neighbor == null)
return;
current_node.removeNeighbor(current_neighbor);
}
public Edge next()
{
if (neighbor_itr == null || !neighbor_itr.hasNext())
if (node_itr.hasNext()){
current_node = node_itr.next();
neighbor_itr = current_node.neighbors.iterator();
}else
return null;
current_neighbor = neighbor_itr.next();
return new Edge(current_node , current_neighbor);
}
public boolean hasNext()
{
if (neighbor_itr == null || !neighbor_itr.hasNext())
if (node_itr.hasNext())
return node_itr.next().neighbors.iterator().hasNext();
else
return false;
return true;
}
};
return edgeIter;
}
Update : The edited/working version :
public Iterator<Edge> EdgeIterator(){
Iterator<Edge> edgeIter = new Iterator<Edge>() {
private Iterator<GraphNode> node_itr = NodeIterator();
private Iterator<GraphNode> neighbor_itr;
private GraphNode current_node;
private GraphNode current_neighbor;
public void remove()
{
if (current_node == null || current_neighbor == null)
return;
current_node.removeNeighbor(current_neighbor);
}
private void moveNext(){
if (neighbor_itr == null || !neighbor_itr.hasNext()){
while (node_itr.hasNext()){
current_node = node_itr.next();
neighbor_itr = current_node.neighbors.iterator();
if (neighbor_itr.hasNext()){
break;
}
}
}
}
public Edge next()
{
moveNext();
current_neighbor = neighbor_itr.next();
return new Edge(current_node , current_neighbor);
}
public boolean hasNext()
{
moveNext();
return neighbor_itr.hasNext();
}
};
return edgeIter;
}

Categories