zoukankan      html  css  js  c++  java
  • #import "NodeStack.h"
    
    typedef struct StackNode
    {
        int value;
        StackNode *next;
    }StackNode;
    
    
    typedef struct KStack{
        StackNode *top;
        int count;
    }KStack;
    
    
    @implementation NodeStack
    
    KStack* initStack()
    {
        KStack *stack = (KStack *)malloc(sizeof(KStack));
        stack->top = NULL;
        stack->count = 0;
        return stack;
    }
    
    void push(KStack *stack,int value)
    {
        StackNode *node = (StackNode *)malloc(sizeof(StackNode));
        node->value = value;
        node->next = stack->top;
        stack->top = node;
        stack->count++;
        printf("进栈元素%d
    ",value);
    }
    
    void pop(KStack *stack)
    {
        if (stack->top == NULL) {
            printf("stack empty
    ");
            return;
        }
        
        StackNode *node = stack->top;
        stack->top = node->next;
        stack->count--;
        printf("出栈元素%d
    ",node->value);
        
        
    }
    
    void traverseStack(KStack *stack)
    {
        while (stack->top) {
            StackNode *node = stack->top;
            printf("遍历元素%d
    ",node->value);
            stack->top = node->next;
        }
    }
    
    bool isEmpty(KStack *stack)
    {
        if (stack->top==NULL) {
            return true;
        }
        return false;
    }
    
    int stackCount(KStack *stack)
    {
        return stack->count;
    }
    
    
    - (void)test
    {
        KStack *stack = initStack();
        int count = stackCount(stack);
        printf("count:%d
    ",count);
        pop(stack);
        push(stack, 1);
        push(stack, 2);
        count = stackCount(stack);
        printf("count:%d
    ",count);
        push(stack, 6);
        count = stackCount(stack);
        printf("count:%d
    ",count);
        pop(stack);
        traverseStack(stack);
    }
    @end
    
    
  • 相关阅读:
    [BZOJ2431] [HAOI2009]逆序对数列
    [Luogu2323] [HNOI2006]公路修建问题
    [Luogu2455] [SDOI2006]线性方程组
    [BZOJ3550] [Sdoi2014]数数
    [Noip2017] 列队
    [Luogu2824] [HEOI2016/TJOI2016]排序
    [BZOJ1060] [ZJOI2007]时态同步
    P1036 选数 题解
    快速幂取模算法详解
    同余定理及其应用
  • 原文地址:https://www.cnblogs.com/guligei/p/9473501.html
Copyright © 2011-2022 走看看