0% found this document useful (0 votes)
13 views

Jinclude

This document contains code for implementing a queue using linked list in C. It includes functions to enqueue, dequeue and print the queue. The code demonstrates adding nodes to the queue, removing nodes from the front and printing the updated queue after each operation.

Uploaded by

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

Jinclude

This document contains code for implementing a queue using linked list in C. It includes functions to enqueue, dequeue and print the queue. The code demonstrates adding nodes to the queue, removing nodes from the front and printing the updated queue after each operation.

Uploaded by

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

include<stdio.

h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};

struct node *front = NULL, *rear = NULL;

void enqueue(int val)


{
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;

//if it is the first node


if(front == NULL && rear == NULL)
//make both front and rear points to the new node
front = rear = newNode;
else
{
//add newnode in rear->next
rear->next = newNode;

//make the new node as the rear node


rear = newNode;
}
}

void dequeue()
{
//used to free the first node after dequeue
struct node *temp;

if(front == NULL)
printf("Queue is Empty. Unable to perform dequeue\n");
else
{
//take backup
temp = front;

//make the front node points to the next node


//logically removing the front element
front = front->next;

//if front == NULL, set rear = NULL


if(front == NULL)
rear = NULL;

//free the first node


free(temp);
}

void printList()
{
struct node *temp = front;

while(temp)
{
printf("%d->",temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
printf("Queue :");
printList();
dequeue();
printf("After dequeue the new Queue :");
printList();
dequeue();
printf("After dequeue the new Queue :");
printList();

return 0;
}

You might also like