2017-07-03 9 views
-2

JavaでLinkedListを使用してStack(push)を実装しようとしていたが、移動中に新しいノードを追加していたにもかかわらず大きな問題があることに気がつきました。Javaデータ構造Stacked Linked Listを使用して

私は以前に何度も定義された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'); 
     } 
} 
+0

@efekctive私はそれがあなたの質問に質問が本当にありません作業 –

+0

を行います願っています両方のバージョンを掲載しています。そしてあなたのコードは簡単に助けてくれる方法で最小限ではありません。 –

+0

@ E_net4ご迷惑をおかけして申し訳ありませんが、これは私の最初の質問です。ありがとうございました!私は質問を正しく行う方法を知っています。次回は疑問を尋ねます。誰もが簡単にできるように標準に従います。 –

答えて

2

あなたはかなり間違っています。これはスタックです。あなたは最後と前を気にします。 pushはthis.last-> node.previousにする必要があり、nodeは最後になります。ポップは反対をする必要があります。

トラバーサルは不要です。

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; 
} 
関連する問題