创建用 C 中的链表实现的通用堆栈

发布于 2024-12-29 00:16:11 字数 646 浏览 1 评论 0原文

我想创建一个通用堆栈。我想用链表来实现它。

我创建了这个结构 stack_l

typedef struct stack
 {
   void * data;

   void (*copy)(void *o);

   struct stack *next;

 } stack_l;

我有一些问题:


  • 结构的第二个字段是指向函数的指针,用于复制新数据(在函数 Push 中通过参数传递)。 Push函数的原型是:

stack_l * Push( stack_l * head, void * d );

  1. 我将指针传递给函数副本的方式是否正确?
  2. 我必须在 Push 函数中实现它?

  • 是否有必要创建一个函数 NewStack (例如)来初始化结构体的字段,或者最好只有一个 Push 函数,如果堆栈为空,则创建第一个元素,如果至少有一个元素,则在顶部添加新元素元素?

  • void * 需要用 malloc 分配吗?

I want to create a generic stack. I want to implement it with a linked-list.

I created this structures stack_l :

typedef struct stack
 {
   void * data;

   void (*copy)(void *o);

   struct stack *next;

 } stack_l;

I have some questions:


  • The second field of the structures is a pointer to a function, to copy a new data (passing by argument in a function Push).
    The prototype of the function Push is:

stack_l * Push( stack_l * head, void * d );

  1. Is it correct how I pass the pointer to the function copy?
  2. I have to implement it in the function Push??

  • Is it necessary create a function NewStack (for example) to inizialize the field of the structures, or is better have only a Push function that create the 1st element if the stack is empty, and add new element on top if there is at least one element?

  • A void * need to be allocated with a malloc ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘然心甜 2025-01-05 00:16:11

首先,您不需要 stackstack_l 使用不同的标识符。现在,回答您的问题:

您的 Push 函数与 stack_l 中的 copy 字段的类型不兼容。如果您想在 struct 中包含指向(成员)函数的指针以使其看起来像 C++,您仍然需要将对象传递给这些函数。

是的,您必须在某个地方实现您的功能,并且不能在struct stack的定义范围内。这意味着您确实需要某种构造函数(如果您愿意,您可以将其称为 newStack)。您的构造函数必须至少初始化您的函数指针(如果您想保留它们)。如果您选择不保留这些函数指针,您可以将初始化代码很好地放在您的 push 例程中,正如您所建议的:

stack_l* stackPush(stack_l* head, void* content) {
  // head might be NULL, meaning the stack is "empty" or it might be an actual pointer
  // we don't care
  stack_l* const front = malloc(sizeof(stack_l));
  *front = (stack_l){.data = content, .next = head};
  return front;
}

请注意,您必须明确说明空堆栈是什么。因此,为了清楚起见,您可能想要实现一个构造函数:

stack_l* stackCreateEmpty() {
  retun NULL;
}

甚至可能是一个析构函数:

void stackDestroyEmpty(stack_l* s) {
  assert(s == NULL && "Tried to destroy non-empty stack.");
}

对于最后一个问题:正如您在上面看到的,您必须使用 malloc 来获取空间来保存您的 stack_l< /code> 对象,除此之外,您不需要为 void* 成员分配额外的空间,因为它是 stack_l 的一部分。

First of all, you do not require different identifiers for stack and stack_l. Now, to your questions:

Your Push function is incompatible with the type of the copy field in stack_l. If you want to include a pointer to (member-)functions in your struct to make it look like C++, you still have to pass your object to these functions.

Yes ,you have to implement your functionality somewhere, and that cannot be within the definition of struct stack. This means that you do need some sort of constructor function (you may call it newStack if you like). Your constructor must at least initialise your function pointers (if you want to keep them). If you opt not to keep these function pointers you can put the initialisation code well in your push routine, as you suggested:

stack_l* stackPush(stack_l* head, void* content) {
  // head might be NULL, meaning the stack is "empty" or it might be an actual pointer
  // we don't care
  stack_l* const front = malloc(sizeof(stack_l));
  *front = (stack_l){.data = content, .next = head};
  return front;
}

Note that you have to explicitly state what the empty stack is. So you may want to implement a constructor for clarity:

stack_l* stackCreateEmpty() {
  retun NULL;
}

And maybe even a destructor:

void stackDestroyEmpty(stack_l* s) {
  assert(s == NULL && "Tried to destroy non-empty stack.");
}

To your last question: As you see above, you have to use malloc to get space to hold your stack_l object, other than that, you do not need to allocate extra space for your void* member, as it is part of stack_l.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文