Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
class Queue: def __init__(self): self.queue = [] def enqueue(self, element): self.queue.append(element) def dequeue(self): if self.isEmpty(): return "Queue is empty" return self.queue.pop(0) def peek(self): if self.isEmpty(): return "Queue is empty" return self.queue[0] def isEmpty(self): return len(self.queue) == 0 def size(self): return len(self.queue) # Create a queue myQueue = Queue() myQueue.enqueue('A') myQueue.enqueue('B') myQueue.enqueue('C') print("Queue: ", myQueue.queue) print("Dequeue: ", myQueue.dequeue()) print("Peek: ", myQueue.peek()) print("isEmpty: ", myQueue.isEmpty()) print("Size: ", myQueue.size()) #Python
#include
#include
#define CAPACITY 10 struct Queue { int queue[CAPACITY]; int front; int size; }; void enqueue(struct Queue *q, int element); int dequeue(struct Queue *q); int peek(struct Queue *q); bool isEmpty(struct Queue *q); int size(struct Queue *q); void printQueue(struct Queue *q); int main() { struct Queue myQueue = { .front = 0, .size = 0 }; enqueue(&myQueue, 'A'); enqueue(&myQueue, 'B'); enqueue(&myQueue, 'C'); printQueue(&myQueue); printf("Dequeue: %c\n", dequeue(&myQueue)); printf("Peek: %c\n", peek(&myQueue)); printf("isEmpty: %d\n", isEmpty(&myQueue)); printf("Size: %d\n", size(&myQueue)); return 0; } void enqueue(struct Queue *q, int element) { if (q->size == CAPACITY) { printf("Queue is full\n"); return; } q->queue[(q->front + q->size) % CAPACITY] = element; q->size++; } int dequeue(struct Queue *q) { if (isEmpty(q)) { printf("Queue is empty\n"); return -1; } int item = q->queue[q->front]; q->front = (q->front + 1) % CAPACITY; q->size--; return item; } int peek(struct Queue *q) { if (isEmpty(q)) { printf("Queue is empty\n"); return -1; } return q->queue[q->front]; } bool isEmpty(struct Queue *q) { return q->size == 0; } int size(struct Queue *q) { return q->size; } void printQueue(struct Queue *q) { printf("Queue: "); for (int i = 0; i < q->size; ++i) { printf("%c ", q->queue[(q->front + i) % CAPACITY]); } printf("\n"); } //C
public class Main { public static void main(String[] args) { Queue myQueue = new Queue(10); myQueue.enqueue('A'); myQueue.enqueue('B'); myQueue.enqueue('C'); myQueue.printQueue(); System.out.println("Dequeue: " + myQueue.dequeue()); System.out.println("Peek: " + myQueue.peek()); System.out.println("isEmpty: " + myQueue.isEmpty()); System.out.println("Size: " + myQueue.size()); } } class Queue { char[] queue; int front; int size; int capacity; public Queue(int capacity) { this.capacity = capacity; this.queue = new char[capacity]; this.front = 0; this.size = 0; } public void enqueue(char element) { if (size == capacity) { System.out.println("Queue is full"); return; } queue[(front + size) % capacity] = element; size++; } public char dequeue() { if (isEmpty()) { System.out.println("Queue is empty"); return '\u0000'; // Null character } char item = queue[front]; front = (front + 1) % capacity; size--; return item; } public char peek() { if (isEmpty()) { System.out.println("Queue is empty"); return '\u0000'; // Null character } return queue[front]; } public boolean isEmpty() { return size == 0; } public int size() { return size; } public void printQueue() { System.out.print("Queue: "); for (int i = 0; i < size; i++) { System.out.print(queue[(front + i) % capacity] + " "); } System.out.println(); } } //Java
Python result:
C result:
Java result:
Queue: ['A', 'B', 'C']
Dequeue: A
Peek: B
isEmpty: False
Size: 2
Queue: A B C
Dequeue: A
Peek: B
isEmpty: 0
Size: 2
Queue: A B C
Dequeue: A
Peek: B
isEmpty: false
Size: 2