英文:
second stack inside reverse function with pointers warning
问题
我有以下使用双向链表实现的堆栈,我想在reverse函数内部使用第二个堆栈,但我遇到了错误。如何做到这一点,假设我想返回`s2`。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
//---------------------堆栈---------------------
typedef struct Stack {
int size;
Node* head;
Node* tail;
int top;
} Stack;
const Stack stack_init = { .size = 0, .head = NULL, .tail = NULL, .top = -1 };
Node* create_node(int elm) {
Node* node = malloc(sizeof * node);
if (!node) return node;
node->data = elm;
node->prev = NULL;
node->next = NULL;
return node;
}
int is_empty_s(Stack *s) {
return s->tail == NULL;
}
void push(Stack *s, int elm) {
Node* updated_head = create_node(elm);
if (!s->head) {
s->head = updated_head;
s->tail = s->head;
} else {
updated_head->next = s->head;
s->head->prev = updated_head;
s->head = updated_head;
}
s->size++;
s->top = s->head->data;
}
int pop(Stack *s) {
if (!is_empty_s(s)) {
Node* node = s->head;
int elm = node->data;
s->head = s->head->next;
if (s->head) {
s->head->prev = NULL;
s->top = s->head->data;
}
else {
s->tail = NULL;
s->top = -1;
}
s->size--;
free(node);
return elm;
}
}
Stack* reverse_s(Stack *s) { // 迭代:使用另一个堆栈,队列
Stack *s2 = stack_init;
while (s->tail) {
push(s2, pop(s));
}
return s2;
}
int main() {
Stack s1 = stack_init;
// Queue queue1 = queue_init; { .size = 0, .head = NULL, .tail = NULL, .front = -1 };
push(&s1, 5);
push(&s1, 4);
return 0;
}
正如您所看到的,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
.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
//---------------------Stack---------------------
typedef struct Stack {
int size;
Node* head;
Node* tail;
int top;
} Stack;
const Stack stack_init = { .size = 0, .head = NULL, .tail = NULL, .top = -1 };
Node* create_node(int elm) {
Node* node = malloc(sizeof * node);
if (!node) return node;
node->data = elm;
node->prev = NULL;
node->next = NULL;
return node;
}
int is_empty_s(Stack *s) {
return s->tail == NULL;
}
void push(Stack *s, int elm) {
Node* updated_head = create_node(elm);
if (!s->head) {
s->head = updated_head;
s->tail = s->head;
} else {
updated_head->next = s->head;
s->head->prev = updated_head;
s->head = updated_head;
}
s->size++;
s->top = s->head->data;
}
int pop(Stack *s) {
if (!is_empty_s(s)) {
Node* node = s->head;
int elm = node->data;
s->head = s->head->next;
if (s->head) {
s->head->prev = NULL;
s->top = s->head->data;
}
else {
s->tail = NULL;
s->top = -1;
}
s->size--;
free(node);
return elm;
}
}
Stack* reverse_s(Stack *s) { // iterative: using another stack, queue
Stack *s2 = stack_init;
while (s->tail) {
push(s2, pop(s));
}
return s2;
}
int main() {
Stack s1 = stack_init;
// Queue queue1 = queue_init; { .size = 0, .head = NULL, .tail = NULL, .front = -1 };
push(&s1, 5);
push(&s1, 4);
return 0;
}
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
函数有几个选项:
- 返回一个
Stack
结构。
Stack reverse_s(Stack *s) {
Stack s2 = stack_init;
while (s->tail)
push(&s2, pop(s));
return s2;
}
- 返回一个 指向 Stack 的指针,并为结构动态分配内存。
Stack *reverse_s(Stack *s) {
Stack *s2 = malloc(sizeof *s2);
*s2 = stack_init;
while (s->tail)
push(s2, pop(s));
return s2;
}
- 修改原始结构。
void reverse_s(Stack *s) {
Stack s2 = stack_init;
while (s->tail)
push(&s2, pop(s));
memcpy(s, &s2, sizeof *s);
}
英文:
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.
Stack reverse_s(Stack *s) {
Stack s2 = stack_init;
while (s->tail)
push(&s2, pop(s));
return s2;
}
Return a pointer-to-Stack, dynamically allocating memory for the structure.
Stack *reverse_s(Stack *s) {
Stack *s2 = malloc(sizeof *s2);
*s2 = stack_init;
while (s->tail)
push(s2, pop(s));
return s2;
}
Modify the original structure.
void reverse_s(Stack *s) {
Stack s2 = stack_init;
while (s->tail)
push(&s2, pop(s));
memcpy(s, &s2, sizeof *s);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论