以前参照weiss的《数据结构与算法分析》写过两篇随笔
栈ADT的链表实现
栈ADT的数组实现
因为考研的缘故,现在看了严蔚敏的《数据结构 c版》也跟着写了一遍,原理都类似
链栈:
/*链栈*/ typedef status typedef struct node Stack; typedef struct node * ptrToNode; struct node { int data; ptrToNode next; }; Status initStack(Stack &S) { S.next = NULL; return OK; } Status getTop(Stack &S, struct node &e) { if(!stackEmpty(S)) return ERROR; e = *(S.next); return OK; } Status Push(Stack &S, int e) { ptrToNode tmp = (ptrToNode)malloc(sizeof(struct node)); tmp->data = e; tmp->next = S.next; S.next = tmp; return OK; } int stackEmpty(Stack &S) { return (S.next == NULL); } Status Pop(Stack &S, int &e) { ptrToNode tmp = S.next; e = tmp->data; S.next = tmp->next; free(tmp); return OK; }