Hello friends!!!!!

Implementation Of Queue in linked list

DATA STRUCTURES

12/26/20241 min read

#include <stdio.h>

#include<stdlib.h>

//Declaring a structure named node globally

struct node{

int data;

struct node *link;

};

//Creating structures named rear and front which are initialized with NULL

struct node *rear = NULL;

struct node *front = NULL;

//Function that enqueues the values into the queue

void Enqueue(int value){

struct node temp = (struct node)malloc(sizeof(struct node*));

temp -> data = value;

temp -> link = NULL;

if(front == NULL){

front = rear = temp;

}

else{

rear -> link = temp;

rear = temp;

}

printf("Value is Enqueued\n");

}

//Dequeue Function

void Dequeue(){

if(front==NULL){

printf("Queue is empty");

}

else{

struct node *temp = front;

front = front -> link;

free(temp);

printf("Value is Dequeued\n");

}

}

//Function that denotes the peek value

void peek(){

if(front==NULL){

printf("Queue is empty");

}

else{

printf("%d is peek value\n", front -> data);

}

}

//Function that displays the values present in the queue

void display(){

if (front==NULL){

printf("Queue is empty");

}

else{

printf("----------Displaying Values----------\n");

struct node *p = front;

while(p != NULL){

printf("%d \n", p-> data);

p = p -> link;

}

}

}

//Finally main function

int main() {

Enqueue(20);

Enqueue(30);

Enqueue(60);

Enqueue(70);

Enqueue(80);

Enqueue(90);

Dequeue();

peek();

display();

return 0;

}

Output:

Value is Enqueued

Value is Enqueued

Value is Enqueued

Value is Enqueued

Value is Enqueued

Value is Enqueued

Value is Dequeued

30 is peek value

----------Displaying Values----------

30

60

70

80

90