2

So I am learning Linked Lists in C currently and I have some doubts regarding some assignments used while creating a Linked List and Inserting nodes. I hope to understand how does the memory stuff works in the background.

struct node {
    int data;
    struct node *next
}
    
void createList(int n, struct node *head)
{
    struct node *temp, *newnode;
    
    //Assignment 1
    temp = head;
    
    //Assignment2
    temp->next = newnode
    //somethings
}

Some Context : head does not contain any data. It is just the first entry point node and it points to the first node that contains any data? temp is used for traversal and newnode is the newly created node and is changed to temp later.

How do the two assignments above work in terms of memory? In Assignment 1, am I copying contents or do the two names point to same location? Same doubts for Assignment 2.

Updated , the significant code is below.

int menu()
{
    int choice = 0;

    printf("\n\t\tMENU\n1.Create Linked List");
    printf("\n2.Display Linked List");
    printf("\nAny other number to exit\n");
    printf("\nEnter Choice : ");
    scanf("%d", &choice);

    return choice;
}

void createList(int n, struct node *head)
{
    int data;
    struct node *temp, *newNode;
    temp = head; // 'head' node with no data pointing towards 'temp'
    for (int i = 1; i <= n; i++)
    {
        newNode = malloc(sizeof(struct node));

        printf("Enter Data %d : ", i);
        scanf("%d", &data);

        newNode->data = data; // First Node
        newNode->next = NULL;

        temp->next = newNode;
        temp = temp->next;
    }
}

void displayList(struct node *head)
{
    struct node *temp;
    temp = head->next; // Link to First Node
    int i = 1;
    printf("\nStart Display\n");
    while (temp != NULL)
    {
        printf("\nData %d : %d\n", i, temp->data);
        temp = temp->next;
        i++;
    }
}

void main()
{
    int choice, n;

    struct node *head;
    head = malloc(sizeof(struct node));
    head->next = NULL;

label:
    choice = menu();
    switch (choice)
    {
    case 1:
        printf("Enter Number of Entries : ");
        scanf("%d", &n);
        createList(n, head);
        goto label;
        break;

    case 2:
        displayList(head);
        goto label;
        break;

    default:
        printf("Wrong Choice");
        exit(0);
        break;
    }
}

Edit 2 : Another doubt :

temp = temp->next

This is used for traversal but how does it work. 'next' is a sublocation in the 'struct node' which has address to next node right ? So what happens with this assignment, in the memory location ?

Would appreciate the help as this topic is turning out to be very hard to understand.

12
  • The only things being copied in the two assignments are pointers. The second assignment temp->next = newnode; is invalid because newnode contains an uninitialized value. Commented Jan 5, 2022 at 16:57
  • When is assign 'temp=head' are there two distinct pointers named 'temp' or 'head' or are they both pointing to same location in memory ? Commented Jan 5, 2022 at 17:02
  • 1
    @ThomasAMathew same location. Commented Jan 5, 2022 at 17:03
  • @Imonninger . Thank You Commented Jan 5, 2022 at 17:06
  • How is head initialized before you pass it to createList? Commented Jan 5, 2022 at 17:08

1 Answer 1

1

In the both assignments values stored in objects in the right hand side are assigned to the objects in the left hand side.

For example if the pointer head is a null pointer then after the assignment

temp = head;

the pointer temp also will be a null pointer.

If the pointer head points to some object (node) of the type struct node (stores the address of the node) then after the assignment the pointer temp also will point to this object (node). That is it will store the same memory address of the node.

To make it more clear consider the following code snippet.

int x = 10;
int *p = &x;
int *q;

q = p;

After the assignment the both pointers p and q point to the variable x. So using either of the pointers you can access the variable x as for example

printf ( "x = %d\n", *q );

*q = 20;

printf ( "x = %d\n", *p );

As for the appended code of the function

void createList(int n, struct node *head)
{
    int data;
    struct node *temp, *newNode;
    temp = head; // 'head' node with no data pointing towards 'temp'
    for (int i = 1; i <= n; i++)
    {
        newNode = malloc(sizeof(struct node));

        printf("Enter Data %d : ", i);
        scanf("%d", &data);

        newNode->data = data; // First Node
        newNode->next = NULL;

        temp->next = newNode;
        temp = temp->next;
    }
}

Then it has undefined behavior provided that the pointer head initially does not point to a dummy node and is equal to NULL.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer. Some clarification : Say if 'head' has data and next points to something else. If 'temp = head' , Is now temp pointing to location where head is pointing to. OR now is there two distinct places named 'temp' or 'head'?
@ThomasAMathew temp and head are two 'distinct place" because they are defined in different memory extents bit after the assignment they have the same value..
Thank You Vlad. A bit of clarification if you don't mind. The function 'createList', how does it work ? The code works but when I assign 'temp=head'. Does this mean 'head' has a memory address(because its a pointer) of some location and does the assignment put the memory address inside 'head' to 'temp' ? Am I correct regarding this ? Say if 'head' is containing address : #r450(location name) and now 'temp' contains the same that is 'temp' also has value #r450 and hence both 'temp' and 'head' points to same location ?. Hope you understood .Thanks for you guidance, I am really struggling here.
@ThomasAMathew The value stored in one object is assigned to another object. After the assignment the both objects have the same value. What is unclear?
Hi Vlad, when I declare 'temp' and 'head' they are pointer variables right ? If 'head' is pointing to some memory location and if I do 'temp=head', the two variables are still two distinct places in the memory right ? They are not the two names of one location right ? Note : My logical understanding of the concepts may be wildly wrong, so do clarify any concepts if you can. Thank You.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.