A queue is implemented using a non-circular singly linked list. The queue has a head pointer and a tail pointer, as shown in the figure. Let n denote the number of nodes in the queue. Let enqueue be implemented by inserting a new node at the head, and dequeue be implemented by deletion of a node from the tail. Which one of the following is the time complexity of the most time-efficient implementation of enqueue and dequeue, respectively, for this data structure?

A queue is implemented using a non-circular singly linked list. The queue has a head pointer and a tail pointer, as shown in the figure. Let n denote the number of nodes in the queue. Let enqueue be implemented by inserting a new node at the head, and dequeue be implemented by deletion of a node from the tail. Which one of the following is the time complexity of the most time-efficient implementation of enqueue and dequeue, respectively, for this data structure? Correct Answer θ(1), θ(n)

Enquene: Inserting new node in front.

next =  Head

Head = P

Hence two-pointer operations will take O(1) time.

Dequeue:

As the list is singly linked, we have to traverse up to the second last node for pointer manipulation, which will take O(n) time.

Enqueue:

Create a Node P.

P-->Data = Data

P-->Next = Head

Head = P

Complexity = Only pointer manipulatuon so constant = O(1)

Dequeue:temp = head;

 While( temp-Next-->Next != NULL)

{       

 temp = temp-Next;       

 }

temp-->next = NULL;

Tail = temp;

Complexity = Traversing list and then free last node and keep track of Tail pointer = O(n)

Related Questions

Given pointer to a node X in a singly linked list. Only one pointer is given, pointer to head node is not given, can we delete the node X from given linked list?
Consider a singly linked list of the form where F is a pointer to the first element in the linked list and L is the pointer to the last element in the list. The time of which of the following operations depends on the length of the list?
Consider the problem of reversing a singly linked list. To take an example, given the linked list below, the reversed linked list should look like Which one of the following statements is TRUE about the time complexity of algorithms that solve the above problem in O(1) space?
A queue is implemented using an array such that ENQUEUE and DEQUEUE operations are performed efficiently. Which one of the following statements is CORRECT (n refers to the number of items in the queue)?