Is it good to maintain a "curr" (end of list) pointer in a Linkedlist program?

  • Is it good to maintain a "curr" (end of list) pointer in a Linkedlist program? or Should we make a function that can return a pointer to the last node of the Linkedlist?
  • What is recommended and what are the advantages and disadvantages of each approach?
+3


source to share


3 answers


The only benefit is to make the addition more efficient.

You can store a pointer to the last element, but you should only do so if you have struct

one that describes the entire list. You don't have to do this globally, as you cannot have more than one list.

struct {
  LinkedListNode next;
  //data
} LinkedListNode

      

struct

that describes the list could be as follows:



struct {
  LinkedListNode first; //required
  LinkedListNode last; //optional, makes adding quicker
} LinkedList

      

Then you have to manipulate the list of functions that accept LinkedList

, and those functions will require more work and testing on your part to ensure that the pointer is always correct.

Another disadvantage of this approach is that you cannot quickly enumerate the list. those. you can't just do this sublist = node->next;

to get a list that has missed some elements.

So an alternative approach is to have no structure LinkedList

and just store the final pointer temporarily while adding multiple items.

+1


source


The easiest way to create a linked list is when you don't need to keep track of the last node added to the list , i.e. curr. The only thing you need to take care of in this list is the head (which points to the first node in the list) and the temporary (the variable that needs to be added further to the list).

pseudocode:

`  
while(you wanna add more nodes in the list)
                   {
                              temporary->next=head;
                              head=temporary;
                   }

      

`

This list expands from right to left, i.e. 1

(head is 1), then 2->1

(now head is 2), then 3->2->1

(now head is 3) and so on, you can see that the new Added node becomes the head.

While you are adding items from left to right , you need to keep track of three things: head , temporary and current



pseudocode for this:

`
 current =head;
while(you wanna add more nodes in the list)
                   {
                              current->next=temporary;
                              current=temporary
                   }

      

`

This list expands from left to right , i.e. 1

(head is 1), then 1->2

(still head remains 2), then 1->2->3

(still head remains 3), etc., you can see that the head remains the same as new nodes are added to the list.

In the first case (a list expanding from right to left), you don't need to keep track of the current one and update it, as you would in the second case.

0


source


It's good to keep track of the beginning of the node (root) to traverse the list. Coming back to your idea, to go backwards you need to use a double / circular linked list that uses the predecessor node held by its successor node, or the last node is linked to the first, as in circular form.

Learn about Doubly LinkedList: https://en.wikipedia.org/wiki/Doubly_linked_list
Circular LinkedList: http://www.martinbroadhurst.com/articles/circular-linked-list.html

-1


source







All Articles