#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