简体   繁体   English

从头开始实现ADT链表

[英]Implementing an ADT linked list from scratch

I've got a class project where I have to build an ADT-based linked list from scratch (meaning I can't use any standard Java ADTs) and then use that to sort a bunch of State objects (that each also contain a linked list of Cities ) alphabetically. 我有一个班级项目,必须从头开始构建基于ADT的链接列表(这意味着我不能使用任何标准Java ADT),然后使用该列表对一堆State对象(每个对象也包含一个链接对象)进行排序按字母顺序排列的Cities列表)。 The backbone of the code is, obviously, the handmade OrderedLinkedList class, and I'm having a lot of trouble figuring out how to implement, speficially, a findOrAdd method that iterates through the list and, if the argument passed is not already in the list, adds it in the appropriate spot (and returns the element if it's already there). 显然,代码的主干是手工的OrderedLinkedList类,并且在弄清楚如何实现遍历列表的findOrAdd方法时遇到很多麻烦,如果传递的参数还没有在列表中列表,将其添加到适当的位置(如果元素已经存在,则返回该元素)。 Most of the things I've read about implementing linked lists don't deal in ADTs and so it's difficult to convert that in my mind and still wrap my head around it. 我所读到的有关实现链接列表的大部分内容都不会在ADT中处理,因此很难在我的脑海中转换并仍然全神贯注。 My (admittedly incomplete) OLL code and its accompanying iterator: 我的(公认不完整的)OLL代码及其随附的迭代器:

import java.util.Iterator;

public class OrderedLinkedList<E extends Comparable<E>> implements Iterable<E> 
{
    private E first;
    private E next;
    private E last;
    private E current;
    private E temp;
    private int size;

    public OrderedLinkedList() 
    {
        this.first = null;
        this.next = null;
        this.last = null;
        this.current = null;
        this.size = 0;
    }

    public E findOrAdd(E element)
    {
        E returnVal = null;
        Iterator<E> listIter = this.iterator();

        if (this.first == null)
        {
            this.first = element;
            this.size++;
        }

        else 
            for (int i = 0; i < this.size; i++)
                {
                    if (listIter.next().compareTo(element) == 1 && listIter.hasNext() == false)
                    {
                        temp = this.first;
                        this.first = element;
                        this.next = temp;
                        this.size++;
                    }   
                    else if (listIter.next().compareTo(element) == 1 && listIter.hasNext() == true)
                        continue;
                    else if (listIter.next().compareTo(element) == 0)
                        returnVal = element;
                    else if (listIter.next().compareTo(element) == -1)
                    {
                        temp = this.next;
                        this.next = element;                        
                    }               
                }

        return returnVal;
    }

    public Iterator<E> iterator()
    {
        return new OrdListIterator<E>();
    }

    private class OrdListIterator<E> implements Iterator<E> 
    {
        private E nextNode;

        public OrdListIterator()
        {
            //maybe something needed here
        }

        public boolean hasNext()
        {
            return (next != null);
        }

        public E next()
        {       
            return (E) next;
        }

        public E first()
        {
            return (E) first;
        }

        public void remove()
        {
            //implement later
        }
    }
}

I've got compareTo() methods in the State and City classes that override the usual method but still function the same way. 我在StateCity类中具有compareTo()方法,这些方法重写了通常的方法,但仍以相同的方式起作用。 Where am I going wrong in findOrAdd ? findOrAdd在哪里出错? How am I going wrong? 怎么错了? I'm not looking for a full correction of the code or anything; 我不是在寻找代码或任何东西的完整更正。 I'm about 99% sure everything under that else block is abysmal. 我大约有99%的人确定else块下的所有else都是糟糕的。 I just need a push in the right direction: someplace to get a foothold. 我只需要朝着正确的方向努力:在某个地方立足。 I'd hugely appreciate any advice. 我非常感谢任何建议。

I believe that you issue may lie in the fact that you are calling listIter.next() for each condition may mean that you are pushing the iterator through at each check. 我认为,您的问题可能出在您针对每种条件调用listIter.next()的事实,这可能意味着您在每次检查时都将迭代器推入。 Possibly you should store this at the beginning of the loop then use the single object in your comparisons... 可能应该将其存储在循环的开始,然后在比较中使用单个对象...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM