LinkStack.h
#pragma once #include<iostream> using namespace std; class LNode { public: int data; LNode* next; }; class LinkStack { public: LNode* top; LinkStack() { top = nullptr; } bool IsEmpty(){ return top == nullptr; } void push(int elem) { LNode* s; s = new LNode(); s->data = elem; s->next = top; top = s; } bool pop(int& elem) { bool res; if (IsEmpty() == true) { res = false; } else { LNode* p; p = top; top = top->next; elem = p->data; delete p; res = true; } return res; } };
main.cpp
#include"LinkStack.h" int main() { LinkStack s; int a[] = { 1,2,3,4,5,6,7,8,9 }; for (int i = 0; i < 9; i++) { s.push(a[i]); } int e; while (s.pop(e)) { cout << e << " "; } cout << endl; return 0; }