运行 ❯
获取您的
自己的
网站
×
更改方向
更改主题,深色/浅色
转到 Spaces
Python
C
Java
class Node: def __init__(self, value): self.value = value self.next = None class Stack: def __init__(self): self.head = None self.size = 0 def push(self, value): new_node = Node(value) if self.head: new_node.next = self.head self.head = new_node self.size += 1 def pop(self): if self.isEmpty(): return "Stack is empty" popped_node = self.head self.head = self.head.next self.size -= 1 return popped_node.value def peek(self): if self.isEmpty(): return "Stack is empty" return self.head.value def isEmpty(self): return self.size == 0 def stackSize(self): return self.size # Usage myStack = Stack() myStack.push('A') myStack.push('B') myStack.push('C') print("Pop: ", myStack.pop()) print("Peek: ", myStack.peek()) print("isEmpty: ", myStack.isEmpty()) print("Size: ", myStack.stackSize()) #Python
#include <stdio.h> #include <stdlib.h> struct Node { char value; struct Node *next; }; struct Stack { struct Node *head; int size; }; struct Stack *createStack() { struct Stack *s = malloc(sizeof(struct Stack)); s->head = NULL; s->size = 0; return s; } void push(struct Stack *s, char value) { struct Node *newNode = malloc(sizeof(struct Node)); newNode->value = value; newNode->next = s->head; s->head = newNode; s->size++; } char pop(struct Stack *s) { if (s->size == 0) { return ' '; } struct Node *popped = s->head; s->head = s->head->next; char poppedValue = popped->value; free(popped); s->size--; return poppedValue; } char peek(struct Stack *s) { if (s->size == 0) { return ' '; } return s->head->value; } int isEmpty(struct Stack *s) { return s->size == 0; } int stackSize(struct Stack *s) { return s->size; } int main() { struct Stack *myStack = createStack(); push(myStack, 'A'); push(myStack, 'B'); push(myStack, 'C'); printf("Pop: %c\n", pop(myStack)); printf("Peek: %c\n", peek(myStack)); printf("isEmpty: %d\n", isEmpty(myStack)); printf("Size: %d\n", stackSize(myStack)); return 0; } //C
public class Main { public static void main(String[] args) { Stack myStack = new Stack(); myStack.push('A'); myStack.push('B'); myStack.push('C'); 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 Node { char value; Node next; Node(char value) { this.value = value; this.next = null; } } class Stack { private Node head; private int size; public Stack() { this.head = null; this.size = 0; } public void push(char value) { Node newNode = new Node(value); if (head != null) { newNode.next = head; } head = newNode; size++; } public char pop() { if (isEmpty()) { return ' '; } char popped = head.value; head = head.next; size--; return popped; } public char peek() { if (isEmpty()) { return ' '; } return head.value; } public boolean isEmpty() { return size == 0; } public int size() { return size; } } //Java
Python 结果
C 结果
Java 结果
弹出:C
窥视:B
isEmpty:False
大小:2
弹出:C
窥视:B
isEmpty:0
大小:2
弹出:C
窥视:B
isEmpty:false
大小:2