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