Create a struct node in C++
Today, I am back with yet another interesting topic – “Creating a custom data structure ( i.e. structures) in C++“. If you are also tired of using the old variable types and would like to use a new data type which is somewhat a combination of the existing data types, you are at the right place. By the end of this post, you will learn completely about structures and their uses in C++. So without wasting any more time, let’s get started.
What is exactly a struct node and how to implement it in C++
Structure (or struct node, as it is popularly known in the programming world) is basically a container that contains variables of different data types (known as its members), about which we guys have already studied. The simplest and the general way to implement a struct is as follows :
#include<bits/stdc++.h> using namespace std; struct node{ int val; float fval; string str; }; int main(){ struct node n; n.val = 10; n.fval = 10.05; n.str = "I am a pro coder"; return 0; }
Here we have defined a ‘struct node‘ data type, which in turn contains 3 data types – an integer value, a float value and a string. Notice carefully that the struct is defined globally (i.e. outside the main function). Inside the main function, we have first declared a struct node type variable, whose name we have kept ‘n‘. Then we have assigned values to all the 3 variables stored inside the ‘n‘ variable.
Another way of defining a structure
The other way to declare a structure is to use the ‘typedef‘ keyword. This way, we won’t have to use the keyword ‘struct‘ every time we want to declare a structure. Instead, we can make a new data type named ‘node‘. The syntax for declaring a structure using the above method is :
#include<bits/stdc++.h> using namespace std; typedef struct{ int val; float fval; string str; } node; int main(){ node n; n.val = 10; n.fval = 10.05; n.str = "I am a pro coder"; return 0; }
Here, the keyword ‘typedef’ is used to convert the defined structure into a variable named ‘node‘, which in turn, has 3 member variables named val, fval and str. In the main function, notice how instead of using the ‘struct‘ keyword, we have directly defined ‘node n‘. This is because using the typedef keyword, we have made a new data type named ‘node‘(or any other name that you like). Now you can easily print the values stored in n to see whether the code is working fine or not :
#include<bits/stdc++.h> using namespace std; typedef struct{ int val = 0; //default value for the variable is 0 float fval = 0.0; //default value for the variable is 0.0 string str = ""; //default value for the variable is "" } node; int main(){ node n; n.val = 10; n.fval = 10.05; n.str = "I am a pro coder"; cout<<n.val<<" "<<n.fval<<" "<<n.str<<endl; return 0; }
The output of the above code is :
10 10.05 I am a pro coder
Now since you guys have also learned to make a data type of your own, I would suggest trying it out yourself, so that your understanding becomes even more clear. So that is it for today, I hope I was able to make things clear to you guys. Feel free to comment down below in case of any queries/doubts. Signing off, goodbye!
Leave a Reply