recategorized by
3,830 views
2 votes
2 votes

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?

  1. Delete the last element of the list
  2. Delete the first element of the list
  3. Add an element after the last element of the list
  4. Interchange the first two elements of the list
recategorized by

3 Answers

Best answer
6 votes
6 votes

A. as we need to traverse (n-1) element to delete nthelement. time taken to do so depends on length of linked list => O(n)

B. Temp=F.

    F=F->NEXT

    FREE(TEMP)

 O(1) and it doesnt depend on the length of list

C.  F->NEXT=TEMP;

     F=TEMP; WHERE TEMP is our new element

D. CONSTANT TIME OPERATION

Ans. Is A

selected by
2 votes
2 votes

option a)if we delete last node then previous node next part must be null . so we need to traverse the list so it will take o(n) time.

option b,c and D will take o(1) time . it dosent depend upon length of list

0 votes
0 votes

Answer : A) Delete the last element of the list

consider an example:-

option A) to delete last element 5 , we need to move from start to second last of the linked list i.e 4 to make it NULL    –  O(N).

option B) delete first element 1, using F(pointer) to make it F=F->NEXT    2->3->4->5->null   – O(1).

option C) add an element after the last element of the list,  suppose to add [6,NULL] , using L (pointer)  L->NEXT=6    – O(1).

option D) interchange the first two elements of the list, 

                TEMP=F->DATA;

                F->DATA=L->DATA

                L->DATA=TEMP,       

                :-O(1)

edited by
Answer:

Related questions

5 votes
5 votes
2 answers
2
Arjun asked Apr 22, 2018
15,115 views
Given a binary-max heap. The elements are stored in an arrays as $25, 14, 16, 13, 10, 8, 12$. What is the content of the array after two delete operations?$14,13,8,12,10$...
2 votes
2 votes
4 answers
3
Arjun asked Apr 22, 2018
6,414 views
If $\text{Tree-1}$ and $\text{Tree-2}$ are the trees indicated below:Which traversals of $\text{Tree-1}$ and $\text{Tree-2}$, respectively, will produce the same sequence...
5 votes
5 votes
2 answers
4
Arjun asked Apr 22, 2018
8,561 views
Consider the following program{ int x=1; printf("%d",(*char(char*)&x)); }Assuming required header files are included and if the machine in which this program is executed ...