• retagged by
30,310 views
61 61 votes

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?

  1. $\Theta(1), \Theta(1)$
  2. $\Theta(1), \Theta(n)$
  3. $\Theta(n), \Theta(1)$
  4. $\Theta(n), \Theta(n)$

7 Answers

Best answer
67 67 votes

New node to be inserted is $P.$

Enqueue(){
    P->Data=Data
    P->Next=Head
    Head=P
}

Time Complexity $=O(1)$ Because only pointer manipulation is involved which takes constant time.

Delete Tail

Dequeue(){
    temp=head
    While(temp->Next->Next!=NULL)
         temp=temp->next
    temp->next=NULL
    tail=temp
}

Time Complexity = Time for Traversing list, free the last node and keep track of Tail pointer $= O(n)$

Answer is $B$

• edited by
12 12 votes
Even if we have the pointer to the tail of the list, in oder to delete it, we need the address of the 2nd last node which can be obtained by traversing the list. so theta(n).

Ans. is (B)
5 5 votes

Enqueue will take O(1)

Algorithm

  1. temp = head
  2. head = head -> next
  3. free(temp)

Dequeue will take O(n)

Algorithm:

  1. Traverse to pre tail node and store its address in pre_tail       ............. O(n)
  2. temp = tail
  3. tail = pre_tail
  4. free(temp)
• edited by
0 0 votes

Answer is B:

 In question there is a Single linked list of n nodes which is given. In this Linked list backward traversing is not possible. and the queue is FIFO data structure.

1)For  Enqueue:This will take θ(1) because inserting a new node at the head(first node) will take constant time.

1)For  Dequeue: In queue deletion of first inserted element, it will take θ(n) time to trace the path from the last node to the first node for deletion

 

 

 

0 0 votes
It is (B) if we use simple Linked List, and constant time if a doubly Linked List would have been given.
Answer:
Position:
Show:

Related questions

6 6 votes
2 2 answers
1.3k
1.3k views
GO Classes asked May 22, 2022
1,267 views
Suppose you implement a queue using a singly linked list with head and tail pointers so that the front of the queue is at the tail of the list, and the rear of the queue ...
2 2 votes
2 2 answers
5.3k
5.3k views
go_editor asked Jul 20, 2016
5,335 views
The efficient data structure to insert/delete a number in a stored set of number isQueueLinked listDoubly linked listBinary tree
53 53 votes
7 answers 7 answers
25.2k
25.2k views
Kathleen asked Oct 9, 2014
25,200 views
Consider the following statements:First-in-first out types of computations are efficiently supported by STACKS.Implementing LISTS on linked lists is more efficient than i...
3 3 votes
1 1 answer
647
647 views
ASUR asked Oct 26, 2025
647 views
Suppose that we use a linked list to represent a queue and that in addition to the enqueue and dequeue functions add a new operation to the queue that deletes the last el...