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

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

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 函数有几个选项:

  1. 返回一个 Stack 结构。
Stack reverse_s(Stack *s) {
    Stack s2 = stack_init;

    while (s->tail)
        push(&s2, pop(s));

    return s2;
}
  1. 返回一个 指向 Stack 的指针,并为结构动态分配内存。
Stack *reverse_s(Stack *s) {
    Stack *s2 = malloc(sizeof *s2);
    *s2 = stack_init;

    while (s->tail)
        push(s2, pop(s));

    return s2;
}
  1. 修改原始结构。
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);
}

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:

确定