#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\n",value);
}

void pop(KStack *stack)
{
    if (stack->top == NULL) {
        printf("stack empty\n");
        return;
    }
    
    StackNode *node = stack->top;
    stack->top = node->next;
    stack->count--;
    printf("出栈元素%d\n",node->value);
    
    
}

void traverseStack(KStack *stack)
{
    while (stack->top) {
        StackNode *node = stack->top;
        printf("遍历元素%d\n",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\n",count);
    pop(stack);
    push(stack, 1);
    push(stack, 2);
    count = stackCount(stack);
    printf("count:%d\n",count);
    push(stack, 6);
    count = stackCount(stack);
    printf("count:%d\n",count);
    pop(stack);
    traverseStack(stack);
}
@end

posted @ 2018-08-14 11:20  路在脚下,  阅读(76)  评论(0编辑  收藏  举报