user2785061
user2785061

Reputation: 87

Remove in Linked structure

Hello There I am trying to test removeCity(), but it didn't remove any element that I provide. also the method addToList() if I use it in the City class I get "Exception in thread "main" java.lang.StackOverflowError" while it work fine in the test class

Any help ?

MyList

public class MyList<T> {
private  Node head;
private Node tail;

public MyList(){
    head = null;
    tail = null;
}

public void addToTail(T info){
    Node n;
    //case 1: empty List
    if(isEmpty()){
        n = new Node(info, null);
        head = n;
        tail = head;
    }
    //case 2: if the list is not empty
    else {
        n = new Node(info, null);
        tail.setNext(n);
        tail = n;
    }
}

public void addToHead(T info){
    Node n;
    //case 1: empty List
    if(isEmpty()){
        n = new Node(info, null);
        head = n;
        tail = head;
    }
    //case 2: if the list is not empty
    else {
        n = new Node(info, head);
        head = n;
    }
}

    public boolean removeHead(){
        //Case 1: if the list is empty
        if(isEmpty())
            return false;
        //case 2: if the list have at least one element
        else{
            Node n = head.getNext();
            head = n;
            return true;
        }

    }

    public boolean removeElement(String element){

        //cacs 1 if before is the head
        if(isEmpty())
            return false;

        if( ((City) head.getInfo()).getCode().equals(element)){
            removeHead();
            return true;
        }

        Node iter = head.getNext();
        Node prev = head;
        while(iter != null && !((City) head.getInfo()).getCode().equals(element)){
            iter = iter.getNext();
            prev = prev.getNext();
        }
        if(iter == null)
            return false;
        else{
            prev.setNext(iter.getNext());
            return true;
        }
    }

    //To check if the list is empty
    public boolean isEmpty(){
        if ( head == null)
            return true;
        else
            return false;
    }

Node

public class Node<T> {


    private T info;
    private Node next;

    public Node(){
        info = null;
        next = null;
    }

    public Node(T info, Node next){
        this.info = info;
        this.next = next;
    }

    public T getInfo(){
        return info;
    }

    public Node getNext(){
        return next;
    }

    public void setNext(Node next){
        this.next = next;
    }

    public void setInfo(T info){
        this.info = info;
    }

    }

City

    public class City implements Serializable {
        public static MyList<City> cityList = new MyList<City>();
        private String name;
        private String code;

        public City(String name, String code) {
           super();
           this.name = name;
           this.code = code;
           addToList(new City(name,code));
        }

        public void addToList(City toAdd){
           City.cityList.addToHead(toAdd);
        }

        public static void removeCity(String name){
            if( cityList.isEmpty()){
                System.out.println("The List is empty");
                return;
            }
            if ( cityList.removeElement(name) == true )
                System.out.println("The City was removed sucssesfully");
            else
                System.out.println("This city does not not exist");
        }

    }

Test

public class DummyTest {
    public static void main(String [] args){
        City x = new City("Ney York","NY");
        City y = new City("London","LD");
        System.out.println(City.cityList);
    }
}

Stacktrace

Exception in thread "main" java.lang.StackOverflowError
    at City.<init>(City.java:15)
    at City.<init>(City.java:18)
    at City.<init>(City.java:18)

Line 15 is the constructor

public City(String name, String code)

Line 18 is addToList

addToList(new City(name,code))

Upvotes: 1

Views: 82

Answers (2)

Bernd Elkemann
Bernd Elkemann

Reputation: 23560

Alexey already found the first error, here are 2 more:

public City(String name, String code) {
   super();
   this.name = name;
   this.code = code;
   addToList(new City(name,code));  // <- infinite recursion
}

is infinite recursion: the (constructor-)method calls the (constructor-)method.

It should probably be addToList(this);

Also: In mylist.java all the new Node(..) should be new Node<T>(..).

Upvotes: 2

Alexey A.
Alexey A.

Reputation: 1419

What I've spotted that you have an issue in your while loop in removeElement method. I am not sure if it will solve your issue. Could you also put a part of stacktrace here were do you get StackOverflowException.

    Node iter = head.getNext();
    Node prev = head;
    while(iter != null && !((City) head.getInfo()).getCode().equals(element)){
        iter = iter.getNext();
        prev = prev.getNext();
    }

this line

   while(iter != null && !((City) head.getInfo()).getCode().equals(element))

should be probably

   while(iter != null && !((City) iter.getInfo()).getCode().equals(element))

iter instead head

Upvotes: 2

Related Questions