java - 如何将元素添加到链表末尾

标签 java list stack

我正在尝试将一个元素添加到链接列表的末尾,但我不太确定如何完成此操作。这是我正在使用的方法的当前代码:

public void pushLast(T element) {
    LinearNode<T> temp = new LinearNode<T>(element);
    if (isEmpty()) {
        top = temp;
    } else {
        LinearNode<T> current = top;
        while (current.getNext() != null) {
            current = current.getNext();
        }
        current.setNext(temp);
    }

这看起来正确吗?这是我使用此方法的完整代码:

import java.lang.StringBuilder;

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;
            int pushLast;
        }

        @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 void pushLast(T element) {
            LinearNode<T> temp = new LinearNode<T>(element);
            if (isEmpty()) {
                top = temp;
            } else {
                LinearNode<T> current = top;
                while (current.getNext() != null) {
                    current = current.getNext();
                }
                current.setNext(temp);
            }
        }


        public T peek() throws EmptyCollectionException {

            return top.getElement();    
        }


        public boolean isEmpty() {

            return (top == null);   
        }


        public int size() {

            return count;
        }


        public String toString() {


            if (isEmpty()) {
                return " ";
            }

            StringBuilder sb = new StringBuilder(top.toString());
            LinearNode<T> next = top.getNext();

            while(next != null) {
                sb.append("\n").append(next.getElement());
                next = next.getNext();

            }

            return sb.toString();

        } // End of the toString method.

    } // End of method header.

} // End of class header.

注意:以防万一您想自己运行它,我提供了附加到上面文件的三个文件。

线性节点:

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

    public LinearNode() {
        next = null;
        element = null;
    }

    public LinearNode(T elem) {
        next = null;
        element = elem;
    }

    public LinearNode<T> getNext() {
        return next;
    }

    public void setNext(LinearNode<T> node) {
        next = node;
    }

    public T getElement() {
        return element;
    }


    public void setElement(T elem) {
        element = elem;
    }
}

空集合异常:

public class EmptyCollectionException extends RuntimeException {

    public EmptyCollectionException(String collection) {
        super("The " + collection + " is empty.");
    }
}

谢谢!

最佳答案

添加数据项后,当堆栈列表中有项时,您不会重置数据结构的当前成员,因此您可能会在 peek().添加第一个项目时,您似乎也没有重置 current 节点。尝试这个:

编辑:我想我是在太累的时候回答这个问题的,而且是不正确的。

关于java - 如何将元素添加到链表末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31778395/

相关文章:

java - 与 olap4j 兼容的内存和可嵌入 OLAP 实现

java - 无法访问 ArrayList 内容

python - 如何从使用函数创建的列表中选择特定字符

stack - 因子编程语言 : setting-up . 因子根配置

java - 漂亮的 GUI 布局

java - 依次调用不同类的 get 方法

c - 结构体动态链表中的内存分配

java - "Iterable<Element> cannot be cast to List<Element>"- 't ` 列表 ` a type of ` 是可迭代的吗?

Java 读取 .txt 文件并使用子字符串进行排序

程序集调用堆栈 - 术语问题