477 views

Consider an implementation of unsorted single linked list. Suppose it has its representation with a head and a tail pointer (i.e. pointers to the first and last nodes of the linked list). Given the representation, which of the following operation can not be implemented in $O(1)$ time ?

1. Insertion at the front of the linked list.
2. Insertion at the end of the linked list.
3. Deletion of the front node of the linked list.
4. Deletion of the last node of the linked list.
in Others
retagged | 477 views

A)Insertion at the front of the linked list.

new_node->next=head;
head=new_node;

$\Theta \left ( 1 \right )$

B) Insertion at the end of the linked list.

Tail->next=new_node;
new_node->next=NULL;

$\Theta \left ( 1 \right )$

C) Deletion of the front node of the linked list.

Next_node=Head;
free(Next_node);


$\Theta \left ( 1 \right )$

D)Deletion of the last node of linked list

Here although we have tail pointer but it will be of no use because there is no previous pointer (as it is single linked list).we need to have second last pointer so that we make it the last pointer and can free the last pointer i.e deleting the last node.So we need to traverse the whole linked list making it $\Theta \left ( n \right )$.

struct node *p,*prev;
while(p)
{
prev=p;
p=p->next;
}
prev->next=NULL;
free(p);

by Boss (16.2k points)
selected