C语言-试图制作一个数组堆栈数据结构来实现撤销/重做;原型;,为什么不是';这不管用吗(

C语言-试图制作一个数组堆栈数据结构来实现撤销/重做;原型;,为什么不是';这不管用吗(,c,data-structures,stack,C,Data Structures,Stack,基本上,我创建了一个create_app()函数来分配堆栈中的两个节点,每个节点都有一个指向数组[max]的指针;undo()弹出最后一个元素,在返回它之前,它将它添加到REDO节点的数组中。REDO()相反,会弹出数组中的最后一个元素,在返回之前将其放入Undo的数组中。我做错了什么 #include<stdio.h> #include<math.h> #include<stdlib.h> #define EMPTY_TOS (-1) typedef st

基本上,我创建了一个create_app()函数来分配堆栈中的两个节点,每个节点都有一个指向数组[max]的指针;undo()弹出最后一个元素,在返回它之前,它将它添加到REDO节点的数组中。REDO()相反,会弹出数组中的最后一个元素,在返回之前将其放入Undo的数组中。我做错了什么

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define EMPTY_TOS (-1)

typedef struct node *node_ptr;

struct node
{
    int arr_size;
    int tos;
    int *arr_stack;
    node_ptr next;

};

typedef node_ptr STACK;

STACK
create_app(int max)
{
    STACK UNDO = (STACK) malloc(sizeof(struct node));
    STACK REDO = (STACK) malloc(sizeof(struct node));
    
    {
        UNDO->arr_stack == (int *) malloc(max * sizeof(int));
        REDO->arr_stack == (int *) malloc(max * sizeof(int));

        if(UNDO->arr_stack != NULL){printf("Out of space!");}
        else
        {
            UNDO->tos = EMPTY_TOS;
            REDO->tos = EMPTY_TOS;
            UNDO->arr_size = max;
            REDO->arr_size = max;
            UNDO->next = REDO;
            REDO->next = UNDO;
            return UNDO;
        }
        

    }
    
}

int
isEmpty(STACK S)
{
    return(S->tos==-1);
}

int
isFull(STACK S)
{
    return(S->tos>=S->arr_size-1);
}

void
push(int x, STACK S)
{
    if(isFull(S)){printf("Stack full!");}
    else
    {
        S->arr_stack[++S->tos] = x;
    }
    
}

int
undo(STACK S)
{
    if(isEmpty(S)){printf("Nothing to undo!");}
    else
    {
        S->next->arr_stack[++S->next->tos] = S->arr_stack[S->tos];
        printf("%d",S->arr_stack[S->tos--]);

    }
    
}

int
redo(STACK S)
{
    if(isEmpty(S->next)){printf("Nothing to redo!");}
    else
    {
        int temp = S->next->arr_stack[S->next->tos];
        push(S->next->arr_stack[S->next->tos], S);
        S->next->tos--;
        printf("%d",temp);
    }
    
}




int main()
{
    
    
    STACK app = create_app(5);
    push(1,app);
    push(2,app);
    push(3,app);
    
    undo(app);
    undo(app);
    redo(app);
    redo(app);

    /* Expected output: 3223 */

    return 0;
   
    
        
}
#包括
#包括
#包括
#定义空的_TOS(-1)
typedef结构节点*node_ptr;
结构节点
{
内部arr_大小;
int tos;
int*arr_堆栈;
node_ptr next;
};
typedef node_ptr堆栈;
堆栈
创建应用程序(int max)
{
堆栈撤消=(堆栈)malloc(sizeof(结构节点));
堆栈重做=(堆栈)malloc(sizeof(结构节点));
{
撤消->arr_堆栈==(int*)malloc(max*sizeof(int));
重做->arr_堆栈==(int*)malloc(max*sizeof(int));
如果(UNDO->arr_stack!=NULL){printf(“空间不足!”);}
其他的
{
撤消->tos=空\u tos;
重做->待办事项=空的待办事项;
撤消->阵列大小=最大值;
重做->阵列大小=最大值;
撤消->下一步=重做;
重做->下一步=撤消;
返回撤消;
}
}
}
int
isEmpty(堆栈S)
{
返回(S->tos==-1);
}
int
已满(堆栈S)
{
返回(S->tos>=S->arr_size-1);
}
无效的
推送(整数x,堆栈S)
{
if(isFull(S)){printf(“堆栈已满!”);}
其他的
{
S->arr_堆栈[++S->tos]=x;
}
}
int
撤消(堆栈S)
{
如果(isEmpty(S)){printf(“无需撤消!”);}
其他的
{
S->next->arr_堆栈[++S->next->tos]=S->arr_堆栈[S->tos];
printf(“%d”,S->arr_堆栈[S->tos--]);
}
}
int
重做(堆栈S)
{
if(isEmpty(S->next)){printf(“无需重做!”);}
其他的
{
int temp=S->next->arr_堆栈[S->next->tos];
推送(S->next->arr_堆栈[S->next->tos],S);
S->next->tos--;
printf(“%d”,温度);
}
}
int main()
{
堆栈应用程序=创建应用程序(5);
推送(1,app);
推送(2,app);
推送(3,app);
撤销(app);
撤销(app);
重做(app);
重做(app);
/*预期产出:3223*/
返回0;
}

