删除链接结构

时间:2013-12-13 13:06:18

标签: java linked-list

你好我在尝试测试removeCity(),但它没有删除我提供的任何元素。 还有方法addToList(),如果我在City类中使用它,我得到“线程中的异常”主“java.lang.StackOverflowError”,而它在测试类中工作正常

任何帮助?

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;
    }

节点

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;
    }

    }

城市

    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");
        }

    }

测试

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);
    }
}

堆栈跟踪

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)

第15行是构造函数

  

public City(字符串名称,字符串代码)

第18行是addToList

  

addToList(新城市(名称,代码))

2 个答案:

答案 0 :(得分:2)

我发现您在while方法的removeElement循环中遇到问题。 我不确定它是否能解决您的问题。 你能不能在这里放置一部分堆栈跟踪,你得到StackOverflowException

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

这一行

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

应该是

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

iter代替head

答案 1 :(得分:2)

Alexey已经发现了第一个错误,这里还有2个错误:

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

是无限递归:(constructor-)方法调用(constructor-)方法。

应该是addToList(this);

另外:在mylist.java中,new Node(..)应该是new Node<T>(..)