数组出栈的C++代码
时间: 2023-10-28 17:05:50 浏览: 70
以下是一个简单的数组出栈的 C++ 代码示例:
```c++
#include<iostream>
using namespace std;
int main(){
int arr[5] = {1, 2, 3, 4, 5};
int top = 4; // 栈顶指针
while(top >= 0){
cout << arr[top] << " "; // 输出栈顶元素
top--; // 将栈顶指针向下移动
}
return 0;
}
```
在上面的示例中,我们首先定义了一个大小为 5 的整数数组 `arr`,并将其初始化为 `{1, 2, 3, 4, 5}`。接着,我们定义了一个整数变量 `top` 作为栈顶指针,初始化为 `4`,即数组的最后一个元素的下标。然后,我们使用一个 while 循环,不断将栈顶元素输出,并将栈顶指针向下移动,直到栈顶指针小于 0,即栈为空为止。
相关问题
顺序栈的入栈和出栈c++
顺序栈的入栈和出栈操作可以用 C++ 语言实现,具体代码如下:
```cpp
#include <iostream>
using namespace std;
const int MAXSIZE = 100; // 定义栈的最大长度
// 定义顺序栈结构体
struct SeqStack {
int data[MAXSIZE];
int top; // 栈顶指针
};
// 初始化顺序栈
void InitStack(SeqStack &s) {
s.top = -1; // 栈顶指针初始化为-1
}
// 判断栈是否为空
bool IsEmpty(SeqStack s) {
return s.top == -1;
}
// 判断栈是否已满
bool IsFull(SeqStack s) {
return s.top == MAXSIZE - 1;
}
// 入栈操作
bool Push(SeqStack &s, int x) {
if (IsFull(s)) {
return false; // 栈已满,入栈失败
}
s.top++; // 栈顶指针加1
s.data[s.top] = x; // 将元素x入栈
return true; // 入栈成功
}
// 出栈操作
bool Pop(SeqStack &s, int &x) {
if (IsEmpty(s)) {
return false; // 栈为空,出栈失败
}
x = s.data[s.top]; // 取出栈顶元素
s.top--; // 栈顶指针减1
return true; // 出栈成功
}
int main() {
SeqStack s;
InitStack(s); // 初始化栈
// 入栈操作
Push(s, 1);
Push(s, 2);
Push(s, 3);
// 出栈操作
int x;
Pop(s, x);
cout << "出栈的元素为:" << x << endl;
Pop(s, x);
cout << "出栈的元素为:" << x << endl;
Pop(s, x);
cout << "出栈的元素为:" << x << endl;
if (Pop(s, x)) {
cout << "出栈的元素为:" << x << endl;
} else {
cout << "栈为空,出栈失败!" << endl;
}
return 0;
}
```
以上代码中,我们定义了一个 `SeqStack` 结构体,包含一个数组 `data` 和一个整型变量 `top`,其中 `top` 表示栈顶指针,初始化为 -1。然后我们实现了栈的初始化、判断栈是否为空或已满、入栈和出栈等操作。在主函数中,我们进行了入栈和出栈的测试。
编写一个C++的入栈和出栈代码
下面是一个简单的 C++ 入栈和出栈的示例代码:
```cpp
#include <iostream>
#define MAXSIZE 100
using namespace std;
class Stack {
private:
int top;
int data[MAXSIZE];
public:
Stack() { top = -1; }
void push(int item) {
if (top >= MAXSIZE - 1) {
cout << "Stack Overflow!" << endl;
return;
}
data[++top] = item;
}
int pop() {
if (top < 0) {
cout << "Stack Underflow!" << endl;
return -1;
}
return data[top--];
}
};
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << endl;
cout << s.pop() << endl;
cout << s.pop() << endl;
cout << s.pop() << endl;
return 0;
}
```
以上代码中,我们使用一个类 `Stack` 来实现栈的基本操作,包括入栈和出栈。在 `Stack` 类中,我们使用一个数组 `data` 来存储栈中的元素,以及一个整数 `top` 来表示栈顶的位置。在 `push` 函数中,我们首先检查栈是否已满,如果是,则输出错误信息并返回;否则,将元素插入到 `top+1` 的位置,并将 `top` 值加 1。在 `pop` 函数中,我们首先检查栈是否为空,如果是,则输出错误信息并返回 -1;否则,返回 `top` 位置上的元素,并将 `top` 值减 1。
在 `main` 函数中,我们创建了一个栈对象 `s`,并依次将元素 10、20、30 入栈,然后依次将栈中的元素出栈,并输出出栈的元素。最后,我们尝试从空栈中出栈一个元素,这时会输出错误信息 "Stack Underflow!"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)