0% found this document useful (0 votes)
9 views3 pages

PR22

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

PR22

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PRACTICAL NO.

22
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node *front, *rear;
};
struct Node* newNode(int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
return temp;
}
struct Queue* createQueue() {
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
void enqueue(struct Queue* q, int data) {
struct Node* temp = newNode(data);
if (q->rear == NULL) {
q->front = q->rear = temp;
printf("Inserted %d\n", data);
return;
}
q->rear->next = temp;
q->rear = temp;
printf("Inserted %d\n", data);
}
void dequeue(struct Queue* q) {
struct Node* temp = q->front;
if (q->front == NULL) {
printf("Queue is empty!\n");
return;
}
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
}
printf("Deleted %d\n", temp->data);
free(temp);
}
void display(struct Queue* q) {
struct Node* temp = q->front;
if (q->front == NULL) {
printf("Queue is empty!\n");
return;
}
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Queue* q = createQueue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
display(q);
dequeue(q);
display(q);
enqueue(q, 40);
display(q);
return 0;
}

OUTPUT

You might also like