为应用程序类创建双向链接列表

时间:2014-04-15 02:45:24

标签: java algorithm list doubly-linked-list

我是新的编程,并且在这里遇到了一些错误。

第一个问题是:我一直在DLLApp.java中遇到编译错误,说"类DLL中的方法不能应用于给定的类型;"。我理解它的含义,因为我给它一个字符串参数,但我无法弄清楚如何使它成为正确的通用类型。

其次,由于这还没有编译,我无法检查我的程序,但我的DLL中的方法如何?我还在尝试理解DLL数据结构。

DLL类

public class DLL<E>
{

   /** A reference to the first node in this DLL. */
   private Node<E> first;

   /** A reference to the last node in this DLL. */
   private Node<E> last;

   /**
    *  Construct an empty DLL.
    */
   public DLL()
   {
      first = null;
      last = null;
   }

   /**
    * Return the first element in this DLL, or null if DLL empty.
    *
    * @return  The first element in this DLL, or null if DLL empty.
    */
   public E getFirst()
   {
      return (first != null ? first.element : null);
   }

   /**
    * Return the last element in this DLL, or null if DLL empty.
    *
    * @return  The last element in this DLL, or null if DLL empty.
    */
   public E getLast()
   {
      return (last != null ? last.element : null);
   }

   /**
    * Return the number of nodes in this DLL.
    *
    * @return  The number of nodes in this DLL.
    */
   public int size()
   {
      int size = 0;

      for (Node<E> curr = first; curr != null; curr = curr.next)
      {
         size++;
      }

      return size;
   }

   /**
    * Add an element to the start of this DLL.
    *
    * @param elem  An element to add to the start of this DLL.
    */
   public void addFirst(E elem)
   {
      insert(elem, null);
   }

   /**
    * Add an element to the end of this DLL.
    *
    * @param elem  An element to add to the end of this DLL.
    */
   public void addLast(E elem)
   {
      insert(elem, last);
   }

   /**
    * Add a new element after the element 'prev' (or at the end of this DLL if
    * 'prev' isn't found).
    *
    * @param elem  A new element to add to this DLL.
    * @param prev  An element to add 'elem' after.
    */
   public void addAfter(E elem, E prev) //use search method then insert
   {
      //Node<E> newNode = new Node<E>(elem, prev, next); //do i need?

      insert(elem, search(prev)); //not sure?
   }

   /**
    * Delete a given element from this DLL.
    *
    * @param elem  An element to delete from this DLL.
    */
   public void delete(E elem) //create node etc
   {
      //Use search then use back and forward links instead of this whole method
      while (first != null) //move through list
      {
         if (first == elem) //if the next element is the deleting number
         {
            //reassign nodes skipping the first so deallocated(I think problem)

            first.prev = first.next;
            first.next = first.prev;
         }

         first = first.next; //move through the list
      }
   }

   /**
    * Return a string representation of DLL to allow System.out.println(aList).
    *
    * @return  A String representation of this DLL.
    */
   public String toString()
   {
      String result = "[";
      for (Node<E> curr = first; curr != null; curr = curr.next)
      {
         result += curr;

         if (curr != last)
         {
            result += ", ";
         }
      }

      return result + "]";
   }

   /**
    * Return a string representation of all the elements in this DLL, in 
    * last-to-first order.
    *
    * @return  A String representation of this DLL in reverse order.
    */
   public String reverseString()
   {
      String result = "[";

      for (Node<E> curr = last; curr != null; curr = curr.prev)
      {
         result += curr;
         if (curr != first)
         {
            result += ", ";
         }
      }

      return result + "]";
   }

   /**
    * Find the first Node which contains the given element.
    *
    * @param elem  The element to search for.
    * @return The first Node containing the given element, or null if no Node
    *         contains the element.
    */
   private Node<E> search(E elem) //HELP
   {
      for (Node<E> curr = first; curr != null; curr = curr.next)
      {
         if (curr.element.equals(elem))
         {
            return curr; //am i returning the element like this?
         }
      }

      return null;
   }

