运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
class Stack: def __init__(self): self.stack = [] def push(self, element): self.stack.append(element) def pop(self): if self.isEmpty(): return "Stack is empty" return self.stack.pop() def peek(self): if self.isEmpty(): return "Stack is empty" return self.stack[-1] def isEmpty(self): return len(self.stack) == 0 def size(self): return len(self.stack) # Create a stack myStack = Stack() myStack.push('A') myStack.push('B') myStack.push('C') print("Stack: ", myStack.stack) print("Pop: ", myStack.pop()) print("Peek: ", myStack.peek()) print("isEmpty: ", myStack.isEmpty()) print("Size: ", myStack.size()) #Python
#include <stdio.h> #include <stdlib.h> typedef struct { int *stack; int top; int capacity; } Stack; Stack* createStack(int capacity) { Stack *newStack = (Stack*)malloc(sizeof(Stack)); newStack->stack = (int*)malloc(capacity * sizeof(int)); newStack->top = -1; newStack->capacity = capacity; return newStack; } void push(Stack *s, int element) { if (s->top == s->capacity - 1) { printf("Stack is full\n"); return; } s->stack[++s->top] = element; } int pop(Stack *s) { if (s->top == -1) { printf("Stack is empty\n"); return -1; } return s->stack[s->top--]; } int peek(Stack *s) { if (s->top == -1) { printf("Stack is empty\n"); return -1; } return s->stack[s->top]; } int isEmpty(Stack *s) { return s->top == -1; } int size(Stack *s) { return s->top + 1; } void printStack(Stack *s) { printf("Stack: "); for (int i = 0; i <= s->top; ++i) { printf("%c ", s->stack[i]); } printf("\n"); } int main() { Stack *myStack = createStack(100); push(myStack, 'A'); push(myStack, 'B'); push(myStack, 'C'); // Print initial stack printStack(myStack); printf("Pop: %c\n", pop(myStack)); printf("Peek: %c\n", peek(myStack)); printf("isEmpty: %d\n", isEmpty(myStack)); printf("Size: %d\n", size(myStack)); return 0; } //C
public class Main { public static void main(String[] args) { Stack myStack = new Stack(10); myStack.push('A'); myStack.push('B'); myStack.push('C'); // Print initial stack System.out.print("Stack: "); myStack.printStack(); System.out.println("Pop: " + myStack.pop()); System.out.println("Peek: " + myStack.peek()); System.out.println("isEmpty: " + myStack.isEmpty()); System.out.println("Size: " + myStack.size()); } } class Stack { char[] stack; int top; int capacity; public Stack(int capacity) { this.capacity = capacity; this.stack = new char[capacity]; this.top = -1; } public void push(char element) { if (top == capacity - 1) { System.out.println("Stack is full"); return; } stack[++top] = element; } public char pop() { if (isEmpty()) { System.out.println("Stack is empty"); return ' '; } return stack[top--]; } public char peek() { if (isEmpty()) { System.out.println("Stack is empty"); return ' '; } return stack[top]; } public boolean isEmpty() { return top == -1; } public int size() { return top + 1; } public void printStack() { for (int i = 0; i <= top; i++) { System.out.print(stack[i] + " "); } System.out.println(); } } //Java
Python 结果
C 结果
Java 结果
栈: ['A', 'B', 'C']
弹出: C
窥视: B
为空: False
大小: 2
栈: A B C
弹出: C
窥视: B
为空: 0
大小: 2
栈: A B C
弹出: C
窥视: B
为空: false
大小: 2