zoukankan      html  css  js  c++  java
  • 链式栈的表示和实现

    表示

    typedef struct node_t {
        
        data_t        data;
        struct node_t    *next;
        
    } linknode_t, *linkstack_t;

    实现

    //创建
    linkstack_t CreateEmptyLinkstack(void)
    {
        linkstack_t stack;
        stack = (linkstack_t)malloc(sizeof(linknode_t));
        if (NULL != stack)
            stack->next = NULL;
        return stack;
    }
    //清空
    int ClearLinkstack(linkstack_t stack)
    {
        linknode_t *node;
        if (NULL != stack) {
            while (NULL != stack->next) {
                node = stack->next;
                stack->next = node->next;
                free(node);
            }
            return 0;
        } else {
            return -1;
        }
    }
    //销毁
    int DestroyLinkstack(linkstack_t stack)
    {
        if (NULL != stack) {
            ClearLinkstack(stack);
            free(stack);
        } else {
            return -1;
        }
    }
    //是否为空
    int EmptyLinkstack(linkstack_t stack)
    {
        if (NULL == stack) return -1;
        
        if (NULL != stack->next) {
            return 0;
        } else {
            return 1;
        }
    }
    //压栈
    int PushStack(linkstack_t stack, data_t x)
    {
        if (NULL == stack) return -1;
        
        linknode_t *stack_new;
        stack_new = (linkstack_t)malloc(sizeof(linknode_t));
        if (NULL == stack_new) return -1;
        stack_new->data = x;
        
        stack_new->next = stack->next;
        stack->next = stack_new;
        return 0;
    }
    //出栈
    int PopStack(linkstack_t stack, data_t *x)
    {
        if (NULL == stack) return -1;
        if (NULL == stack->next) return -1;
            
        linknode_t *node;
        node = stack->next;
        stack->next = node->next;
        if (NULL != x)
            *x = node->data;
        free(node);
        return 0;
        
    }

    测试代码

    int Push_Pop(linkstack_t stack, data_t x);
    
    /*
     * iterate through the stack, from the top to the base 
     * and print out info of each element
     */
    void iterate_stack(linkstack_t stack)
    {
        linknode_t *node; /* pointer to the node to be iterated */
        
        if (NULL == stack) return;
    
        printf("stack = top{");
    
        node = stack->next;
        while (NULL != node) {
            printf("%d->", node->data);
            node = node->next;
        }
        
        if (1 == EmptyLinkstack(stack))
            printf("}base
    ");
        else
            printf("}base
    ");
    }
    
    int main(int argc, char *argv[])
    {
        linkstack_t stack;
    
        if (argc < 2) {
            printf("Usage: %s <len>
    ", argv[0]);
            return -1;
        }
    
        max_depth = atoi(argv[1]);
        
        stack = CreateEmptyLinkstack();
    
        if (NULL == stack) {
            printf("CreateEmptyLinkstack error
    ");
            return -1;
        }
    
        Push_Pop(stack, 1);
    
        DestroyLinkstack(stack);
        
        return 0;
    }
    
    int Push_Pop(linkstack_t stack, data_t x)
    {
        data_t data_pop;
        static int    depth = 0;    
    
        if (depth == max_depth) {
            printf("----- reach the max depth of the stack!
    ");
            return 0;
        } else {
            depth++;
            
            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;
        }
    }

    结果 ./a.out 8 

    Push 1
    stack = top{1}base
    Push 2
    stack = top{2->1}base
    Push 3
    stack = top{3->2->1}base
    Push 4
    stack = top{4->3->2->1}base
    Push 5
    stack = top{5->4->3->2->1}base
    Push 6
    stack = top{6->5->4->3->2->1}base
    Push 7
    stack = top{7->6->5->4->3->2->1}base
    Push 8
    stack = top{8->7->6->5->4->3->2->1}base
    ----- reach the max depth of the stack!
    Pop 8
    stack = top{7->6->5->4->3->2->1}base
    Pop 7
    stack = top{6->5->4->3->2->1}base
    Pop 6
    stack = top{5->4->3->2->1}base
    Pop 5
    stack = top{4->3->2->1}base
    Pop 4
    stack = top{3->2->1}base
    Pop 3
    stack = top{2->1}base
    Pop 2
    stack = top{1}base
    Pop 1
    stack = top{}base
  • 相关阅读:
    js delete 操作符
    js中contains()方法的了解
    绝对地址、相对地址、/、./、../之间的区别
    关于应用版本号的更迭
    js事件的绑定与移除
    js实现阶乘
    圆角和渐变
    css3用到知识点小结
    font-size:0的妙用,用于解决inline或者inline-block造成的间隙
    js怎么把数字转化为字母(A,B.....AA,AB,..)
  • 原文地址:https://www.cnblogs.com/vsyf/p/4915726.html
Copyright © 2011-2022 走看看