gpt4 book ai didi

java - 为应用程序类创建双向链表

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:45:59 24 4
gpt4 key购买 nike

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

第一个问题是:我不断在 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;
}
}
}
}
}

最佳答案

有多种不同的错误。

您尝试调用 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()来自电话。

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

关于java - 为应用程序类创建双向链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23073807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com