zoukankan      html  css  js  c++  java
  • 栈的顺序存储

    SeqStack.h文件

    #ifndef SEQSTACK_H
    #define SEQSTACK_H
    //栈的结构体
    typedef struct _SeqStack
    {
        void** data;//可以动态的开辟内存
        int length;//栈的大小
        int capacity;//容量
    }SeqStack;
    
    //初始化
    SeqStack* Creat_SeqStack(int capacity);
    //入栈
    void Push_SeqStack(SeqStack* stack,void* data);
    //出栈
    void Pop_SeqStack(SeqStack* stack);
    //获得栈顶元素
    void* Top_SeqStack(SeqStack* stack);
    //获得栈大小
    int Size_SeqStack(SeqStack* stack);
    //获得栈的容量
    int Capacity_SeqStack(SeqStack* stack);
    //判断栈是否为空
    int IsEmpty_SeqStack(SeqStack* stack);
    //销毁栈
    void Destroy_SeqStack(SeqStack* stack);
    
    #endif

    SeqStack.c文件

    #include <stdio.h>
    #include <stdlib.h>
    #include "SeqStack.h"
    //初始换栈:创建栈
    SeqStack* Creat_SeqStack(int capacity)
    {
        SeqStack* stack=(SeqStack*)malloc(sizeof(SeqStack));
        stack->data=(void**)malloc(sizeof(void*)*capacity);
        if(stack->data==NULL)
        {
            return NULL;
        }
        stack->length=0;
        stack->capacity=capacity;
        return stack;
    }
    //入栈
    void Push_SeqStack(SeqStack* stack,void* data)
    {
        if(stack==NULL||data==NULL)
        {
            return ;
        }
        if(stack->length==stack->capacity)
        {
            return ;
        }
        stack->data[stack->length]=data;
        stack->length++;
    }
    //出栈
    void Pop_SeqStack(SeqStack* stack)
    {
        if(stack==NULL)
        {
            return;
        }
        stack->data[stack->length-1]=NULL;
        stack->length--;
    }
    //获得栈顶元素
    void* Top_SeqStack(SeqStack* stack)
    {
        if(stack==NULL)
        {
            return NULL;
        }
        return stack->data[stack->length-1];
    }
    //获得栈大小
    int Size_SeqStack(SeqStack* stack)
    {
        if(stack==NULL)
        {
            return -1;
        }
        return stack->length;
    }
    //获得栈的容量
    int Capacity_SeqStack(SeqStack* stack)
    {
        if(stack==NULL)
        {
            return -1;
        }
        return stack->capacity;
    }
    //判断栈是否为空
    int IsEmpty_SeqStack(SeqStack* stack)
    {
        if(stack==NULL)
        {
            return -1;
        }
        if(stack->length==0)
        {
            return 1;
        }
        return 0;
    }
    //销毁栈
    void Destroy_SeqStack(SeqStack* stack)
    {
        if(stack==NULL)
        {
            return;
        }
        if(stack->data!=NULL)
        {
            free(stack->data);
            stack->data=NULL;
        }
        free(stack);
        return ;
    }

    testSeqStack.c文件测试栈的顺序存储

    #include <stdio.h>
    #include"SeqStack.h"
    
    //自定义数据
    typedef struct _Player
    {
        char name[128];
        int age;
    }Player;
    
    int main()
    {
    
        //创建栈
        SeqStack* stack = Creat_SeqStack(200);
        //创建数据
        Player p1={"aaa",10};
        Player p2={"bbb",20};
        Player p3={"ccc",30};
        //数据入栈
        Push_SeqStack(stack,&p1);
        Push_SeqStack(stack,&p2);
        Push_SeqStack(stack,&p3);
        //打印
        while(Size_SeqStack(stack)>0)
        {
            Player* p=(Player*)Top_SeqStack(stack);
            Pop_SeqStack(stack);
            printf("Name:%s Age:%d
    ",p->name,p->age);
        }
        //销毁
        Destroy_SeqStack(stack);
        return 0;
    }
  • 相关阅读:
    SpringCloud Alibaba整合Sentinel
    Jmter入门教程
    惊!!!笔记本外接显示器,显示器界面不能充满全屏
    js-使用attr()方法
    关于JS的assign() 方法
    《转》webpack+vue+vueRouter模块化构建完整项目实例超详细步骤(附截图、代码、入门篇)
    元素水平垂直居中的四种方式(别人文章)
    关于截取字符串substr和substring两者的区别
    JavaScript三种弹出框(alert,confirm和prompt)用法举例
    ::before和::after伪元素的用法
  • 原文地址:https://www.cnblogs.com/jueshi0208/p/5549281.html
Copyright © 2011-2022 走看看