Pointer in c++ struct name - c++

I ran into this bit of code for a c++ linked list implementation.
struct node
{
int info;
struct node *next;
}*start;
What does this mean to have *start rather than just start??
What happens when it is later used like this? What does s mean it is not referenced anywhere else in the function?
struct node *temp, *s;
temp = new(struct node);

Fragment
struct node
{
int info;
struct node *next;
}*start;
is equivalent to
struct node
{
int info;
struct node *next;
};
struct node *start;
So in your first fragment, you define a structure named node and a variable named start of type struct node * within one statement. That's all.
Note that in C++ (unlike in C), you could also write
struct node
{
int info;
node *next;
};
node *start;
i.e. you can omit the struct-keyword when defining variables of type struct node.

Related

Difference between following implementation of structure in c++

struct node
{
int data;
struct node *left;
struct node *right;
};
///////////////////
struct node
{
int data;
node *left;
node *right;
};
I am unable to figure what's the difference between these two implementations. Thanks in advance for help.
There isn't any difference in C++.
In C a struct tag is not usable as the name of a type (although you can typedef a name the same as the tag and use the typedef). In C++ you don't have to go through that palaver. The first form is only allowed for backwards compatibility with C.

node1 was not declared in this scope c++ data structures

I am making this program in data-structures and i get the following error that node1 is not declared in this scope also head was not declared in this scope and size also null i dont understand what the problem is
#include<iostream>
#include<conio.h>
using namespace std;
struct node // I have created the struct of node here
{
int data;
struct node *next;
};
int main() {
node1;
node1 *head;
head = (node *) malloc(size of (node1));
if(head=null)
return;
head->data=1;
head->next=Null;
head->next=(node*)malloc(size of(node1));
head->next->data=2;
head->next->next=Null;
node1 *current=head;
while(current!Null)
{
cout<<current->data;
current=current->next;
}
}
struct node
{
/*
*/
};
node1 head;
node isn't node1;
also, it's C, not C++
Where have you defined node1? You have a struct named node. Also if you want to use node in your code instead of struct node everywhere, I recommend you usea typedef as:
typedef struct node {
//your node data members
}node;

Why does the Struct *node next works for linked list in C++?

Wouldn't it be the same to just have an embedded object of the same structure that is not a pointer?
Struct Node
{
int data;
Node next;
};
//vs
Struct Node
{
int data;
Node * next;
};
No!
Having the following struct:
struct Node {
Node other;
};
Is illegal! Node doesn't have a defined size; the compiler can't build it correctly. A Node would contain a Node which would contain a Node.. wait what?
A pointer however is fine, it just points to a section of memory. When defining a pointer, the type that it points to doesn't have to be complete, it just has to defined.
struct Node;
int main() {
Node* a; // Fine, no errors.
Node b; // Incomplete type error
}

A typedef struct syntax

typedef struct {
int Key_value;
Node *link;
}Node;
Is the above declaration valid? or should I use
typedef struct _node{
int Key_value;
_node *link;
}Node;
No, it's not valid (as you would have noticed if you tried to compile it).
The typedef alias isn't introduced until the after the typedef, so you can't use it inside itself.
The second one isn't valid either (in C), _node is not a valid type name. You must use struct _node for the self-reference.
I tend to use pre-declaration and split it:
typedef struct Node Node;
struct Node {
int Key_Value;
Node *link;
};
The thing is that you can actually give the same name to both the structure and the typedef:
typedef struct Node {
int Key_value;
struct Node *link;
} Node;
Here I have added something which would have caused your code to not compile in a C compiler: The typedef isn't created until after the structure is defined. This means we must use the structure name in the link member declaration.
This can either be solved by giving the structure a name, as above, or by declaring the typedef first:
typedef struct Node *Node;
struct Node {
int Key_value;
Node *link;
};
Also note that in C++ you don't need to use the typedef keyword, or the struct keyword when declaring the link member:
// C++ version
struct Node {
int Key_value;
Node *link;
};
Structure (and class) names can be used as types in C++.
The first declaration is invalid, because Node is not known when you declare link as a structure member. The reason is that a declaration name is visible only after the declarator (simply put, that is after a comma, equal sign, or semi-colon). So, typedef being a declaration like any other, the name Node is only visible after the final semi-colon that ends the declaration statement.
Thus, you must use the second form (the first won't even compile). However, if you're on C, note that you should prepend the struct keyword to _node, like this:
typedef struct _node {
int Key_value;
struct _node *link;
} Node;
This is not necessary if you're on C++.
Both are invalid. Here's one valid way, for C and C++:
struct Node
{
int Key_value;
struct Node *link;
};
// if C, you can also do this
typedef struct Node Node;
The main point is that whatever the type of Link is, it must be something that's already been declared. The line struct X { .... declares that struct X is a type (but does not define it yet, but that's OK).
In C you should do:
typedef struct _node {
int Key_value;
struct _node *link;
} Node;
However, if you are using C++, it's simpler to omit the typedef at all:
struct Node {
int Key_Value;
Node* link;
}

Pointer to a C++ structure

If I create a structure in C++ like this:
typedef struct node {
int item;
int occurrency;
};
I know that a structure is allocated in memory using successive spaces, but what is the name of the structure (node in this example)? A simple way to give a name to the structure?
In C++ you don't have to use typedef to name a structure type:
struct node {
int item;
int occurrency;
};
is enough.
A pointer to an instance of that struct would be defined as node* mypointer;
E.g: You want to allocate a new instance with new:
node* mypointer = new node;
In C
struct node {
int item;
int occurrency;
};
is a tag, and by itself, it doesn't represent a type.
That is why you cannot do
node n;
You have to do
struct node n;
So, to give it a "type name", many C programmers use a typedef
typedef struct node {
int item;
int occurrency;
} node;
That way you can do
node n;
Instead of
struct node n;
Also, you can omit the tag and do the following
typedef struct {
int item;
int occurrency;
} node;
However, in C++ this all changes, the typedef syntax is no longer needed. In C++ classes and structs are considered to be user-defined types by default, so you can just use the following
struct node {
int item;
int occurrency;
};
And declare nodes like this
node n;
node is the name of the type. You can have multiple objects of that type:
struct node {
int item;
int occurrency;
};
node a;
node b;
In this example, both a and b have the same type (==node), which means that they have the same layout in memory. There's both an a.item and a b.item.