Home > Mobile >  Typedef vs tructure tag
Typedef vs tructure tag

Time:07-20

I'm learning link list. I create structure called Node to store the data and link to the next node. I have two doubts.

  1. I dont understand why we are using the structure we are creating inside the same strucutre as element to store link. Is looks like a nested structure, will the element likn will have link.data and link.link in it? It is confusing

    struct Node{ int data; struct Node* link; }node;

  2. I created a typedef for the structure Node as node, so that I can declare variables and use as datatype inside the code. But it gives me error. I'm missing some understanding here. If I use struct Node representation throught my code, it works fine. If I change it to node, i see lot of error. (except when I use it inside sizeof function)

My working code:

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

struct Node{
    int data;
    struct Node* link;
}node;

struct Node head; 

void Insert(int X);
void Print();

int main(){
    head = NULL;
    printf("How laby elements  you need?\n");
    int n,i,x;
    scanf("%d",&n);
    for(i=0;i<n;i  )
    {
        printf("Enter the elements:\n");
        scanf("%d",&x);
        Insert(x);
        Print();
    }
}

void Insert(int x)
{
    struct Node* temp = (struct Node*)malloc(sizeof(node));
    temp->data = x;
    temp->link = head;
    head = temp;
}
void Print(){
    struct Node* temp = head;
    while(temp != NULL)
    {
    printf("The list is: %d\n",temp->data);
    temp = temp->link;
    }
}

My not working code:

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

struct Node{
    int data;
    struct Node* link;
}node;

node* head; // Error

void Insert(int X);
void Print();

int main(){
    head = NULL;
    printf("How laby elements  you need?\n");
    int n,i,x;
    scanf("%d",&n);
    for(i=0;i<n;i  )
    {
        printf("Enter the elements:\n");
        scanf("%d",&x);
        Insert(x);
        Print();
    }
}

void Insert(int x)
{
    node* temp = (node*)malloc(sizeof(node)); // Error
    temp->data = x;
    temp->link = head;
    head = temp;
}
void Print(){
    node* temp = head; // Error
    while(temp != NULL)
    {
    printf("The list is: %d\n",temp->data);
    temp = temp->link;
    }
}

CodePudding user response:

I dont understand why we are using the structure we are creating inside the same strucutre as element to store link. Is looks like a nested structure

The struct Node structure doesn't contain itself. That would be impossible, as it would have infinite size.

Rather, it contains a struct Node * pointer. Specifically, this pointer will be used to locate the next node in the list. But that doesn't make the next node part of the current node.

I created a typedef for the structure Node as node

No, you didn't. There's no typedef in the code you posted.

struct Node {
    int data;
    struct Node* link;
} node;

is short for

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

struct Node node;

This creates a variable of type struct Node called node.

typedef struct Node {
    int data;
    struct Node* link;
} node;

is short for

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

typedef struct Node node;

This creates a type called node.

Note that I prefer to use lowercase for variables, and CamelCase for types, so I wouldn't use node for a type; I'd use Node instead.

typedef struct Node {
    int data;
    struct Node* link;
} Node;

CodePudding user response:

I dont understand why we are using the structure we are creating inside the same strucutre as element to store link. Is looks like a nested structure, will the element likn will have link.data and link.link in it? It is confusing

In this declaration

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

the data member link does not have the type struct Node. It has the pointer type struct Node *. So there is no nested structures. Each node of the list stores a pointer to the next node.

You forgot to place the typedef specifier in this declaration

struct Node{
    int data;
    struct Node* link;
}node;

As a result the name node in the above declaration means an object of the type struct Node. So the compiler issues an error for the following declaration

node* head;

and other usages of the name node in the program because node in this case is an identifier of an object instead of a type specifier.

You have to write

typedef struct Node{
    int data;
    struct Node* link;
}node;

node* head;

In this case the name node denotes a type specifier that is a typedef name for the type specifier struct Node..

This statement in main

head = NULL;

is redundant. The pointer head was already initialized as a null pointer in its declaration in the file scope because this declaration

node* head;

is equivalent to

node* head = NULL;

Pay attention to that it is not a good idea when functions depend on global variables as in your program on the pointer head declared in the file scope.

Do not forget at least to write a function that will free all the allocated memory for the list.

CodePudding user response:

Use typedef struct Node node; to establish the type.

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

typedef struct Node node;
struct Node{
    int data;
    node* link;
};

node* head; 

void Insert(int X);
void Print();

int main(){
    head = NULL;
    printf("How laby elements  you need?\n");
    int n,i,x;
    scanf("%d",&n);
    for(i=0;i<n;i  )
    {
        printf("Enter the elements:\n");
        scanf("%d",&x);
        Insert(x);
        Print();
    }
}

void Insert(int x)
{
    node* temp = malloc(sizeof *temp);
    temp->data = x;
    temp->link = head;
    head = temp;
}
void Print(){
    node* temp = head;
    while(temp != NULL)
    {
    printf("The list is: %d\n",temp->data);
    temp = temp->link;
    }
}
  • Related