c - 等同于 C 中的 NULL

标签 c pointers null

我正在尝试用 C 语言实现简单的堆栈。这是我的简单代码:

1    #include "stdio.h"
2    #include "limits.h"
3    #include "malloc.h"
4    
5    typedef struct StackEntry StackEntry;
6    
7    struct StackEntry
8    {
9        int data;
10        StackEntry *next;
11    };
12    
13    StackEntry* createStack(){
14        return NULL;
15    }
16    
17    int isEmptyStack(StackEntry **pStackTop)
18    {
19        return *pStackTop == NULL;
20    }
21    
22    void push(StackEntry **pStackTop, int pData)
23    {
24        StackEntry *lTempStackEntry;
25        lTempStackEntry = (StackEntry*)malloc(sizeof(StackEntry));
26    
27        if(!lTempStackEntry)
28            return;
29    
30        lTempStackEntry->data = pData;
31        lTempStackEntry->next = *pStackTop;
32    
33        *pStackTop = lTempStackEntry;
34    }
35    
36    int pop(StackEntry **pStackTop)
37    {
38        int lTempData;
39        StackEntry *lTempStackEntry;
40    
41        if(isEmptyStack(pStackTop))
42           return INT_MIN;
43    
44        lTempStackEntry = *pStackTop;
45        *pStackTop = (*pStackTop)->next;
46        lTempData = lTempStackEntry->data;
47        free(lTempStackEntry);
48    
49        return lTempData;
50    }
51    
52    int stackTop(StackEntry **pStackTop)
53    {
54        if(isEmptyStack(pStackTop))
55            return INT_MIN;
56    
57        return (*pStackTop)->data;
58    }
59    
60    void deleteStack(StackEntry ** pStackTop)
61    {
62        StackEntry *secondTopNode, *topNode;
63        topNode = *pStackTop;
64        /*
65         * In this we free all the nodes from second top to bottom,
66         * by one by one attaching them to top->next. At the end
67         * we free the top node.
68         */
69        while(topNode->next)
70        {
71            secondTopNode = topNode->next;
72            topNode->next = secondTopNode->next;  //make third top node
73                                                  //second top node
74            free(secondTopNode);
75            secondTopNode = NULL;
76        }
77        free(topNode);
78        topNode = NULL;
79    }
80    
81    int main(void) {
82      // your code goes here
83        StackEntry *stack = createStack();  //stack: 0x0
84        printf("\n push 1");
85        push(&stack,1);
86        printf("\n push 2");
87        push(&stack,2);
88        printf("\n push 3");
89        push(&stack,3);
90        printf("\n stack top: %d", stackTop(&stack));
91        printf("\n pop: %d  ",pop(&stack));
92        printf("\n stack top: %d", stackTop(&stack));
93        printf("\n pop: %d  ",pop(&stack));
94        printf("\n stack top: %d", stackTop(&stack));
95    
96        deleteStack(&stack);
97        printf("\n stack deleted.");
98        printf("\n is stack empty: %d", isEmptyStack(&stack));   //here it should print 1, but its printing 0
99    
100        printf("\n push 1");
101        push(&stack,1);
102        printf("\n push 2");
103        push(&stack,2);
104    
105        printf("\n pop: %d  ",pop(&stack));
106        printf("\n pop: %d  ",pop(&stack));
107     return 0;
108    }

输出

push 1
push 2
push 3
stack top: 3
pop: 3  
stack top: 2
pop: 2  
stack top: 1
stack deleted.
is stack empty: 0
push 1
push 2
pop: 2  
pop: 1 

deleteStack() 中,我释放了所有堆栈条目并将堆栈顶部设置为 NULL。在 isEmptyStack() 中检查栈顶是否为空。所以它应该评估为 1 并且第 98 行应该打印 1(即 true),但它打印 0。 这里有什么问题吗? Here is the code on ideone .

我进行的调试工作:

enter image description here

最佳答案

在函数 deleteStack 中,您没有将堆栈设置为 null,而是将堆栈指针的副本设置为 null。

你所做的是:

topNode = *pStackTop;
topNode = NULL;

这是不正确的。

你应该做的是:

*pStackTop = NULL;

关于c - 等同于 C 中的 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36246612/

相关文章:

MySQL之谜: Null value is not different from non-null string

c - 当我尝试以相反顺序打印字符串时,为什么我的 C 程序会打印新的空白行?

c - *y++ 和++*y 的区别?

objective-c - 绑定(bind)返回默认值(使用 registerDefaults :) instead of zero 设置

pointers - Go中如何使用map作为数据载体?

c - 指向指针的指针的用途或目的 (C)

java - 重构代码以避免将对象分配给 null

c - 指向字符串数组的指针在函数中可以,但在外部不行

c - 用于嵌入式系统的简约人类可读序列化格式解析器

c - 为什么在 C 中自动变量分配在堆栈内存中而不是堆内存中?