2016-03-23 5 views
0

私が始めてしまう前に、これが読めないのであれば、私はお詫びします。その本を読んでください。 peek()というメソッドを実装する必要があります。このメソッドはスタックの先頭をチェックして表示されます。限界を越えてスタックを覗き見している

私がリセットされている

enter image description here

()およびサイズは()作業が、なぜ(PEEKされる)動作していませんか?

interface ISimpleStack { 

    // Push a character onto the stack. 
    void push(char ch); 

    // Pop a character from the stack. 
    char pop(); 

    // Return true if the stack is empty. 
    boolean isEmpty(); 

    // Return true if the stack is full. 
    boolean isFull(); 

    void reset(); 

    char peek(); 

    int size(); 
} 

// A fixed-length stack for characters. 
class FixedLengthStack implements ISimpleStack { 
    private char[] data; // this array holds the stack 
    private int tos; // index of top of stack 

    // Construct an empty stack given its size. 
    FixedLengthStack(int size) { 
    data = new char[size]; // create the array to hold the stack 
    tos = 0; 
    } 

    // Construct a stack from a stack. 
    FixedLengthStack(FixedLengthStack otherStack) { 
    // size of new stack equals that of otherStack 
    data = new char[otherStack.data.length]; 

    // set tos to the same position 
    tos = otherStack.tos; 

    // copy the contents 
    for(int i = 0; i < tos; i++) 
     data[i] = otherStack.data[i]; 
    } 

    // Construct a stack with initial values. 
    FixedLengthStack(char[] chrs) { 
    // create the array to hold the initial values 
    data = new char[chrs.length]; 
    tos = 0; 

    // initialize the stack by pushing the contents 
    // of chrs onto it 
    for(char ch : chrs) 
     push(ch); 
    } 

    // Push a character onto the stack. 
    public void push(char ch) { 
    if(isFull()) { 
     System.out.println(" -- Stack is full."); 
     return; 
    } 

    data[tos] = ch; 
    tos++; 
    } 

    // Pop a character from the stack. 
    public char pop() { 
    if(isEmpty()) { 
     System.out.println(" -- Stack is empty."); 
     return (char) 0; // a placeholder value 
    } 

    tos--; 
    return data[tos]; 
    } 

    // Return true if the stack is empty. 
    public boolean isEmpty() { 
    return tos==0; 
    } 

    // Return true if the stack is full. 
    public boolean isFull() { 
    return tos==data.length; 
    } 

    public void reset() { 
     tos = 0; 
    } 

    public char peek() { 
    if(isEmpty()) { 
     System.out.println(" -- Stack is empty."); 
     return (char) 0; // a placeholder value 
    } 

    return data[tos]; 
    } 

    public int size() { 
     int size = 0; 
     for(int i = 0; i <= tos; i++){ 
     size = i; 
     } 
     System.out.println("Size of stack is: " + size); 
     return size; 
    } 
} 

// A growable stack for characters. 
class DynamicStack implements ISimpleStack { 
    private char[] data; // this array holds the stack 
    private int tos; // index of top of stack 

    // Construct an empty stack given its size. 
    DynamicStack(int size) { 
    data = new char[size]; // create the array to hold the stack 
    tos = 0; 
    } 

    // Construct a stack from a stack. 
    DynamicStack(DynamicStack otherStack) { 
    // size of new stack equals that of otherStack 
    data = new char[otherStack.data.length]; 

    // set tos to the same position 
    tos = otherStack.tos; 

    // copy the contents 
    for(int i = 0; i < tos; i++) 
     data[i] = otherStack.data[i]; 
    } 

    // Construct a stack with initial values. 
    DynamicStack(char[] chrs) { 
    // create the array to hold the initial values 
    data = new char[chrs.length]; 
    tos = 0; 

    // initialize the stack by pushing the contents 
    // of chrs onto it 
    for(char ch : chrs) 
     push(ch); 
    } 

    // Push a character onto the stack. 
    public void push(char ch) { 

    // if there is no more room in the array, 
    // expand the size of the stack 
    if(tos == data.length) { 
     // double the size of the existing array 
     char[] t = new char[data.length * 2]; 

     // copy the contents of the stack into the larger array 
     for(int i = 0; i < tos; i++) 
     t[i] = data[i]; 

     // set data to refer to the new array 
     data = t; 
    } 

    data[tos] = ch; 
    tos++; 
    } 

