以下是头文件
下载直接从官网下载就可以了,官网下载好的一种是自带WinGW编译器的,一种是不带MinGW编译器的。
你main函数中,14行和15行交换一下位置。先调用s.top()输出栈顶元素,然后再s.pop()出栈。
for循环改成while循环。main函数修改如下:
int main() {
seqStack<int> s;
int a[100] = {15,30,20,80,10,5,40,60,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29};
for (int i = 0; i < 31; i++)
s.push(a[i]);
while (!s.isEmpty())
{
cout << s.top() << " "; //先输出栈顶元素
s.pop(); //然后打因栈顶元素位置,并出栈
cout << endl;
}
}
该回答引用ChatGPTc++
void push(int x)
{
if (top == MAXSIZE - 1)
{
printf("栈满
");
return;
}
else
{
top++;
st[top] = x;
}
}
c++
int pop()
{
if (top == -1)
{
printf("栈空
");
return -1;
}
else
{
int x = st[top];
top--;
return x;
}
}
c++
int main()
{
top = -1; // 初始化top指针
push(1);
push(2);
push(3);
push(4);
pop();
push(5);
printf("%d %d %d %d %d
", st[0], st[1], st[2], st[3], st[4]);
return 0;
}