/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node *front, *rear;
};
struct Node* createNode(int newdata);
void enQueue(struct Queue* q, int value) {
struct Node* newNode = createNode(value);
if (q->front == NULL)
q->front = newNode;
else
q->rear->next = newNode;
q->rear = newNode;
q->rear->next = q->front;
}
int deQueue(struct Queue* q) {
if (q->front == NULL) {
return -1;
}
int value;
if (q->front == q->rear) {
value = q->front->data;
q->front = q->rear = NULL;
} else {
struct Node* temp = q->front;
value = temp->data;
q->front = q->front->next;
q->rear->next = q->front;
}
return value;
}
void printQueue(struct Queue* q) {
if (q->front == NULL) return;
struct Node* curr = q->front;
do {
curr = curr->next;
} while (curr != q->front);
}
int front(struct Queue* q) {
struct Node* front = q->front;
if (front == NULL) {
return -1;
}
return front->data;
}
int rear(struct Queue* q) {
struct Node* rear = q->rear;
if (rear == NULL) {
return -1;
}
return rear->data;
}
struct Queue* createQueue() {
struct Queue* q =
(struct Queue
*)malloc(sizeof(struct Queue
)); q->front = q->rear = NULL;
return q;
}
struct Node* createNode(int newdata) {
struct Node* newnode
= (struct Node
*)malloc(sizeof(struct Node
)); newnode->data = newdata;
newnode->next = NULL;
return newnode;
}
int main() {
struct Queue* q = createQueue();
enQueue(q, 14);
enQueue(q, 22);
enQueue(q, 6);
enQueue(q, 20);
printf("Front value: %d\n", front
(q
)); printf("Rear value: %d\n", rear
(q
)); printQueue(q);
printf("Deleted value = %d\n", deQueue
(q
)); printf("Deleted value = %d\n", deQueue
(q
)); printQueue(q);
return 0;
}
LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgoKICAgICAgICAgICAgICAgICAgICAgICAgICAgIE9ubGluZSBDIENvbXBpbGVyLgogICAgICAgICAgICAgICAgQ29kZSwgQ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