0

I'm watching a tutorial video "implementing linked list in C++". In that tutorial, I don't understand this code:

struct node{
    int data;
    node* next;
};

The struct is a datatype, and what is meant by node? Is it a datatype or it's just a name? Also, I understand the int data; and what is node* next;? I know it's a pointer, and I know next is a name, what is node? Is it a name or a data type?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Its just a name and you could name it anything you want. –  Aug 14 '21 at 07:21
  • 4
    Whatever video your watching should be supplemented (superseded, actually) by a solid language reference and/or [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Not being clear what a `struct` is, how they're declared, and/or how it is used is fundamental to C++. In short: rewind and find an earlier video, and definitely get a good reference. – WhozCraig Aug 14 '21 at 07:23
  • 1
    If you watch the tutorial without a good understanding about what _pointers_ are, it'll be hard to understand. Watch a tutorial about pointers first. – Ted Lyngmo Aug 14 '21 at 07:24
  • @Thirumal 131 - _Is it a name or a data type?_ - It is a name **of** a data type, defined by the `struct node` declaration. – Armali Aug 14 '21 at 14:19

3 Answers3

2

Generally, struct keyword is used as follows

struct some_name_to_identify_the_structure{
  data types
};

In your code, it is defined as follows

struct node{
int data;
node *next;
};

this can be written as follows too

struct some_random_name_123{
int data;
some_random_name_123 *next;
};

So, it's just a name and node *next is a pointer to the structure itself.

  • how should I call it , I mean if I create a " char function() { //some code };" I call it as a function , if I create a class I know it is class and I tell others it is a class , how should I tell other people what's it ,, is it a function that I creating using struct datatype ?? what is a name of it ?+ –  Aug 14 '21 at 08:43
  • @Thirumal131 struct is a custom data type, that can contain different types of data types. Consider a basket with groceries, you will have fruits, veggies, and other different stuff in it. In this case, the basket is a struct, and the groceries in it are data types. You cannot call struct like a function, instead, it is used to declare a variable. Just like you declare an integer as `int a`, you can declare a struct variable as `node a` for your case. I suggest you follow a good C programming book to understand more in depth. If you did understand it, consider accepting my answer :) – iwrestledthebeartwice Aug 14 '21 at 08:57
  • yeah! , Now I get it ,, so thanks for clear explanation . It so helpful if you recommend me a book and by the way I'm listening C++ and I know some basic like statement , loops , class , array and pointer . –  Aug 14 '21 at 09:34
1

In short, a struct is a container for related data and methods and allows access and modification to data inside the struct which can be referenced through the name it's provided with.

So node here is the name it has been provided with.

Mayank Narula
  • 419
  • 4
  • 4
0

The struct is a datatype, and what is meant by node? Is it a datatype or it's just a name? Also, I understand the int data; and what is node* next;? I know it's a pointer, and I know next is a name, what is node? Is it a name or a data type?

Node is a both a user-defined datatype and a name for your struct. node* next means a pointer variable named next that points to a memory address of type node (points to another node struct).

In terms of a linked list, you have have a struct that represents a node. This node has some data in it (in this case an integer variable) and it also has a pointer to the next or another node struct of the same type. It's like a wagon in a train of many wagons or a chain in a chainlink.

Cornel
  • 180
  • 2
  • 4
  • 13