    // Pop a character from the stack. 
    public char pop() { 
    if(isEmpty()) { 
     System.out.println(" -- Stack is empty."); 
     return (char) 0; // a placeholder value 
    } 

    tos--; 
    return data[tos]; 
    } 

    // Return true if the stack is empty. 
    public boolean isEmpty() { 
    return tos==0; 
    } 

    // Return true if the stack is full. For DynamicStack, 
    // this method always returns false. 
    public boolean isFull() { 
    return false; 
    } 

    public void reset() { 
     tos = 0; 
    } 

    public char peek() { 
    if(isEmpty()) { 
     System.out.println(" -- Stack is empty."); 
     return (char) 0; // a placeholder value 
    } 

    return data[tos]; 
    } 

    public int size() { 
     int size = 0; 
     for(int i = 0; i <= tos; i++){ 
     size = i; 
     } 
     System.out.println("Size of stack is: " + size); 
     return size; 
    } 
} 

// Demonstrate ISimpleStack. 
class ISimpleStackDemo { 
    public static void main(String[] args) { 
    int i; 
    char ch; 

    // create an ISimpleStack interface variable 
    ISimpleStack iStack; 

    // Now, construct a FixedLengthStack and a DynamicStack 
    FixedLengthStack fixedStack = new FixedLengthStack(10); 
    DynamicStack dynStack = new DynamicStack(5); 

    // first, use fixedStack through iStack 
    iStack = fixedStack; 

    // push characters onto fixedStack 
    for(i = 0; !iStack.isFull(); i++) 
     iStack.push((char) ('A'+i)); 

    iStack.size(); 

    System.out.print("Top of fixedStack: "); 
    while(!iStack.isEmpty()) { 
     ch = iStack.peek(); 
     System.out.print(ch); 
    } 

    // pop characters off fixedStack 
    System.out.print("Contents of fixedStack: "); 
    while(!iStack.isEmpty()) { 
     ch = iStack.pop(); 
     System.out.print(ch); 
    } 

    System.out.print("\nContents of fixedStack after reset: "); 
    iStack.reset(); 
     ch = iStack.pop(); 
     System.out.print(ch); 
    System.out.println(); 

    // next, use dynStack through iStack 
    iStack = dynStack; 

    // push A through Z onto dynStack 
    // this will result in three increases in its size 
    for(i = 0; i < 26; i++) 
     iStack.push((char) ('A'+i)); 

    iStack.size(); 
    // pop characters off dynStack 
    System.out.print("Contents of dynStack: "); 
    while(!iStack.isEmpty()) { 
     ch = iStack.pop(); 
     System.out.print(ch); 
    } 
    System.out.print("\nContents of dynStack after reset: "); 
    iStack.reset(); 
     ch = iStack.pop(); 
     System.out.print(ch); 
    } 
} 
+0

配列が0ベースで、プッシュ後にtosを増やすと、配列の終わりを超えます。あなたはdata [data.length - 1]を覗く必要があります。 – KevinO

+0

出力:http://pastebin.com/Mi1vBfSY これによりエラーは停止しましたが、2番目のエラーは表示されません。 –

+0

同じ変更を加えましたか?変数tosはスタックの_size_ですが、最後の位置にある文字を見つけるには 'data [tos-1]'を参照する必要があります。 – KevinO

答えて

0

()覗き見するために、次の変更を行う、例のコードを使用すると、出力の問題がペーストビンにおける第2実施例で述べた解決します。これは、FixedLengthStackで発生するのと同じ問題です。実際、少し上手く実装するには、おそらく、size()、reset()、pop()(btwはおそらく配列を空の配列に設定する必要があります)を実装した抽象的なStackクラスを持ち、 ()。

@Override 
public char peek() 
{ 
    if (isEmpty()) { 
     System.out.println(" -- Stack is empty."); 
     return (char) 0; // a placeholder value 
    } 

    return data[tos - 1]; 
} 
関連する問題