• 栈的顺序存储结构及其基本运算实现

    栈是一种只能在一端进行插入或删除操作的线性表其主要特点是:“后进先出”。

    初始化结构体

    1. # define MaxSize 100
    2. typedef struct
    3. {
    4. int data[MaxSize]; //存放栈中的数据元素
    5. int top; //栈顶指针,即存放栈顶元素在data数组中的下标
    6. }SqStack; //顺序栈类型

    四个要素 :

    栈空的条件:s->top---1;
    栈满的条件:s->top==MsxSize-1(data数组的最大下标);
    元素e的进栈操作:先将栈顶指针top增1,然后将元素e放在栈顶指针处;
    出栈的操作:先将栈顶指针top处的元素取出放在e中,然后将栈顶指针减1 ;

    初始化栈

    1. void InitStack(SqStack*& s)
    2. {
    3. s = (SqStack*)malloc(sizeof(SqStack));
    4. s->top = -1;
    5. }

    销毁栈

    1. void DestoryStack(SqStack*& s)
    2. {
    3. free(s);
    4. }

    判断栈是否为空

    1. bool StackEmpty(SqStack* s)
    2. {
    3. return (s->top == -1);
    4. }

     进栈

    1. bool Push(SqStack*& s, int e)
    2. {
    3. if (s->top == MaxSize - 1)
    4. {
    5. return false;
    6. }
    7. s->top++;
    8. s->data[s->top] = e;
    9. return true;
    10. }

    出栈

    1. bool Pop(SqStack*& s, int e)
    2. {
    3. if (s->top == -1)
    4. {
    5. return false;
    6. }
    7. e = s->data[s->top--];
    8. return true;
    9. }

     取栈顶元素

    1. bool GetTop(SqStack* s, int& e)
    2. {
    3. if (s->top == -1)
    4. {
    5. return false;
    6. }
    7. e = s->data[s->top];
    8. return true;
    9. }

     栈的应用

            设计一个算法,利用顺序栈判断一个字符串是否是对称串。所谓对称串指从左向右读和从右向左读的序列相同。

    1. bool symmetry(int str[])
    2. {
    3. SqStack* s;
    4. InitStack(s); //建立并初始化一个栈
    5. for (int i = 0; str[i] != '\0'; i++)
    6. {
    7. Push(s,str[i]);
    8. } //将字符串全部输入到栈中
    9. for (int i = 0; str[i] != '\0'; i++)
    10. {
    11. int e;
    12. Pop(s, e);
    13. if (str[i] != e)
    14. {
    15. DestoryStack(s);
    16. return false;
    17. }
    18. } //将字符串与栈中比较不同直接返回false
    19. DestoryStack(s);
    20. return true;
    21. }

    共享栈:

    四个要素:

    栈空条件:栈1空为top1==-1,栈2空为top2==MaxSize。

    栈满条件:top1==top2-1。

    元素x进栈的操作:进栈1操作为top1++;data[top1]=x;进栈2的操作为top2--;data[top2]=x。

    出栈的操作:出栈1操作为x=data[top1--];出栈2操作为x=data[top2++]; 

    栈的链式存储结构及其基本运算实现

     链栈相对于顺序栈的优点是不存在栈满上溢出的情况。

    结构体初始化

    1. typedef struct linknode
    2. {
    3. int data; //数据域
    4. struct linknode* next;//指针域
    5. }LinkStNode;

    四个要素:

    栈空的条件:s->next==NULL。

    栈满的条件:不存在栈满。

    元素e进栈的操作:新建一个节点存放元素e(由p指向它),将节点p插入到头结点之后。

    出栈的操作:去除首节点的data值并将其删除。

    初始化栈

    1. void InitStack(LinkStNode*& s)
    2. {
    3. s = (LinkStNode*)malloc(sizeof(LinkStNode));
    4. s->next = NULL;
    5. }

    销毁栈

    1. void DestoryStack(LinkStNode*& s)
    2. {
    3. LinkStNode* pre = s, * p = s->next;
    4. while (p != NULL)
    5. {
    6. free(pre);
    7. pre = p;
    8. p = pre->next;
    9. }
    10. free(pre);
    11. }

    判断栈是否为空

    1. bool StackEmpty(LinkStNode* s)
    2. {
    3. return (s->next == NULL);
    4. }

     进栈

    1. bool Push(LinkStNode*& s, int e)
    2. {
    3. LinkStNode* p;
    4. p = (LinkStNode*)malloc(sizeof(LinkStNode));
    5. p->data = e;
    6. p->next = s->next;
    7. s->next = p;
    8. return true;
    9. }

    出栈

    1. bool Pop(LinkStNode*& s, int& e)
    2. {
    3. LinkStNode* p;
    4. if (s->next == NULL)
    5. {
    6. return false;
    7. }
    8. p = s->next;
    9. e = p->data;
    10. s->next = p->next;
    11. free(p);
    12. return true;
    13. }

    取栈顶元素

    1. bool GetTop(LinkStNode* s, int& e)
    2. {
    3. if (s->next == NULL)
    4. {
    5. return false;
    6. }
    7. e = s->next->data;
    8. return true;
    9. }
  • 相关阅读:
    基于Java毕业设计疫情期间社区出入管理系统源码+系统+mysql+lw文档+部署软件
    Python的PyQt框架的使用-创建主窗体篇
    七大排序:插入排序、希尔排序、选择排序、堆排序、冒泡排序、快速排序、归并排序
    生产者消费者模型
    TCP/IP 测试题(一)
    C++基础
    Spring异步任务笔记
    [附源码]Python计算机毕业设计Django小型银行管理系统
    【计算机组成与设计】-第五章 memory hierarchy(三)
    【在线编程-Python篇】Python入门 04 列表(中)
  • 原文地址:https://blog.csdn.net/qq_64585761/article/details/127831910