简体   繁体   中英

Creating a doubly Linked List for an application class

I'm new programming and have run into some errors here.

First problem is: I keep getting a compiler error in DLLApp.java saying "method in class DLL cannot be applied to given types;". I understand what it's saying, because I'm giving it a string Argument, but I can't figure out how to actually make it the correct Generic type.

Secondly, since this is not compiling yet, I can't check my program, but how do my methods in DLL look? I'm still trying to understand the DLL Data structure.

DLL class

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

Test

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

There are a variety of different errors.

You try to call insert(String, String) , which would correspond to insert(E, E) , but there's only an insert(E, Node<E>) method. You'll need to either call search to get a Node<String> object (and make it public / protected to allow this), or change insert to take (E, E) and call search from within that function.

insert is private , thus you can't call it from outside the class - it should be made public / protected .

addLast takes 1 parameter only - presumably you should just remove , tokens.next() from the call.

Neither toString nor reverseString takes any parameters. My guess would be that toString shouldn't be in a loop, and neither should obviously take a tokens.next() parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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