Java 为应用程序类创建双链接列表
我刚开始编程,在这里遇到了一些错误 第一个问题是:我在DLLApp.java中不断收到一个编译器错误,它说“类DLL中的方法不能应用于给定的类型;”。我理解它的意思,因为我给它一个字符串参数,但我不知道如何使它成为正确的泛型类型 其次,由于这还没有编译,我不能检查我的程序,但是我的DLL中的方法看起来怎么样?我仍在努力理解DLL数据结构 DLL类Java 为应用程序类创建双链接列表,java,algorithm,list,doubly-linked-list,Java,Algorithm,List,Doubly Linked List,我刚开始编程,在这里遇到了一些错误 第一个问题是:我在DLLApp.java中不断收到一个编译器错误,它说“类DLL中的方法不能应用于给定的类型;”。我理解它的意思,因为我给它一个字符串参数,但我不知道如何使它成为正确的泛型类型 其次,由于这还没有编译,我不能检查我的程序,但是我的DLL中的方法看起来怎么样?我仍在努力理解DLL数据结构 DLL类 public class DLL<E> { /** A reference to the first node in this D
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
公共类DLL
{
/**此DLL中第一个节点的引用*/
私有节点优先;
/**此DLL中最后一个节点的引用*/
最后是私有节点;
/**
*构造一个空DLL。
*/
公共DLL()
{
第一个=空;
last=null;
}
/**
*返回此DLL中的第一个元素,如果DLL为空,则返回null。
*
*@返回此DLL中的第一个元素,如果DLL为空,则返回null。
*/
公共E getFirst()
{
返回(first!=null?first.element:null);
}
/**
*返回此DLL中的最后一个元素,如果DLL为空,则返回null。
*
*@返回此DLL中的最后一个元素,如果DLL为空,则返回null。
*/
公共E getLast()
{
返回(last!=null?last.element:null);
}
/**
*返回此DLL中的节点数。
*
*@返回此DLL中的节点数。
*/
公共整数大小()
{
int size=0;
for(节点curr=first;curr!=null;curr=curr.next)
{
大小++;
}
返回大小;
}
/**
*将元素添加到此DLL的开头。
*
*@param elem要添加到此DLL开头的元素。
*/
公共无效添加优先(E元素)
{
插入(elem,空);
}
/**
*将元素添加到此DLL的末尾。
*
*@param elem要添加到此DLL末尾的元素。
*/
公共无效添加最后一个(E元素)
{
插入(元素,最后一个);
}
/**
*在元素“prev”之后添加新元素(如果需要,则在此DLL末尾添加新元素)
*找不到“prev”)。
*
*@param elem要添加到此DLL的新元素。
*@param prev在后面添加“elem”的元素。
*/
public void addAfter(E elem,E prev)//使用搜索方法,然后插入
{
//Node newNode=新节点(elem,prev,next);//我需要吗?
插入(elem,search(prev));//不确定?
}
/**
*从此DLL中删除给定元素。
*
*@param elem要从此DLL中删除的元素。
*/
public void delete(E elem)//创建节点等
{
//使用搜索,然后使用返回和转发链接,而不是整个方法
while(first!=null)//在列表中移动
{
if(first==elem)//如果下一个元素是删除编号
{
//重新分配节点跳过第一个如此解除分配的节点(我认为是问题)
first.prev=first.next;
first.next=first.prev;
}
first=first.next;//在列表中移动
}
}
/**
*返回DLL的字符串表示形式以允许System.out.println(aList)。
*
*@返回此DLL的字符串表示形式。
*/
公共字符串toString()
{
字符串结果=“[”;
for(节点curr=first;curr!=null;curr=curr.next)
{
结果+=当前值;
如果(当前!=上次)
{
结果+=“,”;
}
}
返回结果+“]”;
}
/**
*返回此DLL中所有元素的字符串表示形式,以
*从最后到第一道菜。
*
*@按相反顺序返回此DLL的字符串表示形式。
*/
公共字符串反向限制()
{
字符串结果=“[”;
for(节点curr=last;curr!=null;curr=curr.prev)
{
结果+=当前值;
if(curr!=第一个)
{
结果+=“,”;
}
}
返回结果+“]”;
}
/**
*查找包含给定元素的第一个节点。
*
*@param elem要搜索的元素。
*@返回包含给定元素的第一个节点,如果没有节点,则返回null
*包含元素。
*/
私有节点搜索(E元素)//帮助
{
for(节点curr=first;curr!=null;curr=curr.next)
{
if(当前元素等于(元素))
{
return curr;//我是这样返回元素的吗?
}
}
返回null;
}
/**
*在元素“prev”之后插入新元素。如果“prev”为null,则
*在此DLL前面插入“elem”。
*
*@param elem要插入此DLL的新元素。
*@param prev在后面插入'elem'的元素。如果'prev'为空,则插入
*“elem”位于此DLL的开头。
*/
private void insert(E elem,Node prev)//帮助
{
Node newNode=新节点(elem,prev,null);
//如果列表为空,则添加到前面
if(first==null)
{
第一个=新节点;
last=newNode;
}
else if(prev==null)
{
//addFirst(newNode);
first.prev=newNode;
newNode.next=first;
第一个=新节点;
}
else if(prev.next==null)//if位于列表末尾
{
prev.next=newNode;
last=newNode;
}
其他的
{
newNode.next=prev.next;
newNode.next.prev=newNode;
prev.next=newNode;
}
}
/**
*一个简单的容器类,它包含一个元素和对
*上一个和下一个节点。
*/
私有静态类节点
{
/**此节点包含的元素*/
私人电子元件;
/**在此节点之前对该节点的引用*/
私有节点prev;
/**对该节点之后的节点的引用*/
私有节点下一步;
/**
*使用给定元素和相邻节点创建新节点。
*
*@param elem此节点包含的元素。
*@param prev节点
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;
}
}
}
}
}