您的代码中出现了一些小错误,例如
create\u app()
中的这些错误,看起来像是打字错误

UNDO->arr_stack == (int *) malloc(max * sizeof(int));
REDO->arr_stack == (int *) malloc(max * sizeof(int));
                 ^
                 |

if(UNDO->arr_stack != NULL){printf("Out of space!");}
                    ^
                    |
...
一些
int
返回函数在
else
部分中没有返回任何发出警告的内容

这是修改后的代码,对我来说效果很好

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define EMPTY_TOS (-1)

typedef struct node* node_ptr;
struct node
{
    int arr_size;
    int tos;
    int *arr_stack;
    node_ptr next;

};
typedef node_ptr STACK;

STACK
create_app(int max)
{
    STACK UNDO = (STACK) malloc(sizeof(struct node));
    STACK REDO = (STACK) malloc(sizeof(struct node));
    
    {
        UNDO->arr_stack = (int *) malloc(max * sizeof(int));
        REDO->arr_stack = (int *) malloc(max * sizeof(int));

        if(UNDO->arr_stack == NULL){printf("Out of space!");
            return NULL;}
        else
        {
            UNDO->tos = EMPTY_TOS;
            REDO->tos = EMPTY_TOS;
            UNDO->arr_size = max;
            REDO->arr_size = max;
            UNDO->next = REDO;
            REDO->next = UNDO;
            return UNDO;
        }
        
    }
    
}

int
isEmpty(STACK S)
{
    return (S->tos == -1);
}

int
isFull(STACK S)
{
    return (S->tos >= S->arr_size-1);
}

void
push(int x, STACK S)
{
    if(isFull(S)){printf("Stack full!");}
    else
    {
        S->arr_stack[++S->tos] = x;
    }
    
}

void
undo(STACK S)
{
    if(isEmpty(S)){printf("Nothing to undo!");}
    else
    {
        S->next->arr_stack[++S->next->tos] = S->arr_stack[S->tos];
        printf("%d",S->arr_stack[S->tos--]);
    }
    
}

void
redo(STACK S)
{
    if(isEmpty(S->next)){printf("Nothing to redo!");}
    else
    {
        int temp = S->next->arr_stack[S->next->tos];
        push(S->next->arr_stack[S->next->tos], S);
        S->next->tos--;
        printf("%d",temp);
    }
    
}




int main()
{
    
    
    STACK app = create_app(5);
    push(1,app);
    push(2,app);
    push(3,app);
    
    undo(app);
    undo(app);
    redo(app);
    redo(app);

    /* Expected output: 3223 */

    return 0;
   
    
        
}

但是,在使用
free()

释放内存
malloc
ed时,请务必小心!谢谢!完全忘记释放内存。@swag2198如果我有空(应用程序)我会取消分配两个节点撤消和重做吗?不。你必须分别释放每个撤消和重做堆栈。你还必须释放你在其中分配的数组。基本上每个malloc都需要一个相应的free。好的,那么free(S)free(S->next)并将每个arr的tos设置为-1(或free(S->arr_stack)和free(S->arr_stack)(S->next->arr_stack)足够了吗?是的。首先释放
arr_stack
数组,然后释放
S->next
S
。请注意,将
tos
设置为-1不会释放数组。明白了!非常感谢:)
3223