Queues using linked list

the queue is one type of linear data structure,it follows (FIFO) first in first out. the elements which are inserted first, that element will be removed first

create a queue using linked list

the advantage of stacks using a linked list is, there is the size limit, we can insert and deleted n number of elements

we need to define node structure first

struct Node
int data;
struct Node *next;
}*front = NULL,*rear = NULL;

front pointer is used to track the begging element

rear is used to track ending element

insertion in queue

create a newnode pointer fro the structure node, use malloc to allocate data, assign a value from the user to newnode->data, link next address of newnode to NULL

if (front==NULL) ,then the inserted element will be first element ,so initialize newnode to front,rear

front = rear = newNode

else,for the next elements link next address of rear to the newnode, initialize newnode to rear

rear -> next = newNode
rear = newNode

deleting in queue

if (front==NULL) display “Queue is Empty”

create a temp pointer and initialize a front pointer to temp pointer .move front pointer to its next element using the next address. now temp element is pointed to the first element which needs to be deleted, display free temp pointed element

struct node *temp = front;
front = front -> next;
Last updated on by vishal devxo