• 顺序栈的表示和实现


    表示

    typedef struct
    {
        data_t    *data;    /* storage of the stack, 
                 * in array, dynamic allocated 
                 */
        int    top;    /* current top of the stack, the value of 
                 * top is the index of the data array.
                 * note the type of stack we create here
                 * is "full-stack", which means the top always
                 * points to the location in which the last 
                 * item was stored. A push will first 
                 * increment the stack pointer, then store
                 * the value. Another stack type is called
                 * "empty-stack": of which the top points to 
                 * the location in which the next item will be 
                 * stored. A push will first store the value, 
                 * and then increment the stack pointer. 
                 */
        int    max_depth; /* max depth of stack, initialized when the 
                    * stack is created, used to limit top
                    * not exceed the max depth 
                    */
    } sqstack_t;

    实现

    //创建
    sqstack_t *CreateEmptySqstack(int max_depth)
    {
        sqstack_t *stack;
        if (max_depth == 0)
            return NULL;
            
        stack = (sqstack_t *)malloc(sizeof(sqstack_t));
        if (stack == NULL)
            return NULL;
            
        stack->data = (data_t *)malloc(sizeof(data_t) * max_depth);
        if (stack->data == NULL) {
            free(stack);
            return NULL;
        }
        
        stack->top = -1;
        stack->max_depth = max_depth;
        return stack;
    }
    //清空
    int ClearSqstack(sqstack_t *stack)
    {
        if (NULL != stack) {
            stack->top = -1;
            return 0;
        } else {
            return -1;
        }
    
    }
    //销毁
    int DestroySqstack(sqstack_t *stack)
    {
        if (NULL != stack) {
            if (stack->data != NULL)
                free(stack->data);
            free(stack);
            return 0;
        } else {
            return -1;
        }
    }
    //是否为空
    int EmptySqstack(sqstack_t *stack)
    {
        if (NULL != stack) {
            return (-1 == stack->top ? 1 : 0);
        } else {
            return -1;
        }
    }
    //是否为满
    int FullSqstack(sqstack_t *stack)
    {
        if (NULL != stack) {
            return ((stack->max_depth - 1) == stack->top ? 1 : 0 );
        } else {
            return -1;
        }
    }
    //压栈
    int PushStack(sqstack_t *stack, data_t x)
    {
        if (NULL == stack) return -1;
        if (FullSqstack(stack)) return -1;
            
        stack->top++;
        stack->data[stack->top] = x;
        return 0;
    }
    //出栈
    int PopStack(sqstack_t *stack, data_t *x)
    {
        if (NULL == stack) return -1;
        if (EmptySqstack(stack)) return -1;
        if (NULL == x) return -1;
        
        *x = stack->data[stack->top];
        stack->top--;
        return 0;
    }
    //获取栈顶
    int GetTop(sqstack_t *stack, data_t *x)
    {
        if (NULL == stack) return -1;
        if (NULL == x) return -1;
        if(EmptySqstack) return -1;
            
        *x = stack->data[stack->top];
        return 0;
    }

    测试代码

    int Push_Pop(sqstack_t *stack, data_t x);
    
    /*
     * iterate through the stack, from the base to the top 
     * and print out info of each element
     */
    void iterate_stack(sqstack_t *stack)
    {
        int i;
        
        if (!stack) return;
    
        printf("stack = base{");
        
        if (stack->data) { /* just for protection */
            for (i = -1; i < stack->top;) {
                printf("%d,", stack->data[++i]);
            }    
        }
        
        if (1 == EmptySqstack(stack))
            printf("}top
    ");
        else
            printf("}top
    ");
    }
    
    int main(int argc, char *argv[])
    {
        sqstack_t *stack;
        int max_depth;
    
        if (argc < 2) {
            printf("Usage: %s <max_depth>
    ", argv[0]);
            return -1;
        }
    
        max_depth = atoi(argv[1]);
        
        stack = CreateEmptySqstack(max_depth);
    
        if (!stack) {
            printf("CreateEmptySqstack error
    ");
            return -1;
        }
    
        Push_Pop(stack, 1);
    
        DestroySqstack(stack);
        
        return 0;
    }
    
    int Push_Pop(sqstack_t *stack, data_t x)
    {
        data_t data_pop;
        
        if (FullSqstack(stack)) {
            printf("----- reach the max depth of the stack!
    ");
            return 0;
        } else {
            printf("Push %d
    ", x);
            PushStack(stack, x++);
            iterate_stack(stack);
    
            Push_Pop(stack, x);
    
            PopStack(stack, &data_pop);
            printf("Pop %d
    ", data_pop);
            iterate_stack(stack);
    
            return -1;
        }
    }

    结果

    Push 1
    stack = base{1}top
    Push 2
    stack = base{1,2}top
    Push 3
    stack = base{1,2,3}top
    Push 4
    stack = base{1,2,3,4}top
    Push 5
    stack = base{1,2,3,4,5}top
    Push 6
    stack = base{1,2,3,4,5,6}top
    Push 7
    stack = base{1,2,3,4,5,6,7}top
    Push 8
    stack = base{1,2,3,4,5,6,7,8}top
    ----- reach the max depth of the stack!
    Pop 8
    stack = base{1,2,3,4,5,6,7}top
    Pop 7
    stack = base{1,2,3,4,5,6}top
    Pop 6
    stack = base{1,2,3,4,5}top
    Pop 5
    stack = base{1,2,3,4}top
    Pop 4
    stack = base{1,2,3}top
    Pop 3
    stack = base{1,2}top
    Pop 2
    stack = base{1}top
    Pop 1
    stack = base{}top
  • 相关阅读:
    Python中matplotlib模块的简单使用
    Python中numpy模块的简单使用
    TensorFlow入门(矩阵基础)
    TensorFlow入门(常量变量及其基本运算)
    计算机视觉入门
    菜得一P!
    Hdu2097 Sky数
    Hdu2099 整除的尾数
    Hdu2098 分拆素数和
    Linux下用Bash语言实现简单排序的功能
  • 原文地址:https://www.cnblogs.com/vsyf/p/4915637.html
Copyright © 2020-2023  润新知