链栈(C++)

📅 发布时间:2026/7/9 2:36:02 👁️ 浏览次数:
链栈(C++)
#include iostream struct Linkstack { int data; Linkstack* next; }; Linkstack* push(Linkstack* top, int value) { Linkstack* node new Linkstack; node-data value; node-next top; top node; std::cout value 入栈 std::endl; return top; } Linkstack* pop(Linkstack* top) { if (top NULL) { std::cout 栈为空 std::endl; } else { Linkstack* temp top; top top-next; std::cout temp-data 出栈 std::endl; } return top; } void show(Linkstack* top) { if (top NULL) { std::cout 栈为空 std::endl; } else { std::cout 栈内元素; while (top ! NULL) { std::cout top-data ; Linkstack *temp top; top top-next; } std::cout std::endl; } } int main() { Linkstack* top NULL; top push(top, 1); top push(top, 2); top push(top, 3); top push(top, 4); show(top); top pop(top); show(top); top pop(top); show(top); return 0; }