• stack栈


    ./Makefile:

          1 mytest:main.o stack.o                                                                                      
          2     gcc $^ -o $@
          3 main.o:main.c
          4     gcc -I include -c $^ -o $@
          5 stack.o:stack.c
          6     gcc -I include -c $^ -o $@
          7
          8 clean:
          9     rm main.o stack.o mytest

    ./include/stack.h:

          1 #ifndef __Jerry_stack__                                                                                    
          2 #define __Jerry_stack__
          3
          4 typedef int stackelem;
          5 typedef stackelem * pstackelem;
          6
          7 typedef struct stack
          8 {
          9     stackelem *base;
         10     stackelem *top;
         11
         12     int stacksize;
         13
         14     void * (*initstack)  (void);
         15     void (*destory)   (struct stack *pstack);
         16     int (*incstack)   (struct stack **ppstack);
         17     int (*pushstack)  (struct stack *pstack, stackelem *elem);
         18     int (*popstack)  (struct stack *pstack, stackelem *elem);
         19     int (*sizestack)  (struct stack *pstack);
         20     int (*fullstack)  (struct stack *pstack);
         21     int (*emptystack)  (struct stack *pstack);
         22 }stack;
         23
         24 typedef stack * pstack;
         25
         26 #define STACKINCRSIZE (sizeof(stackelem) << 4)
         27 #define STACKINTSIZE  (STACKINCRSIZE << 10)
         28
         29
         30 /*now, I would like to use C++, but......*/
         31 extern void * initstack(void);
         32 extern void  destory(stack *pstack);
         33 /*with the help of C++, I will not use extern*/
         34
         35 /*use your own method */
         36 #define PUSH(pposition, pvar) \
         37     do \
         38     { \
         39         *(pposition) = *pvar;     \
         40         ++pposition;                  \
         41                                   \
         42     }while(0)
         43
         44
         45 /*use your own method */
         46 #define POP(pposition, pvar) \
         47     do \
         48     { \
         49         --pposition;         \
         50         *pvar = *pposition;  \
         51                              \
         52     }while(0)
         53
         54 /*use your own method */
         55 #define FREE(ppoistion) \
         56     do \
         57     { \
         58         free(ppoistion); \
         59     }while(0)
         60
         61
         62 #endif    

    ./main.c:

          1 #include "stack.h"                                                                                         
          2 #include <stdio.h>
          3
          4 int main(void)
          5 {
          6     int teni = 18;
          7     int div,mod;
          8     int eighto;
          9     int err;
         10     stackelem elem;
         11
         12     pstack pnumberstack = initstack();
         13     if(!pnumberstack)
         14     {
         15         printf("init stack error\n");
         16         return -1;
         17     }
         18
         19
         20     mod = teni % 16;
         21     teni = teni /16;
         22     while (teni)
         23     {
         24         pnumberstack->pushstack(pnumberstack, &mod);
         25         mod = teni % 16;
         26         teni = teni / 16;
         27     }
         28
         29     pnumberstack->pushstack(pnumberstack, &mod);
         30     while (pnumberstack->top != pnumberstack->base)
         31     {
         32         pnumberstack->popstack(pnumberstack, &elem);
         33         printf("%d",elem);
         34     }
         35     printf("\n");
         36     destory(pnumberstack);
         37     return 0;
         38 }

    ./stack.c:

          1 #include "stack.h"                                                                                         
          2 #include <stdio.h>
          3 #include <malloc.h>
          4
          5
          6 static int sizestack(stack *pstack);
          7 static int fullstack(stack *pstack);
          8 static int emptystack(stack *pstack);
          9 static int incstack(stack **ppstack);
         10 static int pushstack(stack *pstack, stackelem *elem);
         11 static int popstack(stack *pstack, stackelem *elem);
         12
         13
         14 void * initstack(void)
         15 {
         16     stack *pstack = malloc(sizeof(stack));
         17
         18     if(!pstack)
         19     {
         20         printf("init stack error\n");
         21         goto err_1;
         22     }
         23
         24     pstack->stacksize = STACKINTSIZE;
         25     pstack->base = (stackelem *)malloc(STACKINTSIZE *sizeof(stackelem));
         26     if (!pstack->base)
         27     {
         28         printf("init stack error\n");
         29         goto err_2;
         30     }
         31
         32
         33     pstack->top = pstack->base;
         34
         35     pstack->initstack = initstack;
         36     pstack->destory = destory;
         37     pstack->sizestack = sizestack;
         38     pstack->fullstack = fullstack;
         39     pstack->emptystack = emptystack;
         40     pstack->incstack = incstack;

         41     pstack->pushstack = pushstack;
         42     pstack->popstack = popstack;
         43     return pstack;
         44
         45 err_2:
         46     free(pstack);
         47 err_1:
         48     return NULL;
         49 }
         50
         51 void destory(stack *pstack)
         52 {
         53     if (pstack->base)
         54         FREE(pstack->base);
         55     free(pstack);
         56 }
         57
         58 int sizestack(stack *pstack)
         59 {
         60     return pstack->top - pstack->base;
         61 }
         62
         63 int fullstack(stack *pstack)
         64 {
         65     if(sizestack(pstack) == pstack->stacksize)
         66         return 1;
         67     return 0;
         68 }
         69
         70 int emptystack(stack *pstack)
         71 {
         72     if(sizestack(pstack) == 0)
         73         return 1;
         74     return 0;
         75 }
         76
         77 int incstack(stack **ppstack)
         78 {
         79         (*ppstack)->base = realloc((*ppstack)->base,
         80                                   ((*ppstack)->stacksize + STACKINCRSIZE) * sizeof(stackelem));    

         81         if(!(*ppstack)->base)
         82         {
         83             printf("incstack error\n");
         84             return -1;
         85         }
         86         return 0;
         87 }
         88         
         89 int pushstack(stack *pstack, stackelem *elem)
         90 {
         91     if (fullstack(pstack))
         92     {
         93         int err;
         94         err = incstack(&pstack);
         95         if(err < 0)
         96         {
         97             printf("pushstack errro\n");
         98             return -1;
         99         }
        100     }
        101     PUSH(pstack->top, elem);
        102     return 0;
        103 }
        104
        105 int popstack(stack *pstack, stackelem *elem)
        106 {
        107     if (emptystack(pstack))
        108     {
        109         printf("popstack error\n");
        110         return -1;
        111     }
        112     POP(pstack->top, elem);
        113     return 0;
        114 }                                                                                                          

                                                                                                   

  • 相关阅读:
    项目spring boot 写es hbase 运行内存溢出
    spring boot项目启动报错
    线程的创建启动及线程池的使用
    ajax 跨域问题处理
    spring @Value("${name}")使用
    平时服务正常,突然挂了,怎么重启都起不来,查看日志Insufficient space for shared memory file 内存文件空间不足
    oracle 特殊符号替换删除处理
    Linux——CentOS 7 systemctl和防火墙firewalld命令
    linux 查看并对外开放端口(防火墙拦截处理)
    SpringBoot 使用 Gson 序列化(禁用 Jackson)
  • 原文地址:https://www.cnblogs.com/openix/p/2797735.html
Copyright © 2020-2023  润新知