美好的一天, 有人可以帮助我,这有点令人沮丧,我如何在Stack数组中保留int和char。这里是仅用于int的堆栈数组的代码,dunno如何推送类似(例如)“输入名称”“输入数字”,我应该放什么代码以及我要添加它的部分? 抱歉我的英语..........
#include <iostream>
using namespace std;
int size;
int top;
int * stack_values;
void createStack(int s) {
size = s;
stack_values = new int[s];
top = -1;
}
bool isFull() {
if(top == size-1) {
return true;
} else {
return false;
}
}
bool isEmpty() {
if(top == -1) {
return true;
} else {
return false;
}
}
void pop() {
if(!isEmpty()) {
top--;
} else {
cout << "The stack is Empty!" << endl;
}
}
void push(int num) {
if(!isFull()) {
top += 1; //top++;
stack_values[top] = num;
}
}
void display() {
for(int i = top; i>=0; i--) {
cout << stack_values[i]<<endl;
}
}
int main() {
createStack(5);
bool loop = true;
while(loop) {
cout << "Choices:\n1.Push\n2.Pop \n3.Display \n";
int choice;
cout << "Enter Choice: ";
cin >> choice;
switch(choice) {
case 1: {
int number;
cout << "Enter number: ";
cin >> number;
push(number);
}
break;
case 2: {
pop();
}
break;
case 3: {
display();
}
break;
case 0:
loop = false;
break;
default: {
cout << "Invalid input";
loop = false;
}
break;
}
}
return 0;
}