标签:sizeof define closed 技术 插入 else top creat src
链栈即链式栈,也就是说我们不用再考虑空间的大小,可随心所欲的进行数据的插入/删除了。和顺序栈一样,仍然要保持其stack的特性,只在一端进行插入和删除,后进先出。
示例代码:
#ifndef _LINKSTACK_H #define _LINKSTACK_H typedef int ElemType; typedef int Status; typedef struct linkStack { ElemType data; struct linkStack * top; }linkStack; linkStack * CreateStack(); Status MakeEmpty(); int IsEmpty(); Status Push(); Status Pop(); ElemType Top(); #endif
/* linkstack.c */ #include <stdio.h> #include <stdlib.h> #include "linkstack.h" linkStack * CreateStack() { linkStack * S; S = (linkStack *)malloc(sizeof(linkStack)); S -> top = NULL; MakeEmpty(S); return S; } Status MakeEmpty(linkStack * S) { if(S == NULL) { return FALSE; } else { while(!IsEmpty(S)) Pop(S); } return TRUE; } int IsEmpty(linkStack * S) { return S -> top == NULL; } Status Push(linkStack * S, ElemType Data) { linkStack * p; p = (linkStack *)malloc(sizeof(linkStack)); p -> data = Data; p -> top = S -> top; S -> top = p; return TRUE; } Status Pop(linkStack * S) { linkStack * p; if(IsEmpty(S)) return FALSE; p = S -> top; S -> top = S -> top -> top; free(p); return TRUE; } ElemType Top(linkStack * S) { return S -> top -> data; }
标签:sizeof define closed 技术 插入 else top creat src
原文地址:http://www.cnblogs.com/darkchii/p/7368317.html