2017-09-17 1 views
0

Ich mache meine eigenen Stack-Array für Ints. Es funktioniert gut, außer dass beim Hinzufügen eines Werts zum StackArray immer eine 0 hinzugefügt wird. Jedes Mal, wenn ich den Wert des Peeks erhalten möchte, ist das Ergebnis, dass es leer ist. Ich habe den Code um viele Male geändert, aber wenn ich es auf etwas anderes als zu ändern, was ich Fehler im gesamten Ausführung gegeben istEmpfangen falsche Ergebnisse in benutzerdefinierten Int Stack-Array

Hier bekommen, ist die StackArray Klasse

public class StackArray{ 
//declare variables 
int top, size, length; 
int array[]; 

//constructor to initialize variables 
public StackArray(int _size) 
{ 
    length = 0; 
    top = -1; 
    size = _size; 
    array = new int[size]; 
} 

//push method to add numbers to the stack 
void push(int newNum) 
{ 
    //if statement to check if the stack is full 
    if(top != size) 
    { 
     //update top and length 
     top++; 
     length++; 
     array[top] = newNum; 
    } 
} 

//method to remove the top number in the stack 
int pop() 
{ 
    //declare local variable 
    int temp; 
    //if statement to check if stack is not empty 
    if(!isEmpty()) 
    { 
     temp = top; 
     top--; 
     length--; 
    } 
    else 
     System.out.println("No more items in Stack."); 

    return top; 
} 

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

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

//method to print out the top number in the stack 
void peek() { 
    if (isEmpty()) 
     System.out.println(array[top]); 
    else 
     System.out.println("Stack is empty"); 

} 

//method to turn the stack into a String 
public String toString() 
{ 
    System.out.print("Stack: ["); 
    for(int i = 0; i <= length; i++) 
    { 
     System.out.print(array[i] + ", "); 
    } 
    System.out.println("]"); 

    return ""; 
}} 

Hier ist der Treiber-Klasse ist, dass ich bin mit dem Programm

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; 

    //ask user to enter the length of the stack 
    System.out.print("Please enter the length of the stack: "); 
    size = key.nextInt(); 

    //declate and initialize the stack 
    StackArray stack1 = new StackArray(size); 

    //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); 
        stack1.toString(); 
        break; 
      case 2: stack1.pop(); 
        stack1.toString(); 
        break; 
      case 3: stack1.peek(); 
        stack1.toString(); 
        break; 
      case 4: System.out.println("Size: " + stack1.size()); 
        stack1.toString(); 
        break; 
      case 5: if(!stack1.isEmpty()) 
        { 
         System.out.println("Stack is empty."); 
        } 
        else 
         System.out.println("Stack is NOT empty."); 
        stack1.toString(); 
        break; 
      case 6: end = 1; 
        System.out.println("Goodbye!"); 
        break; 
     } 
    } 
} 

}

+0

'pop' sollte' return temp' nicht 'top' - und' peek' sollte 'array [top] zurückgeben' –

+0

Sie machten insgesamt OK - ich fand einige Probleme nur durch das Lesen Ihres Codes. Schrieb sie in einer Antwort unten. Ich hoffe, sie lösen Ihr Problem. – Assafs

Antwort

0

Der Code hat ein paar Probleme laufen:

void peek() { 
    //This was the change - you can peek as long as the stack 
    // is NOT empty. 
    if (!isEmpty()) 
     System.out.println(array[top]); 
    else 
     System.out.println("Stack is empty"); 

} 

In diesem Zustand hätten Sie in der Minute, in der Ihr Stapel leer war, eine Ausnahme außerhalb des gültigen Bereichs. Das ist eine einfache Lösung. Auch ist Ihre Pop-Methode grundlegend falsch:

int pop() 
{ 
    //declare local variable 
    int temp; 
    //if statement to check if stack is not empty 
    if(!isEmpty()) 
    { 
     temp = array[top]; //note the change here 
     top--; 
     length--; 
    } 
    else 
     System.out.println("No more items in Stack."); 

    return temp; //note the change here 
} 

auch - ich würde empfehlen, dass toString seinen ursprünglichen Zweck erfüllt - eine String-Darstellung zurück, und es nicht zu drucken.

public String toString() 
{ 
    String returnStr = "Stack: ["; 
    for(int i = 0; i < length; i++) 
    { 
     returnStr+= array[i] + ", "; 
    } 
    returnStr += "]"; 

    return returnStr; 
} 

Jetzt statt array1.toString(); in Ihrem Haupt-Code zu schreiben, schreiben Sie einfach System.out.println(array1);. Die toString ist impliziert. Beachten Sie, dass ich die < = in < geändert habe, und jetzt haben Sie nicht die zusätzliche 0 im Ausdruck nach dem Push!

Ein weiteres Problem ist mit der Haupt-case-Anweisung:

//The check here should be positive - say the stack is empty when it is! 
case 5: if(stack1.isEmpty()) { 
      System.out.println("Stack is empty."); 
     } 
     else 
      System.out.println("Stack is NOT empty."); 

Ich denke, das alles ist.

+0

@ Jdmon1998 - Wenn es half - etwas dagegen, die Antwort zu akzeptieren? Klicken Sie einfach auf den grauen Häkchen, es grün machen :) – Assafs

+1

„Bitte geben Sie die Länge des Stapels: 5 \t 1) Drücken \t 2) \t 4) Größe \t 5) isEmpty \t 3) Peek Pop \t 6) End Bitte wählen Sie eine Option: 1 Bitte geben Sie einen Wert: Stack: [3, 0,] \t 2) Pop-Push) \t 3) Peek \t 4) Größe \t 5) isEmpty \t 6) Ende Bitte wählen Sie eine Option: 6 Auf Wiedersehen! " Dies ist, was passiert, wenn ich Push – Jdmon1998

+0

@ Jdmon1998 - Fix für die 0 hinzugefügt. Ich bin froh, ich könnte helfen! – Assafs