使用数组实现的堆栈(Stack using array with C)

huangapple go评论105阅读模式
英文:

Stack using array with c

问题

I'm studying stack using array, and I have problem understanding codes.

  1. typedef struct tagNode
  2. {
  3. int Data;
  4. } Node;
  5. typedef struct tagArrayStack
  6. {
  7. int Capacity;
  8. int Top;
  9. Node* Nodes;
  10. } ArrayStack;
  11. void AS_CreateStack(ArrayStack** Stack, int Capacity)
  12. {
  13. (*Stack) = (ArrayStack*)malloc(sizeof(ArrayStack));
  14. (*Stack)->Nodes = (Node*)malloc(sizeof(Node)*Capacity);
  15. (*Stack)->Capacity = Capacity;
  16. (*Stack)->Top = -1;
  17. }
  18. void AS_Push(ArrayStack* Stack, ElementType Data)
  19. {
  20. Stack->Top++;
  21. Stack->Nodes[Stack->Top].Data = Data;
  22. }

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.

  1. typedef struct tagNode
  2. {
  3. int Data;
  4. } Node;
  5. typedef struct tagArrayStack
  6. {
  7. int Capacity;
  8. int Top;
  9. Node* Nodes;
  10. } ArrayStack;
  11. void AS_CreateStack(ArrayStack** Stack, int Capacity)
  12. {
  13. (*Stack) = (ArrayStack*)malloc(sizeof(ArrayStack));
  14. (*Stack)->Nodes = (Node*)malloc(sizeof(Node)*Capacity);
  15. (*Stack)->Capacity = Capacity;
  16. (*Stack)->Top = -1;
  17. }
  18. void AS_Push(ArrayStack* Stack, ElementType Data)
  19. {
  20. Stack->Top++;
  21. Stack->Nodes[Stack->Top].Data = Data;
  22. }

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->NodesArrayStackNodes 成员,由 Stack 指向。它是指向 Node 的指针,特别是指向 Node 元素数组中的第一个 Node

Stack->Nodes[Stack->Top] 是该 Node 元素数组中索引为 Stack->Top 的元素。

Stack->Nodes[Stack->Top].DataNode 元素的 Data 成员。

Stack->Nodes[Stack->Top].Data = Data;Data 的值设置为函数参数中的 Data,这是指向由 Stack->Nodes 指针指向的数组中索引为 Stack->TopNode 元素的 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.

huangapple
  • 本文由 发表于 2023年4月10日 20:44:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定