2014-09-17 3 views
-1

스택에 대한 Java 코드가 있습니다. 아래의 방법을 채우려면 도움이 필요합니다 : create method 또는 create constructor here.Java 배열 스택 구현

public class ArrayStack 
{ 

    private int[] A; 

    private int top; 

    public ArrayStack() 
    { 
     create constructor here 
    } 

    public ArrayStack(int maxsize) 
    { 
     create constructor here 
    } 

    public boolean isEmpty() { 
     create method 
    } 

    public boolean isFull() 
    { 
     create method 
    } 

    public int peek() 
    { 
     if(isEmpty()) 
      throw new RuntimeException("Peek attempted on empty stack"); 
     else 
      return A[top]; 
    } 

    public void push(int m) 
    { 
     if(isFull()) 
      throw new RuntimeException("Push attempted on full stack"); 
     else 
     { 
      top++; 
      A[top]=m; 
     } 
    } 

    public int pop() 
    { 
     if(isEmpty()) 
      throw new RuntimeException("Pop attempted on empty stack"); 
     else 
     { 
      int value = A[top]; 
      top--; 
      return value; 
     } 
    } 

    public int size() 
    { 
     return (top + 1); 
    } 

    public String toString() 
    { 
     String answer = ""; 
     int i; 
     for(i = 0; i <= top; i++) 
      answer = "\n" + A[i] + answer; 
     return answer; 
    } 

} 
+4

이 질문은 코드 작성 요청이기 때문에 주제가 아닌 것처럼 보입니다. –

+0

@ Jason 우리는 숙제를 돕지 만 다른 질문과 같은 기준으로 돕습니다. – chrylis

+0

['ArrayDeque']를 사용하지 않는 이유는 무엇입니까? (http://docs.oracle.com/javase/7/docs/api/java/util/ArrayDeque.html)? –

답변

2

먼저 생성자는 A 배열을 만들어야합니다.

private int[] A; 
private int top = 0; // <-- start at 0 
public ArrayStack() 
{ 
    this(10);   // <-- delegate to the second constructor. 
    // A = new int[10]; // <-- OR 
} 

public ArrayStack(int maxsize) 
{ 
    A = new int[maxsize]; 
} 

다음은 다른 테스트 중 하나를 수행하는 방법을 보여 드리겠습니다. isFull()을 보자. 0에서 시작하여 자랍니다 (push() 참조) - A도 있습니다. top이 있습니다. 따라서

public boolean isFull() 
{ 
    return (top >= A.length); 
}