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
    
    
  • 相关阅读:
    Pythonday01
    PYTHON_DAY2
    PYTHON_DAY3
    数据字典生成SQL语句
    Spring cloud Netflix >readMe
    SecureCRT的安装与激活
    MyBatis映射文件UserMapper.xml(mysql环境)
    数据库模糊查询4种用法
    MyBatis配置文件myBatisconfig.xml
    计算机基础:2进制和2进制算法。
  • 原文地址:https://www.cnblogs.com/guligei/p/9473501.html
Copyright © 2011-2022 走看看