我的LinkedList实现的构造函数和主方法应该包含哪些内容



对于这样的linkedlist类,是否需要进入主方法或构造函数方法?此外,我认为我的程序运行良好,但我认为我可能会使用更多的代码行,这些代码行只是为了对字符串列表进行排序。添加/删除方法和下面的OrderedListNode类中是否存在任何冗余或逻辑错误?我应该将LinkedList类中的所有变量设为私有变量吗?

package orderedlinkedlist;
/**
 *  Class OrderedLinkedList.
 *
 *  This class functions as a linked list, but ensures items are stored in ascending order.
 */
public class OrderedLinkedList
{
    public static void main(String args[]){
    }
    /** return value for unsuccessful searches */
    private static final OrderedLinkedList NOT_FOUND = null;

    /** current number of items in list */
    public int theSize; //was private
    /** reference to list header node */
    private OrderedListNode head;
    /** reference to list tail node */
    private OrderedListNode tail;
    /** current number of modifications to list */
    public int modCount;        

    /**
     *  Create an instance of OrderedLinkedList.
     *
     */
    public OrderedLinkedList()
    {            
        clear(); 
    }

    public boolean add(String obj) //is it ok that you changed from comparable to string?
    {
        OrderedListNode newNode = new OrderedListNode(obj); // placed at end of list
        if(this.isEmpty()){ //
            head.setNextNode(newNode);
            newNode.setNextNode(tail);
            newNode.setPreviousNode(head);
            tail.setPreviousNode(newNode);
        } else if(obj == null) {
            head.getNext().setPreviousNode(newNode);
            newNode.setNextNode(head.getNext());
            head.setNextNode(newNode);
            newNode.setPreviousNode(head);
        } else {
            int compareToNode;
            for(OrderedListNode node = head.getNext(); node != tail; node = node.getNext()){
                compareToNode = obj.compareToIgnoreCase((node.getData() == null)? "": node.getData().toString());                    
                if(compareToNode < 0){
                    OrderedListNode nodeBefore = node.getPrevious();
                    OrderedListNode nodeAfter = node;
                    nodeBefore.setNextNode(newNode);
                    nodeAfter.setPreviousNode(newNode); 
                    newNode.setNextNode(nodeAfter);
                    newNode.setPreviousNode(nodeBefore);  
                    break;                        
                } else if(node.getNext() == tail) {
                    OrderedListNode nodeBefore = node;
                    OrderedListNode nodeAfter = node.getNext();
                    nodeBefore.setNextNode(newNode);
                    nodeAfter.setPreviousNode(newNode); 
                    newNode.setNextNode(nodeAfter);
                    newNode.setPreviousNode(nodeBefore);  
                    break;
                }
            }                
        }
        theSize++;
        modCount++;
        return true;
    }
    public boolean remove(String obj) //removes first string obj instance it finds?
    {
        // TODO: implement this method (7 points)
        for(OrderedListNode node = head.getNext();node != tail; node=node.getNext()){
            if(node.getData() == obj){
                OrderedListNode nodeBefore = node.getPrevious();
                OrderedListNode nodeAfter = node.getNext();
                nodeBefore.setNextNode(nodeAfter);
                nodeAfter.setPreviousNode(nodeBefore); 
                node = null;
                theSize--;
                modCount++;                    
                return true;
            }                
        }                    
        return false;
    }

    public void clear()
    {
        // reset header node
        head = new OrderedListNode("HEAD", null, null);
        // reset tail node
        tail = new OrderedListNode("TAIL", head, null);
        // header references tail in an empty LinkedList
        head.next = tail;
        // reset size to 0
        theSize = 0;
        // emptying list counts as a modification
        modCount++;
    }
    public boolean isEmpty()
    {
        return theSize == 0;
    }
    public int size()
    {
        return theSize;
    }

    public String toString()
    {
            String s = "";
        OrderedListNode currentNode = head.next;
            while (currentNode != tail)
            {
            s += currentNode.getData(); //getData was "theItem.toString()"
            if (currentNode.next != tail)
            {
                s += ", ";
            }
            currentNode = currentNode.next;
            }       
            return s;
    }

/**************************************************************************
 * Inner Classes
 *************************************************************************/

/**
 *  Nested class OrderedListNode.
 *
 *  Encapsulates the fundamental building block of an OrderedLinkedList
 *  contains a data item, and references to both the next and previous nodes
 *  in the list
 */ 

    public class OrderedListNode {
        private Comparable data;
        private OrderedListNode next;
        private OrderedListNode previous;
        public OrderedListNode() {
          this.data = null;
          this.next = null;
        }           
        public OrderedListNode(Comparable inputData) { //check this
            this.data = inputData;                                                                 
        }                        
        public OrderedListNode(Comparable inputData, OrderedListNode previous, OrderedListNode next) {
            this.data = inputData;
            this.previous = previous;              
            this.next = next;
        }
        public Comparable getData() { 
            return data;
        }
        public void setData(Comparable data) {
            this.data = data;
        }
        public OrderedListNode getNext() {
            return next;
        }
        public void setNextNode(OrderedListNode next) {
            this.next = next;
        }
        public OrderedListNode setPreviousNode(OrderedListNode previous) {
            this.previous = previous;
            return previous;
        }
        private OrderedListNode getPrevious() {      
            return getNodeAt(indexOf(this)>=0 ? indexOf(this): theSize);//0
        }            
        private OrderedListNode getNodeAt(int givenPosition){               
            OrderedListNode currentNode = head;
            for(int counter = 0; counter < givenPosition; counter++){
                currentNode = currentNode.getNext();
            }                
            return currentNode;
        }
        private int indexOf(OrderedListNode givenNode){
            int count = 0;
            for(OrderedListNode node = head.getNext();node != tail; node=node.getNext(), count++){
                if(givenNode == node)
                    return count;
            }                
            return -1;
        }
    }
}


package orderedlinkedlist;
public class testClass {
    public static void main(String args[]){
        OrderedLinkedList list = new OrderedLinkedList();  
        list.add("tesla");
        list.add(null);
        list.add("walnuts"); //move these to main method
        list.add("chocolate");
        list.add("swordfish");
        list.add(null);            
        list.remove("chocolate");
        list.add("pizza");
        list.add("apple");
        list.add(null);      
        list.add("1");       
        list.add("zebra");       
        System.out.println(list.toString());  
        System.out.println("Items in this list: " + list.theSize); 
        System.out.println("Modifications made: " + list.modCount);
    }
}

设计的通用实用程序类不应该有主方法,因为其他人应该使用这个类。一个例子是您的测试类,它使用您的代码并有一个主方法。就你的代码而言,

  • 我认为这不安全。这可能不是问题,但通常建议您记录代码不是线程安全的
  • 你的代码是编译的吗?由于Comparable 没有重载+运算符,因此该语句应该给出编译问题

    s+=currentNode.getData()//getData是"theItem.toString()"

  • 您可能希望避免在toString方法中使用+运算符,而是使用更高效的StringBuilder

Size和modCount不应是公共的。

按照Java约定,您应该添加一个构造函数,将另一个Collection作为arg,例如

public OrderedLinkedList(Collection in)

从该集合复制的。

相关内容

  • 没有找到相关文章

最新更新