queues using linked list

Last Updated on

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 queue using linked list

the advantage of stacks using 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 value from the user to newnode->data,link next adress 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 initialise front pointer to temp pointer .move front pointer to its next element using next address.now temp element is pointed to the first element which need to be deleted, display free temp pointed element

struct node *temp = front;
front = front -> next;