c++ - 初学数据结构遇到有一个疑问。
伊谢尔伦
伊谢尔伦 2017-04-17 14:44:57
0
4
363

教材中有

typedef struct node{
            
            ...
}node;

请问就功能而言和以下有区别吗?

typedef struct{
           ... /*相同内容*/
           
}node;
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(4)
伊谢尔伦

To put it simply, we usually use different names, such as

typedef struct node_t {
  // 具体内容
} Node;

When defining a variable, if you use node_t, you need to write it like this:

struct node_t node;

If you use Node, you can write it in this simplified way:

Node node;

If you omit node_t when writing it, you cannot use the first way of writing.

typedef struct {
  // 具体内容
} Node;

Generally node_t is used to define linked list structures

typedef struct node_t {
  // 具体内容
  struct node_t *next;
} Node;

Actually, this is just a combination of struct and typedef, which can be split into:

struct node_t {
  // 具体内容
};

typedef struct node_t Node;
黄舟

You will find a lot of explanations if you search online, and you will always find what you need
This article seems quite easy to understand. Please take a look at the detailed explanation and usage summary of the structure definition typedef struct
The following is the article Text:

Typedef means type definition. typedef struct is for the convenience of using this structure.

The specific difference is:
If struct node{ } defines the structure like this. When defining the structure variable of node, you need to write like this: struct node n;
If you use typedef, you can write like this: typedef struct node{}NODE;. When applying for variables, you can write like this: NODE n; in fact, it is equivalent to NODE being an alias of node. The difference lies in whether the struct keyword can be omitted when using it.

1 First of all:
If you want to use typedef when defining a structure type in C:
typedef struct Student
{
int no;
char name[12];
}Stu,student;
So when declaring a variable, you can: Stu stu1; or: student stu2; (Stu and student are both aliases for Student)
If there is no typedef:
struct Student
{
int no;
char name[12];
}Stu;
You must use struct Student stu1; or struct Stu stu1; to declare
and you don’t need to write it here. Student (so struct Student stu1; cannot be used anymore)
typedef struct
{
int no;
char name[12];
}Stu;

2 Secondly:
If you use typedef in c++, it will make a difference:
struct Student
{
int no;
char name[12];
} stu1;//stu1 is a variable

typedef struct Student2
{
int no;
char name[12];
}stu2;//stu2 is a structure type, that is, stu2 is an alias of Student2
Use You can directly access stu1.no
, but stu2 must first define stu2 s2;
and then s2.no=10;

刘奇

There is a difference. If there is a member of struct node type in the structure, then the second type will report an error

巴扎黑

I have encountered your problem before:

typedef struct{
    int a;
    char b;
    float c;
} TypeA;

typedef struct Node {
    int data; 
    //这里默认的是int型,如需其他类型可直接修改。 
    struc tNode*next;
    //指向Node型变量的指针
} Node;

Every structural type (assumed to be named a) has such a pointer type (assumed to be named b) inside, that is, b is a pointer type used to store the address of a structure variable of the same type as a (as shown in Figure 1.2, the structure Point A's pointer next, node B and node A pointed to by next belong to the same structural type), then the name of the structural type a must be added after the typedef struct statement that defines a. Comparing it with the previously defined structural type TypeA, you will find the difference in the definition method of the structural type Node here.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template