Data Structures and Algorithms - Old Questions
9. Explain why linked list is called dynamic list? Write the algorithm for deleting a new node before a node.
Answer
AI is thinking...
A linked list is called a dynamic list because it can be used with a data collection that grows and shrinks during program execution. The exact amount of memory space required by a program depends on the data being processed and so this requirement cannot be found in advance. Linked list uses runtime allocation of memory resulting in no wastage of memory space.
Algorithm for deleting the node at the specified position of the singly linked list:
let *head be the pointer to first node in the current list 1. Read position of a node which to be deleted, let it be pos. 2. if head==NULL print “void deletion” and exit 3. Enter position of a node at which you want to delete a new node. Let this position is pos. 4. Set temp=head declare a pointer of a structure let it be *p 5. if (head ==NULL)then print “void ideletion” and exit otherwise;. 6. for(i=1; i<pos-1; i++) temp=temp->next; 7. print deleted item is temp->next->info 8. Set p=temp->next; 9. Set temp->next =temp->next->next; 10. free(p); 11. End |