酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

C 语言链栈判空操作:通过检查栈顶指针实现(附示例代码)

在 C 语言中,对于链栈的判空操作通常可以通过检查栈顶指针是否为 `NULL` 来实现。以下是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链栈的节点结构 typedef struct StackNode { int data; struct StackNode *next; } StackNode; // 初始化链栈 StackNode *initStack() { return NULL; } // 判空操作 int isEmpty(StackNode *top) { return (top == NULL); } int main() { StackNode *top = initStack(); if (isEmpty(top)) { printf("栈为空\n"); } else { printf("栈不为空\n"); } return 0; } ``` 在上述代码中,`isEmpty` 函数接受栈顶指针 `top` 作为参数,如果 `top` 为 `NULL`,则表示栈为空,函数返回 `1`;否则返回 `0`。 ######[AI写代码神器 | 239点数解答 | 2024-11-07 11:12:50]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]