【问题标题】:Pushing to a stack that uses a Linked List in java推送到使用 Java 中的链表的堆栈
【发布时间】:2013-10-06 01:00:55
【问题描述】:

关于我应该将什么传递给推送方法的问题。我想将一系列 .txt 文件中的信息推送到我的 main 方法内的 wiki 堆栈中,以便我以后可以弹出它并使用它。这是 netbeans 给我的错误:

没有找到适合 push() 的方法 方法 FancyStack.push(FancyStack>) 不适用 (实际参数列表和形式参数列表的长度不同) 方法 FancyStack.push(Node) 不适用 (实际参数列表和形式参数列表的长度不同)

如果有人想了解更多信息,请告诉我。

FancyStack<Node<WikiObjects>> wikis = new FancyStack<Node<WikiObjects>>();
FancyStack<Node<WikiObjects>> titles = new FancyStack<Node<WikiObjects>>();
WikiEdits edits = new WikiEdits(args);
String titleName = edits.title();
String editID = edits.editID();


    while(edits.moveToNext() != false){

        wikis.push();
        }

提前致谢!

编辑:这是我的 FancyStack

import java.util.*;

public class FancyStack<E> {

//pieced together linked list
private int cnt;
private Node<E> head;
public FancyStack<E> stack;
public FancyStack<E> s;

public FancyStack() {
    head = null;
    cnt = 0;
}

public void push(E item) { //THIS WORKS
    //your average kind of pushing an item onto stack
    Node<E> newNode = new Node<E>(item);
    newNode.setLink(head);
    head = newNode;
    cnt++;
}

public void push(FancyStack<E> s) { //THIS WORKS
    //pushes all elements of FancyStack s into a new stack (this)
    //however stack this is in reverse order from FancyStack<E> s
    if (s.isEmpty()) {
        throw new NoSuchElementException("Empty Stack");
    }

    while (!s.isEmpty()) {
        Node<E> element = s.head;
        this.push(element.getInfo());
        s.pop();
        element = element.getLink();
    }

}

public boolean isEmpty() {
    return head == null;
}

public int size() {
    return cnt;
}

public E pop() { //THIS CORRECT
    if (isEmpty()) {
        throw new NoSuchElementException("Stack underflow");
    } else {
        E item = head.item;
        head = head.link;
        cnt--;
        return item;
    }
}

public E peek() { //THIS WORKS
    if (isEmpty()) {
        throw new NoSuchElementException("Stack underflow");
    }
    return head.item;
}

public FancyStack<E> reversed() {
  /*  if (this.isEmpty()) {    // want to test exceotion error with this commented out.
        throw new NoSuchElementException("Empty Stack");
    }*/
    FancyStack<E> newthis = new FancyStack<E>();
    while (!this.isEmpty()) { //elmt short for 'element'
        Node<E> elmt = this.head;
        newthis.push(elmt.getInfo());
        this.pop();
        elmt = elmt.getLink();
    }
    return newthis;

}

}

【问题讨论】:

    标签: java linked-list stack push


    【解决方案1】:

    您已指定 wikisFancyStackNode&lt;WikiObjects&gt; 对象。您必须推送一个Node&lt;WikiObjects&gt; 对象。 FancyStack 还允许你推送另一个FancyStack。在我看来,这是一个名称不佳的 API,因为它没有按照它所说的去做。它不是推送FancyStack,而是推送FancyStack 中的所有元素。它应该命名为pushAll

    根据提供的 FancyStack 源编辑

    【讨论】:

    • 有道理。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-12-19
    • 2023-03-24
    • 2011-04-18
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多