-1

Below are separate codes for linked lists created using structure pointers and structure variables.

// Here a linked list is created using structure pointers  
#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int data;
    struct Node * ptr;
};

void traversal(struct Node * poin) 
{
    while(poin != NULL)
    {
        printf("%d, ", poin->data);
        poin = poin->ptr; 
    }
}

int main()
{
    struct Node * head = NULL; // Structure pointers
    struct Node * second = NULL;
    struct Node * third = NULL;

    head = (struct Node *) malloc (sizeof(struct Node));
    second = (struct Node *) malloc (sizeof(struct Node));
    third = (struct Node *) malloc (sizeof(struct Node));

    head->data = 5;
    head->ptr = second;

    second->data = 23;
    second->ptr = third;

    third->data = 839;
    third->ptr = NULL;
    
    traversal(&head);
    return 0;
}

// Here the same linked list is created using structure variables
#include <stdio.h>

struct Node
{
    int data;
    struct Node * ptr;
};

// Pointer poin moves along different elements of a linked list
void traversal(struct Node * poin)
{
    while(poin != NULL)
    {
        printf("%d, ", poin->data);
        poin = poin->ptr; 
    }
}

int main()
{
    struct Node head, second, third; // Structure variables
    head.data = 5;
    head.ptr = &second;

    second.data = 23;
    second.ptr = &third;

    third.data = 839;
    third.ptr = NULL;
    
    traversal(&head);
    return 0;
}

Why linked lists are created using structure pointers while the same can be done using structure variables, if we ignore the dynamic memory allocation? It is still confusing to me.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • 5
    Because you don't know how many you are going to need to begin with -- it is a dynamic data structure... You also cannot create nodes local to a function because on return, the local copy of the in the function is destroyed. If in your example, you know you need 3 nodes, then you don't really have a list, you have 3 nodes linked by pointers. – David C. Rankin Jan 07 '21 at 07:21
  • A structure "variable" can be *copied*, but the only way to link to one without making a copy is to use pointers. – Some programmer dude Jan 07 '21 at 07:28
  • 2
    Not to mention that it's impossible to nest a structure inside itself. – Some programmer dude Jan 07 '21 at 07:29
  • 1
    You may also go through these 2 threads as well for more discussion on the topic. [Why do we declare pointer to the same struct in Linked Lists ](https://stackoverflow.com/q/32026292/7610724) and [Why the pointer in linked list is of structure type?](https://cs.stackexchange.com/q/40665) – Khalid Saifullah Jan 07 '21 at 07:36

1 Answers1

1

Linked lists are one of the "dynamic data structures", as oppopsed to static data structures like anykind of variables.
The main advantage (at least the advantage related to the "dynamic" attribute) is the possibility to change the total of the data structure at runtime. The focus is on the structure, not on the values inside the structure.
For your question, the most relevant kind of change at runtime is increasing the number of elements, i.e. adding more of them, especially adding a number of additional elements which cannot be predicted at before runtime. (I should specify runtime as the time when the program is already busy doing its job, i.e. after setting up variables.)
If you use variables (even arrays, even variable length arrrays after they have been initially created) the number is fixed. You can setup a large number, large enough for the maximum of needed elements. But if you cannot determine such a maximum you are in a dead end.

TLDR Dynamic data structures can grow (and shrink and grow again) during runtime.
Variables cannot. So I reject your "the same can be done using structure variables".

(This answer focuses on C, which you have tagged. For other languages, including C++, more aspects would have to be discussed.)

Yunnosch
  • 26,130
  • 9
  • 42
  • 54