帮我写一个实现顺序栈的代码
时间: 2023-05-29 09:02:42 浏览: 60
下面是 Python 的实现:
```python
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
def peek(self):
if not self.is_empty():
return self.items[-1]
def size(self):
return len(self.items)
```
使用方式:
```python
stack = Stack()
stack.push(1) # 入栈
stack.push(2)
stack.push(3)
print(stack.pop()) # 3,出栈
print(stack.peek()) # 2,查看栈顶元素
print(stack.size()) # 2,查看栈大小
```
相关问题
你可以用C语言帮我写出一个顺序栈吗
当然可以,以下是一个简单的顺序栈的代码:
```c
#define MAXSIZE 100 // 定义栈的最大容量为100
typedef struct {
int data[MAXSIZE]; // 存储数据的数组
int top; // 栈顶指针
} Stack;
Stack *init_stack() {
Stack *s = (Stack *)malloc(sizeof(Stack));
s->top = -1; // 栈顶指针初始化为-1
return s;
}
int is_empty(Stack *s) {
return s->top == -1; // 判断栈是否为空
}
int is_full(Stack *s) {
return s->top == MAXSIZE - 1; // 判断栈是否已满
}
int push(Stack *s, int x) {
if (is_full(s)) {
return 1; // 栈已满,插入失败
}
s->top++; // 栈顶指针加1
s->data[s->top] = x; // 将元素x插入栈顶
return 0; // 插入成功
}
int pop(Stack *s) {
if (is_empty(s)) {
return -1; // 栈为空,弹出失败
}
int x = s->data[s->top]; // 取出栈顶元素
s->top--; // 栈顶指针减1
return x; // 返回栈顶元素
}
int get_top(Stack *s) {
if (is_empty(s)) {
return -1; // 栈为空,获取栈顶元素失败
}
return s->data[s->top]; // 获取栈顶元素
}
void destroy_stack(Stack *s) {
free(s); // 销毁栈
}
```
这是一个基本的顺序栈的实现,你可以根据自己的实际需要对其中的代码进行修改和优化。
写一个顺序栈的括号匹配代码及其主函数
好的,以下是顺序栈的括号匹配代码及主函数:
```c++
#include <iostream>
#include <string>
using namespace std;
const int MAXSIZE = 100; // 定义栈的最大容量
// 定义顺序栈结构体
struct SeqStack {
char data[MAXSIZE];
int top;
};
// 初始化顺序栈
void InitStack(SeqStack &S) {
S.top = -1;
}
// 判断顺序栈是否为空
bool StackIsEmpty(SeqStack S) {
return S.top == -1;
}
// 判断顺序栈是否已满
bool StackIsFull(SeqStack S) {
return S.top == MAXSIZE - 1;
}
// 入栈操作
bool Push(SeqStack &S, char x) {
if (StackIsFull(S)) {
return false; // 栈已满,入栈失败
}
S.top++;
S.data[S.top] = x;
return true; // 入栈成功
}
// 出栈操作
bool Pop(SeqStack &S, char &x) {
if (StackIsEmpty(S)) {
return false; // 栈为空,出栈失败
}
x = S.data[S.top];
S.top--;
return true; // 出栈成功
}
// 获取栈顶元素
bool GetTop(SeqStack S, char &x) {
if (StackIsEmpty(S)) {
return false; // 栈为空,获取栈顶元素失败
}
x = S.data[S.top];
return true; // 获取栈顶元素成功
}
// 判断括号是否匹配
bool IsMatch(string str) {
SeqStack S;
InitStack(S);
for (int i = 0; i < str.length(); i++) {
if (str[i] == '(' || str[i] == '[' || str[i] == '{') {
Push(S, str[i]); // 左括号入栈
}
else if (str[i] == ')' || str[i] == ']' || str[i] == '}') {
if (StackIsEmpty(S)) {
return false; // 栈为空,右括号多余,不匹配
}
char top;
GetTop(S, top);
if ((top == '(' && str[i] == ')') || (top == '[' && str[i] == ']') || (top == '{' && str[i] == '}')) {
Pop(S, top); // 左右括号匹配,出栈
}
else {
return false; // 左右括号不匹配,不匹配
}
}
}
if (StackIsEmpty(S)) {
return true; // 最后栈为空,括号匹配
}
else {
return false; // 最后栈不为空,左括号多余,不匹配
}
}
// 主函数
int main() {
string str;
cout << "请输入一个字符串:" << endl;
cin >> str;
if (IsMatch(str)) {
cout << "括号匹配" << endl;
}
else {
cout << "括号不匹配" << endl;
}
return 0;
}
```
以上代码实现了顺序栈的括号匹配功能,主函数中通过调用 `IsMatch()` 函数实现对输入字符串的括号匹配判断。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)