实现一个 toString 来打印链表的内容

Implementing a toString to print the content of a linked list

所以我正在尝试使用链接堆栈 class,我可以打印以下代码:

STACK TESTING    
The stack contains:

4

8

8

9

The size of the stack is: 3

The stack contains:

但是我在执行 toString 打印出其余部分以获得最终结果时遇到了问题:

STACK TESTING

The stack contains:

empty

4

8

8

9

The size of the stack is: 3

The stack contains:

9

7

3

我觉得这是需要完成的非常基本的事情,但我倾向于过度考虑所有事情。我最初尝试创建 class 的实例,然后在其上调用 toString() ,但它没有按照我的要求进行。如果我碰巧尝试别的东西,我往往会收到 cannot convert from LinearNode<T> to blah blah blah 的消息。实际打印其余内容的最佳方法是什么?

这是我的代码:

import java.util.Iterator;
    public class Murray_A05Q3 {
    public static void main(String[] args) {

        LinkedStack<Integer> stack = new LinkedStack<Integer>();
        System.out.println("STACK TESTING");
        System.out.println("The stack contains:\n" + stack.toString());
        stack.push(3);
        stack.push(7);
        stack.push(4);
        System.out.println(stack.peek());
        stack.pop();        
        stack.push(9);
        stack.push(8);
        System.out.println(stack.peek());        
        System.out.println(stack.pop());
        System.out.println(stack.peek());        
        System.out.println("The size of the stack is: " + stack.size());
        System.out.println("The stack contains:\n" + stack.toString());        

    } // End of method header.

    public static class LinkedStack<T> implements StackADT<T> {
        private int count;  
        private LinearNode<T> top; // serves as node class

     // Creating an empty stack
        public LinkedStack() {
            count = 0;
            top = null;
        }

     // Adds the specified element to the top of this stack.      
        @Override
        public void push(T element) {
            LinearNode<T> temp = new LinearNode<T>(element);
            temp.setNext(top);
            top = temp;
            count++;
        }

        public T pop() throws EmptyCollectionException {
            if (isEmpty())
                throw new EmptyCollectionException("stack");

                T result = top.getElement();
                top = top.getNext();
                count--;

             return result;
         }

         public T peek() throws EmptyCollectionException {
            return top.getElement();    
         }

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

         public int size() {
             return count;
         }

            /**
             * Returns a string representation of this stack. The string has the
             * form of each element printed on its own line, with the top most
             * element displayed first, and the bottom most element displayed last.
             * If the list is empty, returns the word "empty".
             * @return a string representation of the stack
             */

           public String toString() {
               //String LinkedStack = "null";
               //return LinkedStack.toString();
               return top;
           } // End of the toString method.
    } // End of method header.
} // End of class header.

线性节点:

public class LinearNode<T>
{
    private LinearNode<T> next;
    private T element;

    /**
     * Creates an empty node.
     */
    public LinearNode()
    {
        next = null;
        element = null;
    }

    /**
     * Creates a node storing the specified element.
     * @param elem element to be stored
     */
    public LinearNode(T elem)
    {
        next = null;
        element = elem;
    }

    /**
     * Returns the node that follows this one.
     * @return reference to next node
     */
    public LinearNode<T> getNext()
    {
        return next;
    }

    /**
     * Sets the node that follows this one.
     * @param node node to follow this one
     */
    public void setNext(LinearNode<T> node)
    {
        next = node;
    }

    /**
     * Returns the element stored in this node.
     * @return element stored at the node
     */
    public T getElement()
    {
        return element;
    }

    /**
     * Sets the element stored in this node.
     * @param elem element to be stored at this node
     */
    public void setElement(T elem)
    {
        element = elem;
    }
}

这是我第一次使用链表,所以非常感谢任何帮助。

您的问题中缺少 class LinearNode 的代码,但我认为您可以这样做:

public String toString() {
    if(isEmpty()) {
        return "";
    }
    StringBuilder sb = new StringBuilder(top.toString());
    LinearNode<T> next = top.next();
    while(next != null) {
        sb.append("\n").append(next);
        next = next.next();
    }
    return sb.toString();
}