gpt4 book ai didi

c - 不正确的数据被插入使用指针实现的堆栈

转载 作者:太空宇宙 更新时间:2023-11-04 06:33:20 25 4
gpt4 key购买 nike

我已经使用指针实现了堆栈。我一直在尝试将其概括为与任意数据类型一起使用。我试过但无法弄清楚将不正确的数据压入堆栈的原因。

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum action {START, PUSH, POP, TOP, LENGTH, QUIT, END};
enum status {SUCCESS, FAILURE};

typedef struct node {
void *data;
struct node *lower;
} stack_node;

typedef struct stack {
size_t elem_size;
size_t stack_size;
stack_node *top;
} stack_struct;

void clear_screen(void)
{
system("cls");
}

static enum action get_user_action(void)
{
int choice = START;
do {
clear_screen();
printf("%d Push data\n"
"%d Pop Data\n"
"%d See the top of the stack\n"
"%d See the length of the stack\n"
"%d Exit\n\n"
"Enter your choice -> ", PUSH, POP, TOP, LENGTH, QUIT);
scanf("%d", &choice);
} while (!(START < choice && choice < END));
return (enum action) choice;
}

enum status stack_create(stack_struct **stack, size_t elem_size)
{
(**stack).elem_size = elem_size;
(**stack).stack_size = 0;
(**stack).top = NULL;
return SUCCESS;
}

enum status push(stack_struct **stack, void *data)
{
stack_node *node = malloc(sizeof(node));
if (node == NULL) {
return FAILURE;
}

node->data = malloc(sizeof((**stack).elem_size));
if (node->data == NULL) {
return FAILURE;
}
memcpy(node->data, data, (**stack).elem_size);

if ((**stack).top == NULL) {
node->lower = NULL;
} else {
node->lower = (**stack).top;
}
(**stack).top = node;
(**stack).stack_size += 1;
return SUCCESS;
}

enum status pop(stack_struct *stack, void *data)
{
if (stack->top == NULL) {
return FAILURE;
}
stack_node *node = stack->top;
memcpy(data, node->data, stack->elem_size);
stack->top = node->lower;

free(node->data);
free(node);

stack->stack_size -= 1;
return SUCCESS;
}

enum status peek(stack_struct *stack, void *data)
{
if (stack->top == NULL) {
return FAILURE;
}
memcpy(data, stack->top->data, stack->elem_size);
return SUCCESS;
}

void stack_delete(stack_struct *stack)
{
while (stack->top != NULL)
{
stack_node *node = stack->top;
stack->top = stack->top->lower;
free(node->data);
free(node);
}
}

int main(void)
{
enum action choice;
stack_struct *stack = malloc(sizeof(stack_struct));
if (stack == NULL)
{
printf("Not enough memory\n");
return 1;
}
stack_create(&stack, sizeof(int));

while ((choice = get_user_action()) != QUIT) {
clear_screen();
int data;
switch (choice) {

case PUSH:
printf("Enter data to be pushed -> ");
scanf("%d", &data);
if (push(&stack, &data) == SUCCESS){
printf("%d pushed onto the stack", (int)stack->top->data);
} else {
printf("Not enough memory\n");
}
break;

case POP:
if (pop(stack, &data) == SUCCESS){
printf("The data is %d\n", data);
} else {
printf("Stack underflow\n");
}
break;

case TOP:
if (peek(stack, &data) == SUCCESS){
printf("The data at top is %d\n", data);
} else {
printf("Nothing in the stack\n");
}
break;

case LENGTH:
printf("Length is %d\n", stack->stack_size);
break;

default:
assert(!"You should not have reached this.\n");

}
stack_delete(stack);
getchar();
getchar();
}
}

我压入 234 并得到一个垃圾值。

更新 1

我有一个使用指针的堆栈工作副本。它不适用于任意数据类型,仅适用于 int .它can be viewed on codereview我想到了为任意数据制作它。

更新 2

