C - 如果我使用此方法弹出元素,我怎么知道堆栈是空的



我正在尝试使用单向链表在 C 中实现一个堆栈,只要堆栈不为空,它就可以正常工作。一旦清空,我的 pop 方法永远不会检测到它是空的,而是给出一些随机值。有什么方法可以让我知道堆栈是空的吗?这是我正在使用的pop方法以及示例主程序输出。

int main(int argc, char const *argv[])
{
    Node* top;
    push(&top,5);
    printf("Popped Element: %dn",pop(&top));
    printf("Popped Element: %dn",pop(&top));
    return 0;
}
int pop(Node** top)
{
    if(*top == NULL)
    {
        printf("Error: Stack is empty!n");
        return;
    }
    int temp = (*top)->iData;
    *top = (*top)->next;
    return temp;
}

输出:

Popped Element: 5
Popped Element: 1707388

编辑:这是推送的代码

void push(Node** top ,int num)
{
    Node* temp = (Node* )malloc(sizeof(Node));
    temp->iData = num;
    temp->next = *top;
    *top = temp;
}

创建新节点并将其添加到列表中时在 NULL 旁边初始化。

return -1;

而不是

return;

编辑:

初始化 *top =NULL 在主像

   int main(int argc, char const *argv[])
{
    Node* top=NULL;
    push(&top,5);
    printf("Popped Element: %dn",pop(&top));
    printf("Popped Element: %dn",pop(&top));
    return 0;
}

我认为问题来自您的节点声明,如果您声明一个 Node*,则必须在使用之前分配它。 也许可以测试一下(我在这台计算机上没有任何编译器,所以我无法自己测试它)

    int main(int argc, char const *argv[])
{
    Node* top;
    top = malloc(sizeof(*top));
    push(&top,5);
    printf("Popped Element: %dn",pop(&top));
    printf("Popped Element: %dn",pop(&top));
    return 0;
}
int pop(Node** top)
{
    if(*top == NULL)
    {
        printf("Error: Stack is empty!n");
        return;
    }
    int temp = (*top)->iData;
    *top = (*top)->next;
    return temp;
}

我希望这可以解决您的问题。

定义一个全局变量或静态变量来记住堆栈的使用情况。每次在堆栈中推送某些内容时,请使用 SizeOf(...) 调整计数器的值,并反向调整 pop。

你可以只返回节点而不是整数,如果节点NULL,列表为空,否则你可能会得到节点数据:

Node *pop (Node **top)
{
    Node *ret = NULL;
    if (*top != NULL)
    {
        ret = *top;
        *top = (*top)->next;
    }
    return ret;
}

pop函数需要返回一个整数。所以这段代码

if(*top == NULL)
    {
        printf("Error: Stack is empty!n");
        return;
    }

需要修改。

要么让pop获取指向具有该值的整数的指针,如果是这种情况,则pop函数返回 true - 或者有一个不应该在堆栈上的 have,例如 -1。

所以让returnreturn(-1);

int pop(Node** top, int *value)
{
    if(*top == NULL)
    {
        printf("Error: Stack is empty!n");
        return 0;
    }
    *value = (*top)->iData;
    *top = (*top)->next;
    return 1;
}

编辑

更改Node *top = NULL以下代码后push

void push(Node **top, int value)
{
    Node *newTop = malloc(sizeof(Node));
    newTop->next = *top;
    *top = newTop;
    newTop->iData = value;
}

节点的 Ctor 中初始化在 NULL 旁边。如果堆栈为空,则返回一个值(例如 -1)

编辑:

如果您没有 Ctor。(你使用马洛克)然后,您必须将堆栈上第一个节点的下一个字段初始化为 NULL

在创建节点时,即在 push() 中,如果第一个(Head) 元素的下一个应该是 NULL。

在您的情况下,(*top)->next不是 NULL。

所以

printf("Error: Stack is empty!n");
return;

未执行。

对于编辑:

void push(Node** top ,int num)
{
    Node* temp = (Node* )malloc(sizeof(Node));
     if(*top == NULL)
     {
         temp->next = NULL;
     }
     else
     {
         temp->next = *top;
     }
    temp->iData = num;    
    *top = temp;
}

更新的代码

#include <stdlib.h>
typedef struct
{
int iData;
struct Node *next;
}Node;
int pop(Node** top)
{
    if(*top == NULL)
    {
        printf("Error: Stack is empty!n");
        return 0;
    }
    Node *tmp = *top;
    int temp = tmp->iData;
    *top = tmp->next;
    free(tmp);
    return temp;
}
void push(Node** top ,int num)
{
    Node* temp = (Node* )malloc(sizeof(Node));
     if(*top == NULL)
     {
         temp->next = NULL;
     }
     else
     {
         temp->next = *top;
     }
    temp->iData = num;
    *top = temp;
}
int main(int argc, char const *argv[])
{
    Node* top;
    push(&top,5);
    printf("Popped Element: %dn",pop(&top));
    printf("Popped Element: %dn",pop(&top));
    return 0;
}

这将打印

Popped Element: 5
Error: Stack is empty!
Popped Element: 0

第二个Popped Element: 0是因为调用 pop() 两次。 无论如何,检测到堆栈空。

最新更新