Java数据结构Stack使用Linked List

时间:2017-07-03 13:15:21

标签: java data-structures

我试图在Java中使用LinkedList实现Stack(推送)然后我意识到我遇到了一个大问题,即使我在附加新节点时它们在遍历时并不存在。

我意识到我一次又一次地使用先前定义的Node对象,因此重写了数据。像下面的代码一样:

package LLAPPLICATION;

import java.util.Scanner;

class Node {
    Node next;
    int data;
}

public class Stack {
    Node first;

    void push(Node node) {
        if (first == null) {
            first = node;
            first.next = null;

        } else if (first.next == null) {
            first.next = node;
            node.next = null;
        } else {
            Node temp = new Node();
            temp = first;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = node;
            node.next = null;
        }
    }                

    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        Stack stk = new Stack();
        Node tmp = new Node();
        char cho;
        do {
        System.out.print("Enter the element to insert ::");
                        /* This is the part where it got tricky.If I use tmp by declaring it at the top it just wont happen.I think its because of garbage collection i.e. every iteration causes new instance i.e tmp to be created and hence preventing it from being overwritten*/
                        tmp.data = inp.nextInt();
                        stk.push(tmp);
                        System.out.print("Do you want to PUSH again..(Y/N):");
                        cho = inp.next().charAt(0);
                    } while (cho == 'Y' || cho == 'y');

      }
}

然后我就这样做了它并且它起作用了。现在我真的很困惑,我认为这是因为垃圾收集但不确定。

package LLAPPLICATION;

import java.util.Scanner;

class Node {
    Node next;
    int data;
}

public class Stack {

    Node first;

    void push(Node node) {
        if (first == null) {
            first = node;
            first.next = null;

        } else if (first.next == null) {
            first.next = node;
            node.next = null;
        } else {
            Node temp = new Node();
            temp = first;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = node;
            node.next = null;
        }
    }

    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        Stack stk = new Stack();
        char cho;

                    do {
        /*If I declare a tmp inside of the loop it does*/
                        Node tmp = new Node();
                        System.out.print("Enter the element to insert ::");
                        tmp.data = inp.nextInt();
                        stk.push(tmp);
                        System.out.print("Do you want to PUSH again..(Y/N):");
                        cho = inp.next().charAt(0);
                  } while (cho == 'Y' || cho == 'y');
        }
}

1 个答案:

答案 0 :(得分:2)

你几乎都错了。这是一个堆栈。你关心的是上次和上次。推送只需要使this.last-> node.previous和节点成为最后一个。 Pop只需要做相反的事情。

不需要遍历。

void push(Node node){
    if (this.last != null)
        node.previous = this.last;
    this.last = node;
    size++
}

Node pop(){
   if (this.last == null){
   // Exception/etc
   }
   Node n = this.last;
   this.last = n.previous;
   size--
   return node;
}
相关问题