栈的子函数

来源:互联网 发布:ntfs for mac安装不了 编辑:程序博客网 时间:2024/04/27 21:14
#include<iostream>#include<string>typedef int ElemType;struct SNode{ElemType data;SNode* next;};void InitStack(SNode*& HS){HS = NULL;}void Push(SNode*& HS, const ElemType& item){SNode* newptr = new SNode;newptr->data = item;newptr->next = HS;HS = newptr;}ElemType Pop(SNode*& HS){if (HS == NULL){std::cerr << "Linked stack is empty!" << std::endl;exit(1);}SNode* p = HS;HS = HS->next;ElemType temp = p->data;delete p;return temp;}ElemType Peek(SNode* HS){if (HS == NULL){std::cerr << "Linked stack is empty!" << std::endl;exit(1);}return HS->data;}bool EmptyStack(SNode* HS){return HS == NULL;}void ClearStack(SNode*& HS){SNode *cp, *np;cp = HS;while (cp!=NULL){np = cp->next;delete cp;cp = np;}HS = NULL;}

原创粉丝点击