帮我写一个关于c++的栈的简单示例
时间: 2023-02-27 15:00:45 浏览: 47
好的,这是一个关于 C 语言栈的简单示例:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
void push(int value) {
if (top == MAX_SIZE - 1) {
printf("栈已满,无法再压入元素\n");
return;
}
stack[++top] = value;
}
int pop() {
if (top == -1) {
printf("栈为空,无法弹出元素\n");
return -1;
}
return stack[top--];
}
int main() {
push(1);
push(2);
push(3);
printf("当前栈顶元素为:%d\n", stack[top]);
pop();
printf("当前栈顶元素为:%d\n", stack[top]);
return 0;
}
```
该程序演示了如何使用 C 语言实现一个简单的栈,包括如何在栈中压入元素(push)和弹出元素(pop)。
相关问题
用C++写一个栈的模板
以下是一个简单的栈模板的示例,使用C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
// 定义栈结构体
typedef struct {
int data[MAX_SIZE];
int top;
} Stack;
// 初始化栈
void initStack(Stack *s) {
s->top = -1;
}
// 入栈操作
int push(Stack *s, int x) {
if (s->top == MAX_SIZE - 1) {
return 0; // 栈满,无法入栈
}
s->data[++s->top] = x;
return 1; // 入栈成功
}
// 出栈操作
int pop(Stack *s, int *x) {
if (s->top == -1) {
return 0; // 栈空,无法出栈
}
*x = s->data[s->top--];
return 1; // 出栈成功
}
// 获取栈顶元素
int getTop(Stack *s, int *x) {
if (s->top == -1) {
return 0; // 栈空,无栈顶元素
}
*x = s->data[s->top];
return 1; // 获取成功
}
// 判断栈是否为空
int isEmpty(Stack *s) {
return s->top == -1;
}
// 打印栈元素
void printStack(Stack *s) {
printf("Stack: ");
for (int i = s->top; i >= 0; i--) {
printf("%d ", s->data[i]);
}
printf("\n");
}
int main() {
// 初始化栈
Stack s;
initStack(&s);
// 入栈操作
push(&s, 1);
push(&s, 2);
push(&s, 3);
// 打印栈元素
printStack(&s);
// 出栈操作
int x;
pop(&s, &x);
printf("Pop: %d\n", x);
// 获取栈顶元素
getTop(&s, &x);
printf("Top: %d\n", x);
// 判断栈是否为空
printf("Is empty? %d\n", isEmpty(&s));
return 0;
}
```
该栈模板实现了栈的基本操作,包括初始化、入栈、出栈、获取栈顶元素、判断栈是否为空等。可以根据实际需要进行修改和扩展。
写一个栈与递归的转换的c++代码
以下是一个栈与递归的转换的c++代码示例:
```c++
#include <iostream>
#include <stack>
using namespace std;
void recursive_function(int num) {
if (num == 0) return;
// 其他递归操作
recursive_function(num - 1);
}
void stack_function(int num) {
stack<int> stk;
while (num > 0) {
// 其他栈操作
stk.push(num);
num--;
}
while (!stk.empty()) {
// 其他栈操作
int top_num = stk.top();
stk.pop();
}
}
int main() {
int num = 10;
recursive_function(num);
stack_function(num);
return 0;
}
```
该示例中,我们定义了一个递归函数和一个使用栈实现的函数。在递归函数中,我们简单地将 num 值递归减少一直到 0。在使用栈的函数中,我们使用一个 while 循环将 num 值压入栈中,然后再使用一个 while 循环将栈内元素取出,实现逆序输出 num 值。
相关推荐
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.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)