Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to 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
#include
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 result:
C result:
Java result:
Stack: ['A', 'B', 'C']
Pop: C
Peek: B
isEmpty: False
Size: 2
Stack: A B C
Pop: C
Peek: B
isEmpty: 0
Size: 2
Stack: A B C
Pop: C
Peek: B
isEmpty: false
Size: 2