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;
}
}
}
}
'pop' sollte' return temp' nicht 'top' - und' peek' sollte 'array [top] zurückgeben' –
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