实现栈的基本操作代码
时间: 2023-09-28 17:05:37 浏览: 43
以下是使用 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 top(self):
"""取栈顶元素"""
if not self.is_empty():
return self.items[-1]
def is_empty(self):
"""判断栈是否为空"""
return len(self.items) == 0
def get_size(self):
"""获取栈的大小"""
return len(self.items)
```
以上代码定义了一个 Stack 类,包含了入栈、出栈、取栈顶元素、判断栈是否为空和获取栈的大小等基本操作。当需要使用栈时,只需创建一个 Stack 的实例对象,然后调用相应的方法即可。
相关问题
c语言代码实现顺序栈的基本操作
可以实现顺序栈的基本操作的C语言代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // 定义栈的大小
typedef int ElemType; // 栈内元素类型
typedef struct {
ElemType data[MAX_SIZE]; // 存储栈内元素的数组
int top; // 栈顶指针
} SqStack; // 定义顺序栈类型
void InitStack(SqStack *S); // 初始化栈
int StackEmpty(SqStack *S); // 判断栈是否为空
int StackFull(SqStack *S); // 判断栈是否已满
void Push(SqStack *S, ElemType x); // 入栈操作
ElemType Pop(SqStack *S); // 出栈操作
ElemType GetTop(SqStack *S); // 获取栈顶元素值
int main() {
SqStack S;
int i;
ElemType x, top;
InitStack(&S); // 初始化栈
// 生成元素
for (i = 1; i <= 10; i++) {
x = i;
Push(&S, x); // 入栈
}
while (!StackEmpty(&S)) {
top = GetTop(&S); // 获取栈顶元素值
printf("%d ", top); // 打印栈顶元素值
Pop(&S); // 出栈
}
return 0;
}
// 初始化栈
void InitStack(SqStack *S) {
S->top = -1;
}
// 判断栈是否为空
int StackEmpty(SqStack *S) {
if (S->top == -1) {
return 1; // 栈为空
}
return 0; // 栈不为空
}
// 判断栈是否已满
int StackFull(SqStack *S) {
if (S->top == MAX_SIZE - 1) {
return 1; // 栈已满
}
return 0; // 栈未满
}
// 入栈操作
void Push(SqStack *S, ElemType x) {
if (StackFull(S)) {
printf("Error: Stack Full.\n"); // 栈已满,无法入栈
return;
}
S->top++; // 栈顶指针加1
S->data[S->top] = x; // 新元素入栈
}
// 出栈操作
ElemType Pop(SqStack *S) {
ElemType x;
if (StackEmpty(S)) {
printf("Error: Stack Empty.\n"); // 栈为空,无法出栈
return -1;
}
x = S->data[S->top]; // 栈顶元素出栈
S->top--; // 栈顶指针减1
return x; // 返回栈顶元素值
}
// 获取栈顶元素值
ElemType GetTop(SqStack *S) {
if (StackEmpty(S)) {
printf("Error: Stack Empty.\n"); // 栈为空,无法获取栈顶元素值
return -1;
}
return S->data[S->top]; // 返回栈顶元素值
}
```
C++实现栈的基本操作
栈是一种后进先出(LIFO)的数据结构,常用的基本操作包括入栈、出栈、获取栈顶元素和判断栈是否为空。以下是 C 实现栈的基本操作的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int top;
} Stack;
void init(Stack *s) {
s->top = -1;
}
int is_empty(Stack *s) {
return s->top == -1;
}
int is_full(Stack *s) {
return s->top == MAX_SIZE - 1;
}
void push(Stack *s, int value) {
if (is_full(s)) {
printf("Stack is full.\n");
exit(1);
}
s->data[++s->top] = value;
}
int pop(Stack *s) {
if (is_empty(s)) {
printf("Stack is empty.\n");
exit(1);
}
return s->data[s->top--];
}
int top(Stack *s) {
if (is_empty(s)) {
printf("Stack is empty.\n");
exit(1);
}
return s->data[s->top];
}
int main() {
Stack s;
init(&s);
push(&s, 1);
push(&s, 2);
push(&s, 3);
printf("%d\n", top(&s));
printf("%d\n", pop(&s));
printf("%d\n", pop(&s));
printf("%d\n", pop(&s));
printf("%d\n", is_empty(&s));
return 0;
}
```
这段代码定义了一个结构体 Stack,包含一个数组 data 和一个整数 top,表示栈顶元素的下标。init 函数用于初始化栈,is_empty 和 is_full 函数分别用于判断栈是否为空和是否已满。push 函数用于入栈,pop 函数用于出栈,top 函数用于获取栈顶元素。在 main 函数中,我们创建了一个栈 s,依次将元素 1、2、3 入栈,然后依次取出并输出,最后判断栈是否为空。