英文:
Stack using array with c
问题
I'm studying stack using array, and I have problem understanding codes.
typedef struct tagNode
{
int Data;
} Node;
typedef struct tagArrayStack
{
int Capacity;
int Top;
Node* Nodes;
} ArrayStack;
void AS_CreateStack(ArrayStack** Stack, int Capacity)
{
(*Stack) = (ArrayStack*)malloc(sizeof(ArrayStack));
(*Stack)->Nodes = (Node*)malloc(sizeof(Node)*Capacity);
(*Stack)->Capacity = Capacity;
(*Stack)->Top = -1;
}
void AS_Push(ArrayStack* Stack, ElementType Data)
{
Stack->Top++;
Stack->Nodes[Stack->Top].Data = Data;
}
This is the code.
I understood how it works.
But I have problem understanding this.
Stack->Nodes[Stack->Top].Data
Why the code uses '.Data'?
What is the meaning and name of this?
I don't know how '.' works in C.
英文:
I'm studying stack using array, and I have problem understanding codes.
typedef struct tagNode
{
int Data;
} Node;
typedef struct tagArrayStack
{
int Capacity;
int Top;
Node* Nodes;
} ArrayStack;
void AS_CreateStack(ArrayStack** Stack, int Capacity)
{
(*Stack) = (ArrayStack*)malloc(sizeof(ArrayStack));
(*Stack)->Nodes = (Node*)malloc(sizeof(Node)*Capacity);
(*Stack)->Capacity = Capacity;
(*Stack)->Top = -1;
}
void AS_Push(ArrayStack* Stack, ElementType Data)
{
Stack->Top++;
Stack->Nodes[Stack->Top].Data = Data;
}
This is the code.
I understood how it works.
But I have problem understanding this.
Stack->Nodes[Stack->Top].Data
Why the code uses '.'Data?
What is meaning and name of this?
I don't know how '.' works in C
答案1
得分: 1
Stack
是一个指向 ArrayStack
的指针。
Stack->Nodes
是 ArrayStack
的 Nodes
成员,由 Stack
指向。它是指向 Node
的指针,特别是指向 Node
元素数组中的第一个 Node
。
Stack->Nodes[Stack->Top]
是该 Node
元素数组中索引为 Stack->Top
的元素。
Stack->Nodes[Stack->Top].Data
是 Node
元素的 Data
成员。
Stack->Nodes[Stack->Top].Data = Data;
将 Data
的值设置为函数参数中的 Data
,这是指向由 Stack->Nodes
指针指向的数组中索引为 Stack->Top
的 Node
元素的 Data
成员的值。
在这个语句中,Data
有两个含义。在.
之后,它指的是结构体的成员。在 =
的右侧,它指的是函数参数。
英文:
Stack
is a pointer to an ArrayStack
.
Stack->Nodes
is the Nodes
member of the ArrayStack
that Stack
points to. It is a pointer to a Node
, particularly to the first Node
in an array of Node
elements.
Stack->Nodes[Stack->Top]
is the element with index Stack->Top
in that array of Node
elements.
Stack->Nodes[Stack->Top].Data
is the Data
member of the Node
element.
Stack->Nodes[Stack->Top].Data = Data;
sets the value of the Data
member of the Node
element with index Stack->Top
in the array pointed to by Stack->Nodes
to the value of Data
, which is a parameter of the function.
Data
has two meanings in this statement. After the .
, it refers to a member of a structure. By itself, on the right side of =
, it refers to the function parameter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论