Related lists: insert at the end using the last pointer

I am studying linked lists and want to know if the following program (basically the InsertAtEnd function) is executed correctly, which I did to insert elements at the end of the list.

The basic idea is that * HEAD points to the first element of the list and * LAST points to the last element. This saves time and computation when moving to the last item in a list, and then adds items.

#include<stdio.h>
#include<stdlib.h>

// Structure for the list element/node
struct node 
{
 int data; // Stores the data
 struct node *next; // Points to the next element in the list.
};

int InsertAtEnd(struct node **, struct node **, int); /*Declaration of the function which 
                                                    inserts elements at the end.*/

int main()
{
 struct node *HEAD=NULL; //Points to the first element in the list.
 struct node *LAST=NULL; //Points to the last element in the list.
 int i=1;

 for(i=1;i<11;i++)
   {
     InsertAtEnd(&HEAD,&LAST,i);
   }
 }

// Function to insert element at the end.
int InsertAtEnd(struct node **headref,struct node **lastref,int i)
 {
   struct node *newnode=malloc(sizeof(struct node)); /*Allocates memory for the newnode 
                                                     and store the address in pointer 
                                                     newnode*/
   newnode->data=i; // Assign value to the data variable of the newnode.
   newnode->next=NULL; // Assign NULL to the next pointer of the newnode.

   if(*headref==NULL) //Checks if the list is empty.
    {
      *headref=newnode; // Places the address of the new node in HEAD pointer.
      *lastref=newnode; // Places the address of the new node in LAST pointer.

       return 0; //Exit function
    }

/* If the list is not empty, then make the next pointer of the present last node point to the new node*/

   (*lastref)->next=newnode;

   *lastref=(*lastref)->next; // Increment LAST to point to the new last node.

    return 0;
}

The questions I want to ask are:

a) Is the above code correct for adding elements at the end (for example, the InsertAtEnd function)? (Note: I tested it on my machine and it works as expected. But I still want to confirm people from you)

b) Is the code efficient (InsertAtEnd function)?

c) ( InsertAtEnd), .

d) ? ?

+3
6

a)

b) . return void, , int

c) . , .

d) malloc . , , node . empy, . () .

, malloc NULL, .

+4

( ), .. . , , .

, , malloc(). , node . , , , malloc(). , node, , , . , . , . , .

+2

a) malloc . NULL , . , .

B + C + D) ; O (1) , , LAST, . .

+2

a) , ( , headref!= NULL, lastref == NULL).

, 0?

b) , node, . , .

- , O (1). - , , .

c) . O O (1). , - , , , 5 ​​ 5000 .

d) , , , .. node headref. node - lastref. .

+2

a) , , , . , malloc.

b) , , . , , *lastref=(*lastref)->next;, *lastref=newnode;. , .

c) (O (1)), . , .

d) , insertAtEnd . malloc.

, :

  • - ( size head - lastElement -pointers)

  • , ( , void* s)

+1

Sorry, this does not answer the question, but I thought you might find it useful. I suggest a slight change in approach:

int InsertAtEnd(struct node ***, int); /*Declaration of the function which 
                                         inserts elements at the end.*/

struct node *HEAD=NULL; //Points to the first element in the list.
struct node **LAST=&HEAD; //Points to the last (NULL) *pointer* in the list.

// Function to insert element at the end.
int InsertAtEnd(struct node ***lastrefref,int i) // no need to pass HEAD
{
    struct node *newnode=malloc(sizeof(struct node)); /*Allocates memory for the newnode 
                                                        and store the address in pointer 
                                                        newnode*/

    // And here we should be checking for errors...

    newnode->data=i; // Assign value to the data variable of the newnode.
    newnode->next=NULL; // Assign NULL to the next pointer of the newnode.

    **lastrefref = newnode; // Put new element at end of list
    *lastrefref=&(newnode->next); // Update last pointer location
}

The calling convention loses the argument and no conditions are required. If you need quick access to the last element of the list, this is a little loose, because it is not so simple.

struct <anything> ***

starting to become stupid, mind.

+1
source

Source: https://habr.com/ru/post/1761312/


All Articles