链式
#includeusing namespace std; #define SElemType char #define Status int typedef char OperandType; typedef struct SNode { SElemType data; SNode* next; }SNode, * linkStack; Status InitStack(linkStack& S) { S = (linkStack)malloc(sizeof(SNode)); if (!S) return 0; S->next = NULL; } Status DestroyStack(linkStack& S) { linkStack p = S->next, ptmp; while (p) { ptmp = p->next; free(p); p = ptmp; } free(S); return 1; } Status ClearStack(linkStack& S) { linkStack p = S->next, ptmp; while (p) { ptmp = p->next; free(p); p = ptmp; } S->next = NULL; return 1; } Status StackEmpty(linkStack& S) { return S->next == NULL; } int StackLength(linkStack S) { int ans = 0; linkStack p = S->next; while (p) { ans++; p = p->next; } return ans; } Status GetTop(linkStack S, SElemType& e) { if (S->next == NULL) return 0; e = S->next->data; return 1; } Status Push(linkStack& S, SElemType e) { SNode* p = (SNode*)malloc(sizeof(SNode)); p->data = e; p->next = S->next; S->next = p; return 1; } Status Pop(linkStack& S, SElemType& e) { if (S->next == NULL) return 0; e = S->next->data; SNode* p = S->next; S->next = p->next; free(p); return 1; } Status visit(SElemType e) { cout << e << endl; return 1; } Status StackTraverse(linkStack S, Status(*visit)(SElemType)) { if (S->next == NULL) return 0; for (int i = StackLength(S); i >= 1; i--) { linkStack p = S->next; int j = 1; while (p && j < i) { p = p->next; j++; } visit(p->data); } printf("n"); return 1; } Status StackTreverse_Top(linkStack S, Status(*visit)(SElemType)) { if (S->next == NULL) return 0; linkStack p = S->next; while (p) { visit(p->data); p = p->next; } printf("n"); return 1; } int main() { return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)