|
Linked list - August 22, 2008 at 13:10 PM by Rajmeet Ghai
Describe the steps to insert data into a singly linked list.
Answer
Steps to insert data into a singly linked list:-
Insert in middle
Data: 1, 2, 3, 5
1. Locate the node after which a new node (say 4) needs to be inserted.(say
after 3)
2. create the new node i.e. 4
3. Point the new node 4 to 5 (new_node->next = node->next(5))
4. Point the node 3 to node 4 (node->next =newnode)
Insert in the beginning
Data: 2, 3, 5
1. Locate the first node in the list (2)
2. Point the new node (say 1) to the first node. (new_node->next=first)
Insert in the end
Insert in the end
Data: 2, 3, 5
1. Locate the last node in the list (5)
2. Point the last node (5) to the new node (say 6). (node->next=new_node)
Explain how to reverse singly link list.
Answer
Reverse a singly link list using iteration:-
1. First, set a pointer ( *current) to point to the first node i.e. current=base
2. Move ahead until current!=null (till the end)
3. set another pointer (* next) to point to the next node i.e.
next=current->next
4. store reference of *next in a temporary variable ( *result) i.e
current->next=result
5. swap the result value with current i.e. result=current
6. and now swap the current value with next. i.e. current=next
7. return result and repeat from step 2
A linked list can also be reversed using recursion which eliminates the use of a
temporary variable.
Define circular linked list.
Answer
An array of pointers is an array consisting of pointers. Here, each pointer
points to a row of the matrix or an element. E.g char *array [] = {“a”, “b”}.
This is an array of pointers to to characters.
Define circular linked list.
Answer
In a circular linked list the first and the last node are linked. This means
that the last node points to the first node in the list. Circular linked list
allow quick access to first and last record using a single pointer.
|