gpt4 book ai didi

c - 将元素指针压入堆栈

转载 作者:行者123 更新时间:2023-11-30 15:15:59 25 4
gpt4 key购买 nike

我正在尝试将元素的指针插入堆栈,以便它返回一个指针而不是元素。

根据我有限的理解,它返回元素而不是指针。

typedef struct Stack
{
int capacity;
int size;
TLDNode* elements;
}Stack;


void push(Stack *S,TLDNode *element)
{

S->elements = element;
S->size = S->size + 1;
return;
}



Stack *S;
S = (Stack *)malloc(sizeof(Stack));
S->elements = ( TLDNode *)malloc(sizeof( TLDNode)*100);
S->size = 0;
S->capacity = 100;

PUSHTOSTACK(tld->head, S);

void PUSHTOSTACK(TLDNode *root,Stack *S) {

    PUSHTOSTACK(S,root);

}

最佳答案

堆栈结构的 elements 成员的类型为 TLDNode*,它相当于 TLDNode 数组。您想要的是指向 TLDNode指针数组,因此您需要其中另一个 *:

typedef struct Stack
{
int capacity;
int size;
TLDNode** elements;
}Stack;

从技术上讲,这只是一个指向TLDNode的指针,但这基本上相当于一个指针数组,如以下代码片段所示:

TLDNode *node_array[10]; // Array of 10 pointers to TLDNodes
TLDNode **elements = node_array; // node_array is assignable to elements

关于c - 将元素指针压入堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33293312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com