数据结构(二):栈与链栈


#include <cstdio> #include <windows.h> #define MAXSIZE 100 struct Node { int data[MAXSIZE]; int top; }; typedef struct Node MyStack; //初始化 void MyStackInit(MyStack S) { S.top = -1; } //判空 BOOL MyStackEmpty(MyStack S) { if (S.top == -1) return TRUE; else return FALSE; } //入栈 void MyStackPush(MyStack S, int x) { if (S.top = MAXSIZE - 1) { printf("栈满"); exit(0); } S.top++; S.data[S.top] = x; } //出栈 int MyStackPop(MyStack S) { int x; if (MyStackEmpty(S) == TRUE) { printf("栈空"); exit(0); } else { x = S.data[S.top]; S.top--; return x; } } //取栈顶 int MyStackGetPop(MyStack S) { if (MyStackEmpty(S) == TRUE) { printf("栈空"); exit(0); } else { return S.data[S.top]; } } /********************************* * 链栈 * **********************************/ struct Node { int data; struct Node* next; }; typedef struct Node MyStackNode; typedef struct { MyStackNode* top; }MyLinkedStack; MyLinkedStack* S; //初始化 void MyLinkedStackInit(MyLinkedStack* S) { S->top = NULL; } BOOL MyLinkedStackEmpty(MyLinkedStack* S) { if (S->top == NULL) return TRUE; else return FALSE; } void MyLinkedStackPush(MyLinkedStack* S, int x) { MyStackNode* ms; ms = (MyStackNode*)malloc(sizeof(MyStackNode)); if (ms = NULL) { printf("申请失败"); exit(0); } else { ms->data = x; ms->next = S->top; S->top = ms; } } int MyLinkedStackPop(MyLinkedStack* S) { MyStackNode* p; int x; if (S->top == NULL) { printf("栈空"); exit(0); } x = S->top->data; p = S->top; S->top = S->top->next; free(p); return x; }

 博客写的丑的我自己不能看了。。。

posted @ 2016-07-14 22:41  Vcanccc  阅读(207)  评论(0编辑  收藏  举报