zoukankan      html  css  js  c++  java
  • c语言实现基本的数据结构(三) 栈

    #include <stdio.h>
    #include <tchar.h>
    #include <stdlib.h>
    
    
    #define StackSize 5
    #define IncrementSize 5
    // TODO:  在此处引用程序需要的其他头文件
    struct Stack
    {
        int *base;
        int *top;
        int stacksize;
    };
    //初始化栈
    bool Init_Stack(Stack* s){
        s->base = (int*)malloc(StackSize*sizeof(int));
        if (s->base == NULL) return false;
        s->top = s->base;
        s->stacksize = StackSize;
        return true;
    }
    //销毁栈
    bool Destroy_Stack(Stack* s){
        free(s->base);
        s->base = NULL;
        s->top = NULL;
        s->stacksize = 0;
        return true;
    }
    //清空栈
    bool Clear_Stack(Stack* s){
        s->top = s->base;
        return true;
    }
    //插入元素
    bool Push(Stack* s, int value){
        if (s->top - s->base >= s->stacksize){
            s->base = (int*)realloc(s->base, (StackSize + IncrementSize)*sizeof(int));
            s->top = s->base + s->stacksize;//重置一下栈顶,感觉也没多大必要呀
            s->stacksize += IncrementSize;
        }
        *++s->top = value;//栈顶指向栈顶元素
        s->stacksize++;
        return true;
    }
    //弹出元素
    int Pop(Stack* s){
        if (s->top == s->base) return false;
        int p = *s->top--;
        s->stacksize--;
        return p;
    }
    //逆序打印
    void Print_Stack(Stack s){
        if (s.top == s.base) printf("此栈为空
    ");
        while (s.top!=s.base){
            printf("%d->", Pop(&s));
        }
        printf("
    ");
    }
    //正序打印
    void Stack_Traverse(Stack s){
        if (s.top == s.base) printf("此栈为空
    ");
        while (s.top != s.base){
            printf("%d->", *++s.base);
        }
        printf("
    ");
    }
  • 相关阅读:
    OC3(字符串,值类)
    OC2(初始化方法)
    OC1(类和对象)
    postgresql 时间戳格式为5分钟、15分钟
    centos添加ftp用户并禁止外切目录
    postgresql 判断日期是否合法
    tigerVNC的简单使用教程(CentOS的远程桌面连接)
    linux awk解析csv文件
    由windows向linux上传下载文件方法
    codeblocks linux编译告警乱码解决办法
  • 原文地址:https://www.cnblogs.com/xin1998/p/7745043.html
Copyright © 2011-2022 走看看