第二个堆栈在反转函数内部,带指针的警告

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

second stack inside reverse function with pointers warning

问题

  1. 我有以下使用双向链表实现的堆栈,我想在reverse函数内部使用第二个堆栈,但我遇到了错误。如何做到这一点,假设我想返回`s2`
  2. ```c
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. typedef struct Node {
  6. int data;
  7. struct Node* prev;
  8. struct Node* next;
  9. } Node;
  10. //---------------------堆栈---------------------
  11. typedef struct Stack {
  12. int size;
  13. Node* head;
  14. Node* tail;
  15. int top;
  16. } Stack;
  17. const Stack stack_init = { .size = 0, .head = NULL, .tail = NULL, .top = -1 };
  18. Node* create_node(int elm) {
  19. Node* node = malloc(sizeof * node);
  20. if (!node) return node;
  21. node->data = elm;
  22. node->prev = NULL;
  23. node->next = NULL;
  24. return node;
  25. }
  26. int is_empty_s(Stack *s) {
  27. return s->tail == NULL;
  28. }
  29. void push(Stack *s, int elm) {
  30. Node* updated_head = create_node(elm);
  31. if (!s->head) {
  32. s->head = updated_head;
  33. s->tail = s->head;
  34. } else {
  35. updated_head->next = s->head;
  36. s->head->prev = updated_head;
  37. s->head = updated_head;
  38. }
  39. s->size++;
  40. s->top = s->head->data;
  41. }
  42. int pop(Stack *s) {
  43. if (!is_empty_s(s)) {
  44. Node* node = s->head;
  45. int elm = node->data;
  46. s->head = s->head->next;
  47. if (s->head) {
  48. s->head->prev = NULL;
  49. s->top = s->head->data;
  50. }
  51. else {
  52. s->tail = NULL;
  53. s->top = -1;
  54. }
  55. s->size--;
  56. free(node);
  57. return elm;
  58. }
  59. }
  60. Stack* reverse_s(Stack *s) { // 迭代:使用另一个堆栈,队列
  61. Stack *s2 = stack_init;
  62. while (s->tail) {
  63. push(s2, pop(s));
  64. }
  65. return s2;
  66. }
  67. int main() {
  68. Stack s1 = stack_init;
  69. // Queue queue1 = queue_init; { .size = 0, .head = NULL, .tail = NULL, .front = -1 };
  70. push(&s1, 5);
  71. push(&s1, 4);
  72. return 0;
  73. }

正如您所看到的,reverse函数尚未完成,我是C语言的新手,不知道如何处理这个语法。

英文:

I have the following stack implementation using doubly linked list, and I want to use a second stack inside the reverse function but I get errors. how to do that, let's say I want to return s2.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Node {
  4. int data;
  5. struct Node* prev;
  6. struct Node* next;
  7. } Node;
  8. //---------------------Stack---------------------
  9. typedef struct Stack {
  10. int size;
  11. Node* head;
  12. Node* tail;
  13. int top;
  14. } Stack;
  15. const Stack stack_init = { .size = 0, .head = NULL, .tail = NULL, .top = -1 };
  16. Node* create_node(int elm) {
  17. Node* node = malloc(sizeof * node);
  18. if (!node) return node;
  19. node->data = elm;
  20. node->prev = NULL;
  21. node->next = NULL;
  22. return node;
  23. }
  24. int is_empty_s(Stack *s) {
  25. return s->tail == NULL;
  26. }
  27. void push(Stack *s, int elm) {
  28. Node* updated_head = create_node(elm);
  29. if (!s->head) {
  30. s->head = updated_head;
  31. s->tail = s->head;
  32. } else {
  33. updated_head->next = s->head;
  34. s->head->prev = updated_head;
  35. s->head = updated_head;
  36. }
  37. s->size++;
  38. s->top = s->head->data;
  39. }
  40. int pop(Stack *s) {
  41. if (!is_empty_s(s)) {
  42. Node* node = s->head;
  43. int elm = node->data;
  44. s->head = s->head->next;
  45. if (s->head) {
  46. s->head->prev = NULL;
  47. s->top = s->head->data;
  48. }
  49. else {
  50. s->tail = NULL;
  51. s->top = -1;
  52. }
  53. s->size--;
  54. free(node);
  55. return elm;
  56. }
  57. }
  58. Stack* reverse_s(Stack *s) { // iterative: using another stack, queue
  59. Stack *s2 = stack_init;
  60. while (s->tail) {
  61. push(s2, pop(s));
  62. }
  63. return s2;
  64. }
  65. int main() {
  66. Stack s1 = stack_init;
  67. // Queue queue1 = queue_init; { .size = 0, .head = NULL, .tail = NULL, .front = -1 };
  68. push(&s1, 5);
  69. push(&s1, 4);
  70. return 0;
  71. }

As you can see the reverse function is not yet completed, I am new to C and this syntax I don't know how to handle it.

答案1

得分: 2

reverse_s 函数中,s2 的类型是 指向 Stack 的指针stack_init 具有结构类型 Stack。将 Stack 值分配给 Stack * 变量是不兼容的。

对于 reverse_s 函数有几个选项:

  1. 返回一个 Stack 结构。
  1. Stack reverse_s(Stack *s) {
  2. Stack s2 = stack_init;
  3. while (s->tail)
  4. push(&s2, pop(s));
  5. return s2;
  6. }
  1. 返回一个 指向 Stack 的指针,并为结构动态分配内存。
  1. Stack *reverse_s(Stack *s) {
  2. Stack *s2 = malloc(sizeof *s2);
  3. *s2 = stack_init;
  4. while (s->tail)
  5. push(s2, pop(s));
  6. return s2;
  7. }
  1. 修改原始结构。
  1. void reverse_s(Stack *s) {
  2. Stack s2 = stack_init;
  3. while (s->tail)
  4. push(&s2, pop(s));
  5. memcpy(s, &s2, sizeof *s);
  6. }
英文:

In reverse_s, s2 is of type pointer-to-Stack. stack_init has the structure type Stack. The assignment of a Stack value to a Stack * variable is incompatible.

A few options for reverse_s:

Return a Stack structure.

  1. Stack reverse_s(Stack *s) {
  2. Stack s2 = stack_init;
  3. while (s->tail)
  4. push(&s2, pop(s));
  5. return s2;
  6. }

Return a pointer-to-Stack, dynamically allocating memory for the structure.

  1. Stack *reverse_s(Stack *s) {
  2. Stack *s2 = malloc(sizeof *s2);
  3. *s2 = stack_init;
  4. while (s->tail)
  5. push(s2, pop(s));
  6. return s2;
  7. }

Modify the original structure.

  1. void reverse_s(Stack *s) {
  2. Stack s2 = stack_init;
  3. while (s->tail)
  4. push(&s2, pop(s));
  5. memcpy(s, &s2, sizeof *s);
  6. }

huangapple
  • 本文由 发表于 2023年2月14日 20:24:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447790.html
匿名

发表评论

匿名网友

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

确定