1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include<stdlib.h> #include<assert.h> #include<string.h> #include"LinkStack.h" StackNode* Buynode() { StackNode *newnode = (StackNode*)malloc(sizeof(StackNode)); if(NULL!=newnode) { memset(newnode,0,sizeof(StackNode)); } return newnode; } Status InitStack(LinkStack *ps) { assert(ps!=NULL); ps->top = Buynode(); if(NULL == ps->top)return OVERFLOW; ps->cursize = 0; return OK; } void DestroyStack(LinkStack *ps) { assert(ps!=NULL); ClearStack(ps); free(ps->top); ps->top = NULL; return OK; } void ClearStack(LinkStack *ps) { assert(ps!=NULL); while(ps->cursize!=0) { DelFront(ps); } return OK; } int StackLength(const LinkStack *ps) { assert(ps!=NULL); return ps->cursize; } bool StackEmpty(const LinkStack *ps) { assert(ps!=NULL); return ps->cursize == 0; } Status Push(LinkStack *ps, SElemType val) { assert(ps!=NULL); StackNode *newnode = Buynode(); if(NULL==newnode)return OVERFLOW; newnode->next = ps->top->next; ps->top->next = newnode; newnode->data = val; ps->cursize +=1; return OK; } Status GetTop(LinkStack *ps, SElemType *pval) { assert(ps!=NULL); if(pval==NULL)return NULLPTR; if(ps->cursize<=0)return false; *pval = ps->top->next->data; return OK; } Status Pop(LinkStack *ps, SElemType *pval) { assert(ps!=NULL); if(pval==NULL)return NULLPTR; if(ps->cursize<=0)return ERROR; GetTop(ps,pval); DelNext(ps); return OK; } bool DelFront(LinkStack *ps) { assert(ps!=NULL); if(ps->cursize<=0)return false; StackNode *p = ps->top->next; ps->top->next = p->next; free(p); ps->cursize-=1; return true; }
|