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.
Related
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.
I am always confused what is why we are using typedef struct node for creating node rather same thing can be easily implemented using struct node only...
That's not the way it is done.
it is usually
struct node { ... };
versus
typedef struct { ... } node;
//here node is a alias for an anonymous struct or...
typedef struct node_ { ... } node; //note the two names are different
In C++ that doesn't make any particular difference: node is in any case a type.
But it is different in C, where node is a tag for the type struct node (not just node) in the first case, and just the type node in the second case
Libraries defining types that have to work the same for both C and C++ usually adopt the second form, so that they can in any other expression to mention just node, instead of struct node, like in
node* first() { ... }
instead of
struct node* first() { ... }
I am learning C++ and to do so, I'm implementing some common data structures.
I started implementing a Binary Search Tree and it went all right, so now I'm implementing a Red Black Tree because I want to work through extending the BST Nodes into RBT Nodes (by just adding a bool color to them), and here is when my problems come.
This is my Node definition:
// NODE (key, value) //
class Node {
public:
int key;
int value; // value is integer for simplicity
Node *parent;
Node *left;
Node *right;
Node();
Node(int key, int value);
Node *grandparent();
Node *uncle();
};
And then I have my RedBlackNode:
// RED BLACK NODE (node with color) //
class RedBlackNode : public Node {
public:
int key;
int value;
RedBlackNode *parent;
RedBlackNode *left;
RedBlackNode *right;
bool color;
RedBlackNode();
RedBlackNode(int key, int value);
RedBlackNode *grandparent();
RedBlackNode *uncle();
};
That looks exactly the same, except for the color, and because grandparent() and uncle() return a RedBlackNode and not a Node.
The same happens with my BinarySearchTree methods Insert(key, value) and Search(key), and I would like to know if I have to rewrite all the methods only because of that.
I've been thinking also in using templates, but that doesn't make sense to me, because a BST will only be formed by Nodes, and a RBT will be formed by colored Nodes.
Thanks for your time in advance.
I came across the code below and I'm a little confused as to its purpose.
struct bob{
int myNum;
struct bob * next;
};
static struct bob_stuff{
int theNum;
struct bob *lists;
} bob;
I know the second struct is static and being intialized as a bob struct, but why would you do that? But I'm not really sure why'd you have 2 structs.
I think that it is the names of the structures that confuse you.
It is a definition of a single linked list.
The first structure
struct bob{
int myNum;
struct bob * next;
};
defines a node of the list. That it would be more clear I will rewrite it with different names
struct Node{
int value;
struct Node * next;
};
The second structure simply defines a head of the list and the number of nodes in the list (as I thik)
static struct bob_stuff{
int theNum;
struct bob *lists;
} bob;
So it can be rewritten like
static struct SingleLinkedList{
int nodes_count;
struct Node *head;
} list;
This abstract list is used as a container for some Bob stuff.:)
That looks like "module" (or "class", if you will) state for maintaining a bunch of singly-linked lists of integers.
Of course the naming is terrible, it should be e.g.
struct list_node {
int myNum;
struct list_node *next;
};
static struct {
int theNum;
struct list_node *lists;
} listState;
Note that the name ("struct tag") bob_stuff is pointless and confusing, and should probably be removed. If you want a static, thus local, variable of struct type, chances are the tag won't be useful anyway.
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;
}