LinkedList Development Issues
I trying to write a class that is my own personal LinkedList class . I am having problems getting two methods to work correctly . Any suggestions on what I am doing wrong on the following methods .
1. Iterator<E> iterator
// writing my own version of Iterator with a hasNext() , Next(), Remove(). I keep on getting reference to Java’s Iterator and not mine ?
2. Reverse ()
// Writing a method that takes my LinkedList and reverses the order via object reference (pointers) . I go through the method on a whiteboard and I feel like the references do change . When I go to run it tough I get a list that just decreases . i.e. 1 2 3 4 then becomes 2 3 4 .
OK , any suggestions would be appreciated . Thank you.
Here are my three classes:
Code:
//import java.util.Iterator;
class TestListv2 {
public static void main(String [ ] args) {
LinkedList383<Integer> ll = new LinkedList383<Integer>( );
for (int i=0; i<11; i++)
ll.add(i);
int counter = 0;
for (Iterator<Integer>iterator it = ll.iterator( ); it.hasNext( ); ;){
System.out.println(it.next( ));
if (counter++ == 5) it.remove( );
}
System.out.println(ll);
ll.reverse();
System.out.println(ll);
}
}
Code:
//import java.util.Iterator;
public class LinkedList383<E> {
private LinkedListv2<E> front;
private LinkedListv2<E> back;
public LinkedList383( ) {
front = new LinkedListv2<E>( );
back = new LinkedListv2<E>(null, null, front);
front.setNext(back);
}
public void reverse (){
front.reverse();
}
public boolean add(E data) {
LinkedListv2<E> newLL = new LinkedListv2<E>(data, back, back.getPrevious( ));
back.getPrevious( ).setNext(newLL);
back.setPrevious(newLL);
return true;
}
public void add(int index, E data) throws IndexOutOfBoundsException {
front.add(index,data);
}
public E set(int index, E data) throws IndexOutOfBoundsException {
return front.set(index,data);
}
public boolean remove(E data) {
return front.remove(data);
}
public boolean remove(int index) throws IndexOutOfBoundsException {
return front.remove(index);
}
public boolean contains(E data) {
return front.contains(data);
}
public E get(int index) throws IndexOutOfBoundsException {
return front.get(index);
}
public String toString( ) {
return front.toString( );
}
public E iterator(int index) {
return front.get(index);
}
}
Code:
// version 2 of LinkedList: doubly linked
// in this version, there is an object with null data at both the front and the back of the list
//import java.util.*;
public class LinkedListv2<E> {
private E data;
private LinkedListv2<E> next;
private LinkedListv2<E> previous;
// first and last object (front and back of the list) ALWAYS have data == null
// also must call setupBack
public LinkedListv2( ) {
data = null;
next = null;
}
public void setupBack( ) {
LinkedListv2<E> back = new LinkedListv2<E>(null, null, this);
next = back;
}
public LinkedListv2(E d, LinkedListv2<E> n, LinkedListv2<E> p) {
data = d;
next = n;
previous = p;
}
// a couple of getters and setters needed for LinkedList383<E>
public LinkedListv2<E> getNext( ) { return next; }
public LinkedListv2<E> getPrevious( ) { return previous; }
public void setNext(LinkedListv2<E> n) { next = n; }
public void setPrevious(LinkedListv2<E> p) { previous = p; }
// Itterator
public Iterator<E> iterator( ) {
return new Iterator<E>( ) {
private LinkedListv2<E> node = LinkedListv2.this;
public boolean hasNext( ) {
while(node.next != null){
node = node.next;
}
return true;
}
public E next( ) {
if ( node !=null)
node = next.next;
return node.data;
/*previous = node;
node = node.next;
return node.data;
*/
}
public void remove( ) {
previous.next = node.next;
node.next = node.previous;
}
};
}
// revers the items in the list
public void reverse ( ) {
LinkedListv2<E> current = this.next;
while(current != null){
next = current.next;
current.next = current.previous;
current.previous = next;
if (current == null){
current.previous = current.next;
current = this;
//next = current.previous;
}
current = current.next;
}
}
// add in front of blank back of the list
public boolean add(E newData) {
LinkedListv2<E> current = next;
while (current.next != null)
current = current.next;
LinkedListv2<E> newLL = new LinkedListv2<E>(newData, current, current.previous);
current.previous.next = newLL;
current.previous = newLL;
return true;
}
public void add(int index, E newData) throws IndexOutOfBoundsException {
// find where to add
LinkedListv2<E> current = next;
for (int i=0; i<index; i++) {
if (current.next == null) throw new IndexOutOfBoundsException( );
current = current.next;
}
LinkedListv2<E> newLL = new LinkedListv2<E>(newData, current, current.previous);
current.previous.next = newLL;
current.previous = newLL;
}
public E set(int index, E newData) throws IndexOutOfBoundsException {
// find where to set
LinkedListv2<E> current = next;
for (int i=0; i<index; i++) {
if (current.next == null) throw new IndexOutOfBoundsException( );
current = current.next;
}
if (current.data == null) throw new IndexOutOfBoundsException( );
E oldData = current.data;
current.data = newData;
return oldData;
}
public boolean remove(E goneData) {
// find goneData in the list
LinkedListv2<E> current = next;
while (current.next != null && !current.data.equals(goneData))
current = current.next;
if (current.next == null) return false;
current.previous.next = current.next;
current.next.previous = current.previous;
return true;
}
public boolean remove(int index) throws IndexOutOfBoundsException {
// find goneData in the list
LinkedListv2<E> current = next;
for (int i=0; i<index; i++) {
if (current.next == null) throw new IndexOutOfBoundsException( );
current = current.next;
}
if (current.data == null) throw new IndexOutOfBoundsException( );
current.previous.next = current.next;
current.next.previous = current.previous;
return true;
}
public boolean contains(E someData) {
// find someData in the list
LinkedListv2<E> current = next, previous = this;
while (current.next != null && !current.data.equals(someData)) {
previous = current;
current = current.next;
}
return current != null;
}
public E get(int index) throws IndexOutOfBoundsException {
// find location
LinkedListv2<E> current = next;
for (int i=0; i<index; i++) {
if (current.next == null) throw new IndexOutOfBoundsException( );
current = current.next;
}
if (current.data == null) throw new IndexOutOfBoundsException( );
return current.data;
}
public String toString( ) {
String answer = "[ ";
LinkedListv2<E> current = next;
while (current.next != null) {
answer += current.data.toString( ) + ", ";
current = current.next;
}
return answer + "]";
}
}