问题描述
我正在尝试为 Java 中的字符创建一个堆栈,但存在一个错误,无法将字符压入堆栈。我输入一个字符串并提取每个字符以放置在堆栈中。 奇怪的是,堆栈只存储字符串的一个字符。有人可以帮我吗?
int top,capacity;
char[] stack;
stack7() {
top = -1;
capacity = 6;
stack = new char[capacity];
}
public boolean isempty() {
return top == -1;
}
public boolean isfull() {
return top == capacity - 1;
}
public void push(char data) {
if (isfull()) {
System.out.println("Stack is full");
} else {
stack[++top] = data;
}
}
public char pop() {
if (isempty()) {
System.out.println("Stack is empty");
}
return stack[top--];
}
public char peek() {
return stack[top];
}
public void display() {
int temp = top;
if (temp >= 0) {
System.out.print(stack[temp] + " ");
temp--;
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(system.in);
System.out.print("enter string");
String str = sc.nextLine();
stack7 st = new stack7();
for (int i = 0; i < str.length(); i++) {
st.push(str.charat(i));
}
st.display();
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)