Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
class Node: def __init__(self, data): self.data = data self.next = None class Queue: def __init__(self): self.front = None self.rear = None self.length = 0 def enqueue(self, element): new_node = Node(element) if self.rear is None: self.front = self.rear = new_node self.length += 1 return self.rear.next = new_node self.rear = new_node self.length += 1 def dequeue(self): if self.isEmpty(): return "Queue is empty" temp = self.front self.front = temp.next self.length -= 1 if self.front is None: self.rear = None return temp.data def peek(self): if self.isEmpty(): return "Queue is empty" return self.front.data def isEmpty(self): return self.length == 0 def size(self): return self.length def printQueue(self): temp = self.front while temp: print(temp.data, end=" ") temp = temp.next print() # Create a queue myQueue = Queue() myQueue.enqueue('A') myQueue.enqueue('B') myQueue.enqueue('C') print("Queue: ", end="") myQueue.printQueue() print("Dequeue: ", myQueue.dequeue()) print("Peek: ", myQueue.peek()) print("isEmpty: ", myQueue.isEmpty()) print("Size: ", myQueue.size()) #Python
#include
#include
typedef struct Node { char data; struct Node* next; } Node; typedef struct { Node* front; Node* rear; int length; } Queue; Queue* createQueue() { Queue* newQueue = (Queue*)malloc(sizeof(Queue)); newQueue->front = NULL; newQueue->rear = NULL; newQueue->length = 0; return newQueue; } void enqueue(Queue* q, char element) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = element; newNode->next = NULL; if (q->rear == NULL) { q->front = q->rear = newNode; q->length++; return; } q->rear->next = newNode; q->rear = newNode; q->length++; } char dequeue(Queue* q) { if (q->length == 0) { printf("Queue is empty\n"); return ' '; } Node* temp = q->front; q->front = temp->next; if (q->front == NULL) { q->rear = NULL; } q->length--; char dequeued = temp->data; free(temp); return dequeued; } char peek(Queue* q) { if (q->length == 0) { printf("Queue is empty\n"); return ' '; } return q->front->data; } int isEmpty(Queue* q) { return q->length == 0; } int size(Queue* q) { return q->length; } void printQueue(Queue* q) { Node* temp = q->front; while (temp) { printf("%c ", temp->data); temp = temp->next; } printf("\n"); } int main() { Queue* myQueue = createQueue(); enqueue(myQueue, 'A'); enqueue(myQueue, 'B'); enqueue(myQueue, 'C'); printf("Queue: "); 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; } //C
public class Main { public static void main(String[] args) { Queue myQueue = new Queue(); myQueue.enqueue('A'); myQueue.enqueue('B'); myQueue.enqueue('C'); System.out.print("Queue: "); 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 Node { char data; Node next; public Node(char data) { this.data = data; this.next = null; } } class Queue { Node front; Node rear; int length; public Queue() { this.front = null; this.rear = null; this.length = 0; } public void enqueue(char element) { Node newNode = new Node(element); if (this.rear == null) { this.front = this.rear = newNode; length++; return; } this.rear.next = newNode; this.rear = newNode; length++; } public char dequeue() { if (isEmpty()) { System.out.println("Queue is empty"); return ' '; } Node temp = this.front; this.front = temp.next; if (this.front == null) { this.rear = null; } length--; return temp.data; } public char peek() { if (isEmpty()) { System.out.println("Queue is empty"); return ' '; } return this.front.data; } public boolean isEmpty() { return length == 0; } public int size() { return length; } public void printQueue() { Node temp = this.front; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } 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