- 顺序栈
- 共享栈
- 链栈(带有头节点)
- 链栈(不带有头节点)
链栈就是链表,只是多了一些限制罢了。 顺序栈
#include共享栈#define MAX_SIZE 10 using namespace std; typedef struct SqStack { int data[MAX_SIZE]; int top; }SqStack; void InitStack(SqStack &S) { S.top=-1; } bool StackEmpty(SqStack &S) { if(S.top==-1) return true; else return false; } bool Push(SqStack &S,int x) { if(S.top==MAX_SIZE-1) return false; S.data[++S.top]=x; return true; } bool Pop(SqStack &S,int &x) { if(S.top==-1) return false; x=S.data[S.top--]; return true; } bool GetTop(SqStack &S,int &x) { if(S.top==-1) return false; x=S.data[S.top]; return true; } void Output(SqStack S) { printf("data="); for(int i=0;i cout< SqStack S; int e; InitStack(S); Push(S,5); Push(S,6); Push(S,100); Push(S,1); Push(S,7); Output(S); Pop(S,e); Output(S); Pop(S,e); Output(S); }
#include链栈(带有头节点)#define MAX_SIZE 10 using namespace std; typedef struct ShareStack { int data[MAX_SIZE]; int top_up; int top_down; }ShareStack; void InitShareStack(ShareStack &S) { S.top_down=-1; S.top_up=MAX_SIZE; } bool ShareStackEmpty(ShareStack &S) { if(S.top_down+1==S.top_up) return true; } bool ShareStack_downEmpty(ShareStack &S) { if(S.top_down==-1) return true; else return false; } bool ShareStack_upEmpty(ShareStack &S) { if(S.top_up==MAX_SIZE) return true; else return false; }
#include链栈(不带有头节点)#define MAX_SIZE 10 using namespace std; typedef struct LinkNode { int data; LinkNode *next; }LinkNode,*LiStack; void InitStack(LiStack &L) { L=(LinkNode *)malloc(sizeof(LinkNode)); L->next=NULL; } bool ListackEmpty(LiStack &L) { if(L->next==NULL) return true; else return false; } bool LiStackPush(LiStack &L,int x) { LinkNode *p=(LinkNode *)malloc(sizeof(LinkNode)); p->data=x; p->next=L->next; L->next=p; return true; } bool ListackPop(LiStack &L,int &x) { if(L->next==NULL) return false; LinkNode *p=L->next; x=p->data; L->next=p->next; free(p); return true; } int main() { }
#includeusing namespace std; typedef struct LinkNode { int data; LinkNode *next; }LinkNode,*LiStack; void InitStack(LiStack &L) { L=NULL; } bool LiStackEmpty(LiStack &L) { if(L==NULL) return true; else return false; } bool LiStackPush(LiStack &L) { LinkNode *p=(LinkNode *)malloc(sizeof(LinkNode)); p->next=L; L=p; } bool ListackPop(LiStack &L,int &x) { if(L==NULL) return false; LinkNode *p=L; x=p->data; L=L->next; free(p); return true; } int main() { return 0; }



