zoukankan      html  css  js  c++  java
  • 数据结构(二):栈与链栈


    #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; }

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

  • 相关阅读:
    HDU 5486 Difference of Clustering 图论
    HDU 5481 Desiderium 动态规划
    hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
    HDU 5478 Can you find it 随机化 数学
    HDU 5477 A Sweet Journey 水题
    HDU 5476 Explore Track of Point 数学平几
    HDU 5475 An easy problem 线段树
    ZOJ 3829 Known Notation 贪心
    ZOJ 3827 Information Entropy 水题
    zoj 3823 Excavator Contest 构造
  • 原文地址:https://www.cnblogs.com/Vcanccc/p/5671996.html
Copyright © 2011-2022 走看看