   /**
    * Insert a new element after the element 'prev'.  If 'prev' is null then
    * insert 'elem' at the front of this DLL.
    *
    * @param elem  A new element to insert into this DLL.
    * @param prev  An element to insert 'elem' after. If 'prev' is null insert
    * 'elem' at the start of this DLL.
    */
   private void insert(E elem, Node<E> prev) //HELP
   {
      Node<E> newNode = new Node<E>(elem, prev, null);
         //if list is empty, add to front
      if (first == null)
      {
         first= newNode;
         last = newNode;
      }
      else if (prev == null)
      {
         //addFirst(newNode);
         first.prev = newNode;
         newNode.next = first;
         first = newNode;
      }
      else if (prev.next == null) //if at end of list
      {
         prev.next = newNode;
         last = newNode;
      }
      else
      {
         newNode.next = prev.next;
         newNode.next.prev = newNode;
         prev.next = newNode;
      }
   }

   /**
    * A simple container class which holds an element, and references to
    * previous and next Nodes.
    */
   private static class Node<E>
   {

      /** The element this Node holds. */
      private E element;

      /** A reference to the Node before this one. */
      private Node<E> prev;

      /** A reference to the Node after this one. */
      private Node<E> next;


      /** 
       * Create a new Node with the given element, adjacent nodes.
       *
       * @param elem  The element this Node holds.
       * @param prev  The Node which comes before this one.
       * @param next  The Node which comes after this one.
       */
      public Node(E elem, Node<E> prev, Node<E> next)
      {
         this.element = elem;
         this.prev = prev;
         this.next = next;
      }

      /** 
       * Return a string representation of this Node.
       *
       * @return A string representation of this Node.
       */
      public String toString()
      {
         return element.toString();
      }

   } // end class Node

} // end class DLL

测试

package lab15;

import java.util.Scanner;

public class DLLApp
{

   public static void main(String[]args)
   {
      Scanner input = new Scanner(System.in);
      DLL<String> dll = new DLL<String>();

      while (input.hasNext())
      {
         handleLine(input.nextLine(), dll);
      }
   }

   public static void handleLine(String values, DLL<String> list)
   {
      Scanner tokens = new Scanner(values);

      if (tokens.hasNext("[aesdpr]"))
      {
         char command = tokens.next().charAt(0);

         switch (command)
         {
           case 'a':

            while (tokens.hasNext())
            {
              String nextToke = tokens.next();
               if (tokens.hasNext())
               {
                  list.insert(nextToke, tokens.next());
               }
            }
            break;

           case 'e':

            while (tokens.hasNext())
            {
               String nextToke = tokens.next();
               if (tokens.hasNext())
               {
                  list.addLast(nextToke, tokens.next());
               }
            }
            break;

         case 's':

            while (tokens.hasNext())
            {
               list.addFirst(tokens.next());
               }
            break;
         case 'd':
            if (tokens.hasNext())
            {
               list.delete(tokens.next());
            }
            break;

         case 'p':

            while (tokens.hasNext())
            {
               list.toString(tokens.next());
            }
            break;

         case 'r':

            if (list.size() != 0)
             {
               list.reverseString(tokens.next());
               break;
            }
         }
      }
   }
}

1 个答案:

答案 0 :(得分:3)

有各种不同的错误。

您尝试拨打与insert(String, String)对应的insert(E, E),但只有insert(E, Node<E>)方法。您需要致电search以获取Node<String>对象(并将其设为public / protected以允许此项),或更改insert (E, E)并在该函数中调用search

insertprivate,因此您无法在课堂外调用它 - 应该public / protected

addLast仅获取1个参数 - 大概您应该从通话中删除, tokens.next()

toStringreverseString都不接受任何参数。我的猜测是toString不应该在循环中,显然也不应该使用tokens.next()参数。