2016-07-22 15 views
1

좋아요. 그래서 자바에 대한 여름 코스를 듣고 있습니다. 예, 이것은 수업에서 과제가되었지만, 난처분하고, 이미 휴식을 취했으며, 여전히 논리 오류가 무엇인지 이해하지 못하는 것 같습니다. 나는 링크 목록을 사용하여 스택을 만들 예정이다. Outer 클래스와 Inner 클래스는 UserStack에서 구현되어야합니다. UserStack은 교사가 제공 한 MyStack을 구현합니다. StackApp에는 메인이 들어 있습니다. 컴파일되고 실행됩니다. 입력 할 정수를 올바르게 묻습니다. 제거 할 것이 있으면 제거하고 보여줄 것이 있다면 훔쳐보기를 시도 할 것입니다. 하지만 그것은 항상 그것이 제거되었거나 숫자 0을 표시한다고 말합니다. toString override를 시도해야합니까? 나는 교수에게 물었고 다른 학생들처럼 Google에 가라고했습니다.Java Stack peek 메서드가 올바른 숫자 대신 0을 표시합니다.

MyStack.java

public interface MyStack 
{ 
    public void push (int item); 

    public int pop(); 

    public int peek(); 

    public boolean isEmpty(); 

} 

UserStack.java

import java.util.NoSuchElementException; 

public class UserStack implements MyStack 
{ 
    private class Node 
    { 
     public int value; 
     public Node link; 

     public Node(int data) 
     { 
      data = value; 
     } 
    } 

    private Node head = null; 

    public void push (int item) 
    { 
     Node newHead = new Node(item); 
     newHead.link = head; 
     head = newHead; 
    } 

    public int pop() 
    { 
     if(isEmpty()) 
      throw new NoSuchElementException(); 
     int tmp = head.value; 
     head = head.link; 

     return tmp; 
    } 

    public int peek() 
    { 
     if(isEmpty()) 
      throw new NoSuchElementException(); 

     int tmp = head.value; 

     return tmp; 
    } 

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

StackApp.java

import java.util.Scanner; 

class StackApp 
{ 
    UserStack stack = new UserStack(); 
    public void displayMenu() 
    { 
     System.out.println ("1) Add an integer to the list\n" + 
          "2) Remove last integer entered\n" + 
          "3) Look at last integer entered\n" + 
          "0) Exit the program"); 
     System.out.print ("Selection: "); 
    } 

    public StackApp() 
    { 
     int option; 
     Scanner input = new Scanner(System.in); 

     do{ 
     displayMenu(); 
     option = input.nextInt(); 

     switch (option) 
     { 
      case 1: 
       int number; 
       System.out.println("Enter integer to add: "); 
       number = input.nextInt(); 
       stack.push(number); 
       break; 
      case 2: 
       int number2 = stack.pop(); 
       System.out.println("Interger removed: " + number2); 
       break; 
      case 3: 
       int number3 = stack.peek(); 
       System.out.println("Next Interger: " + number3); 
       break; 
      case 0: 
       System.out.println("Goodbye"); 
       break; 
      default: 
       System.err.println("Unrecongized choice"); 
       break; 
     } 
     }while(option != 0); 
    } 

    public static void main(String[] args) 
    { 
     new StackApp(); 
    } 
} 
+2

' 노드'생성자 :'data = value;'는'value = data;'이어야합니다. –

+0

정말 고마워요. 나는 월요일부터 이걸 쳐다 보았고 결코 눈치 채지 못했다. – GenCrash10

답변

0

당신은 결코 새 항목의 value를 설정하지 않습니다.

+0

문제는'push'가 아니라'Node' 생성자에서 꽤 명백한 것 같습니다 ... –

+0

전문가에게 명백한 것은 초보자가 아닐 수도 있습니다. – SomeStudent

+0

@SomeStudent Jon은이 답변의 이전 상태를 나타냅니다. – EJP

-1

메이트, 당신의 노드 생성자에 값을 할당 중심으로 전환 해보십시오 :

원본 :

public Node(int data) 
     { 
      data = value; 
     } 

새로운 : 또한

public Node(int data) 
     { 
      this.value = data; 
     } 

, 키워드의 사용은 "이"입니다 이 클래스 수준 변수 "value"를 데이터의 값으로 설정하려고한다는 것을 프로그램에 알리는 것이 중요합니다. 따라서 키워드 "this"를 사용해야합니다. 나는 당신이 당신의 "데이터"를 "값"과 동일하게 설정했을 때 당신의 할당이 조금 틀렸다고 확신한다. 그리고 값은 결코 초기 값을 명시 적으로 설정하지 않았기 때문에 기본값은 0이다.

+0

'this'는 필수적이지 않습니다. 여기에는 필수적이지 않습니다. 그러나 생성자에서 좋은 연습 인 것에 동의합니다. 매개 변수에 같은 이름을 사용했을 것입니다. – EJP