2017-09-25 44 views
0

我在將值添加到鏈接堆棧時遇到問題,至少將值打印出來。我甚至不確定我是否正確編碼,甚至將值添加到堆棧。我爲此創建了3個類,一個Node類,一個LinkedStack類和一個Driver以運行該程序。在自定義LinkedStack方法中沒有添加toString()方法

這裏是節點類:

public class Node 
{ 
    int data; 
    Node link; 

    //contructor 
    public Node(int d) 
    { 
     data = d; 
     link = null; 
    } 

    public int getData() 
    { 
     return data; 
    } 

    public Node getLink() 
    { 
     return link; 
    } 

    public void setData(int d) 
    { 
     data = d; 
    } 

    public void setLink(Node n) 
    { 
     link = n; 
    } 
} 

這裏是LinkedStack類:

import java.util.NoSuchElementException; 

public class LinkedStack 
{ 

    //declare variables 
    private Node top; 
    private int size; 

    //constructor to initialize variables 
    public LinkedStack() 
    { 
     size = 0; 
     top = null; 
    } 

    //push method to add numbers to the stack 
    public void push(int value) 
    { 
     if(!isEmpty()) 
     { 
      top.setData(value); 
      size++; 
     } 
    } 

    //method to remove the top number in the stack 
    public int pop() 
    { 
     Node temp = top; 
     if(!isEmpty()) 
     { 
      temp = top; 
      size--; 
     } 

     return temp.getData(); 
    } 

    //boolean method to check if the stack is empty 
    public boolean isEmpty() 
    { 
     if(top == null) 
      return true; 
     else 
      return false; 
    } 

    //method to return the size of the stack 
    public int size() 
    { 
     return size; 
    } 

    //method to print out the top number in the stack 
    public int peek() 
    { 
     if (isEmpty() == true) 
     { 
      throw new NoSuchElementException("Stack is empty."); 
     } 
     else 
     { 
      return top.getData(); 
     } 
    } 

    //method to turn the stack into a String 
    public String toString() 
    { 
     String returnStr = "Stack: ["; 
     for(int i = 0 ; i < size; i++) 
     { 
      returnStr += top.getData() + ", "; 
     } 

     returnStr += "]"; 

     return returnStr; 
    } 
} 

這裏是驅動程序類:

import java.util.Scanner; 

public class Driver 
{ 
    public static void main(String[] args) 
    { 
     //declare variables and initialize scanner 
     Scanner key = new Scanner(System.in); 
     int size, choice, value, end; 

     end = 0; 

     //declare and initialize the stack 
     LinkedStack stack1 = new LinkedStack(); 

     //loop to continue operations 
     while(end == 0) 
     { 
      //print out menu for commands 
      System.out.println("\t1) Push \n\t2) Pop \n\t3) Peek \n\t4) Size \n\t5) isEmpty \n\t6) End"); 
      System.out.print("Please choose an option: "); 
      choice = key.nextInt(); 

      //switch the choice and execute commands 
      switch (choice) 
      { 
       case 1: System.out.println("Please enter a value: "); 
        value = key.nextInt(); 
        stack1.push(value); 
        System.out.println(stack1.toString()); 
        break; 
       case 2: stack1.pop(); 
        System.out.println(stack1.toString()); 
        break; 
       case 3: stack1.peek(); 
        System.out.println(stack1.toString()); 
        break; 
       case 4: System.out.println("Size: " + stack1.size()); 
        System.out.println(stack1.toString()); 
        break; 
       case 5: if(stack1.isEmpty()) 
       { 
        System.out.println("Stack is empty."); 
       } 
       else 
        System.out.println("Stack is NOT empty."); 
        System.out.println(stack1.toString()); 
        break; 
       case 6: end = 1; 
        System.out.println("Goodbye!"); 
        break; 
      } 
     } 
    } 
} 

我得到的問題是什麼打印在堆棧中。 在此先感謝您的幫助!

+1

'push()'方法應該創建一個新的'Node'並將其添加到列表中。你似乎從來沒有使用'Node'類的'link'成員。 –

回答

0

我更新了一些你的方法,我想休息方法是好去。

public int pop() 
{ 
    Node temp = top; 
    if(!isEmpty()) 
    { 
     top = temp.link; 
     size--; 
    } else { 
      throw new IllegalAccessError(); 
    } 
    return temp.getData(); 
} 

public void push(int value) 
{ 
    Node node = new Node(value); 
    if(!isEmpty()) 
    { 
     node.link = top; 
     top = node;  
    } else { 
     top = node; 
    } 
    size++; 
} 
    public String toString() 
    { 
     String returnStr = "Stack: ["; 
     Node tmp = top; 
     while(tmp != null) { 
      returnStr += tmp.getData() + ", "; 
      tmp = tmp.link; 
     } 
     returnStr += "]"; 

     return returnStr; 
    } 
+0

謝謝!這有很大幫助 – Jdmon1998

0

你忘了初始化一個節點對象

//push method to add numbers to the stack 
    public void push(int value) { 
     if(!isEmpty()){ 
      top.setData(value); 
      size++; 
     } else { 
      top = new Node(value); 
      size++; 
     } 
    } 
+0

這也有幫助,我會檢查你的,但我不能檢查兩個答案。 – Jdmon1998