推送到在java中使用链接列表的堆栈

时间:2013-10-06 01:00:56

标签: java linked-list stack push

关于我应该将什么传递到推送方法的问题。我想将一系列.txt文件中的信息推送到我的main方法中的wikis堆栈中,以便我以后可以弹出它并使用它。这是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;

}

}

1 个答案:

答案 0 :(得分:1)

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

根据提供的FancyStack来源

进行编辑