2016-03-23 20 views
0

Başlamadan önce, bu bir kitap okumaktan zor olup olmadığını ve çok fazla değiştirmemize izin verilmediğinden dolayı özür dileriz. Yığının üstünü kontrol eden ve ardından görüntülenen peek() adında bir yöntem uygulamak zorundayım.Yığın dışına çıkma yığınının üstünü kapatma

enter image description here

ben Sıfırladığınız() ve gözetleme neden boyut() çalışan, ancak() çalışmıyor?

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

Dizi 0 tabanlıysa ve bir itme sonrasında tos değerini artırırsanız, dizinin sonunun ötesinde değil. Verilere göz atmanız gerekiyor [data.length - 1]. – KevinO

+0

Çıktı: http://pastebin.com/Mi1vBfSY Bu hatayı durdurdu, ancak ikincisini görüntülemiyor. –

+0

Aynı değişikliği yaptınız mı? Değişken tos yığının _size_'idir, ancak son konumdaki karakteri bulmak için 'data [tos - 1]' 'e bakmanız gerekir. – KevinO

cevap

0

() gözetleme için aşağıdaki değişiklik yaparak, örnekteki kodu kullanarak çıkış sorunu macun bölmesinde 2 örnekte belirtildiği çözer. FixedLengthStack'ta ortaya çıkacak aynı sorundur. Aslında, biraz daha iyi bir uygulama büyük olasılıkla, size(), reset(), pop(), (btw, muhtemelen diziyi de boş bir diziye ayarlamalıdır) uygulayan soyut bir Stack sınıfına sahip olacaktır.().

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

    return data[tos - 1]; 
} 
İlgili konular