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
    
    
  • 相关阅读:
    4、idea使用git
    ♫【MongoDB】
    -_-#【网站优化】预加载(Pre-loader) / 预读取(Pre-fetching)
    【Node】package.json
    ☀【移动】Google Maps JavaScript API v3
    -_-#【移动】视频分段
    ☀【Grunt】package.json, Gruntfile.js, npm install, grunt
    【three.js】
    【单元测试】
    洛谷——P1125 笨小猴
  • 原文地址:https://www.cnblogs.com/guligei/p/9473501.html
Copyright © 2011-2022 走看看