Q. Perform implementation of a queue using a singly linked list L. The operations INSER and DELETE should take O (1) time.
Ans:
Implementation of queue by using a singly linked list:
When implement a queue as a single liked list, a queue q consists of a list and two pointers, q.front and the q.rear.
inserting an element is stated below:
insert(q,x)
{
p=getnode(); info(p) = x; next(p) = null; if(q.rear == null)
q.front = p;
else
next(q.rear) = p;
q.rear = p;
}
deletion is stated below:
remove(q)
{
if(empty(q))
{
printf("queue overflow");
exit(1);
}
p=q.front; x=info(p); q.front=next(p); if(q.front == null) q.rear = null; freenode(p); return(x);
}