在 p0w 指出 printf 之后在main是不正确的我更正。我还更改了 pop , peekstack_delete函数使指针指向struct已通过。

printf显示正在传递正确的数据,但是 poppeek不这么认为。

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum action {START, PUSH, POP, TOP, LENGTH, QUIT, END};
enum status {SUCCESS, FAILURE};

typedef struct node {
void *data;
struct node *lower;
} stack_node;

typedef struct stack {
size_t elem_size;
size_t stack_size;
stack_node *top;
} stack_struct;

void clear_screen(void)
{
system("cls");
}

static enum action get_user_action(void)
{
int choice = START;
do {
clear_screen();
printf("%d Push data\n"
"%d Pop Data\n"
"%d See the top of the stack\n"
"%d See the length of the stack\n"
"%d Exit\n\n"
"Enter your choice -> ", PUSH, POP, TOP, LENGTH, QUIT);
scanf("%d", &choice);
} while (!(START < choice && choice < END));
return (enum action) choice;
}

enum status stack_create(stack_struct **stack, size_t elem_size)
{
(**stack).elem_size = elem_size;
(**stack).stack_size = 0;
(**stack).top = NULL;
return SUCCESS;
}

enum status push(stack_struct **stack, void *data)
{
stack_node *node = malloc(sizeof(node));
if (node == NULL) {
return FAILURE;
}

node->data = malloc(sizeof((**stack).elem_size));
if (node->data == NULL) {
return FAILURE;
}
memcpy(node->data, data, (**stack).elem_size);

if ((**stack).top == NULL) {
node->lower = NULL;
} else {
node->lower = (**stack).top;
}
(**stack).top = node;
(**stack).stack_size += 1;
return SUCCESS;
}

enum status pop(stack_struct **stack, void *data)
{
if ((**stack).top == NULL) {
return FAILURE;
}
stack_node *node = (**stack).top;
memcpy(data, node->data, (**stack).elem_size);
(**stack).top = node->lower;
node->lower = NULL;

free(node->data);
free(node);

(**stack).stack_size -= 1;
return SUCCESS;
}

enum status peek(stack_struct **stack, void *data)
{
if ((**stack).top == NULL) {
return FAILURE;
}
memcpy(data, (**stack).top->data, (**stack).elem_size);
return SUCCESS;
}

void stack_delete(stack_struct **stack)
{
while ((**stack).top != NULL)
{
stack_node *node = (**stack).top;
(**stack).top = (**stack).top->lower;
free(node->data);
free(node);
}
}

int main(void)
{
enum action choice;
stack_struct *stack = malloc(sizeof(stack_struct));
if (stack == NULL)
{
printf("Not enough memory\n");
return 1;
}
stack_create(&stack, sizeof(int));

while ((choice = get_user_action()) != QUIT) {
clear_screen();
int data;
switch (choice) {

case PUSH:
printf("Enter data to be pushed -> ");
scanf("%d", &data);
if (push(&stack, &data) == SUCCESS){
printf("%d pushed onto the stack\n", *(int *)stack->top->data);
printf("%u is top of stack", stack->top);
} else {
printf("Not enough memory\n");
}
break;

case POP:
if (pop(&stack, &data) == SUCCESS){
printf("The data is %d\n", data);
} else {
printf("Stack underflow\n");
}
break;

case TOP:
if (peek(&stack, &data) == SUCCESS){
printf("The data at top is %d\n", data);
} else {
printf("Nothing in the stack\n");
}
break;

case LENGTH:
printf("Length is %d\n", stack->stack_size);
break;

default:
assert(!"You should not have reached this.\n");

}
stack_delete(&stack);
getchar();
getchar();
}
}

最佳答案

你的结构 datavoid * 所以修复 printf

printf("%d 入栈", *(int *)stack->top->data);

但是,对于其他堆栈操作,似乎也存在其他问题。

此外,如果您打算将其作为通用堆栈,为什么要在 printf 中使用 %d

您可能需要重新访问。

关于c - 不正确的数据被插入使用指针实现的堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18041793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com