Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderZsq committed Jan 24, 2019
1 parent 7c725d8 commit f53108d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
149807CC21F83D02000F6532 /* ArrayList.c in Sources */ = {isa = PBXBuildFile; fileRef = 149807CB21F83D02000F6532 /* ArrayList.c */; };
14ABAC2521F9675600EB7005 /* Object.m in Sources */ = {isa = PBXBuildFile; fileRef = 14ABAC2421F9675600EB7005 /* Object.m */; };
14C4608F21F989E800415433 /* LinkList.c in Sources */ = {isa = PBXBuildFile; fileRef = 14C4608E21F989E800415433 /* LinkList.c */; };
14F8639621F9A8B800528C8C /* Stack.c in Sources */ = {isa = PBXBuildFile; fileRef = 14F8639521F9A8B800528C8C /* Stack.c */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -37,6 +38,8 @@
14ABAC2421F9675600EB7005 /* Object.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Object.m; sourceTree = "<group>"; };
14C4608D21F989E800415433 /* LinkList.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LinkList.h; sourceTree = "<group>"; };
14C4608E21F989E800415433 /* LinkList.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = LinkList.c; sourceTree = "<group>"; };
14F8639421F9A8B800528C8C /* Stack.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Stack.h; sourceTree = "<group>"; };
14F8639521F9A8B800528C8C /* Stack.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = Stack.c; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -69,6 +72,8 @@
149807C221F83BAB000F6532 /* Algorithm4Objective-C */ = {
isa = PBXGroup;
children = (
14F8639421F9A8B800528C8C /* Stack.h */,
14F8639521F9A8B800528C8C /* Stack.c */,
14C4608D21F989E800415433 /* LinkList.h */,
14C4608E21F989E800415433 /* LinkList.c */,
149807CA21F83D02000F6532 /* ArrayList.h */,
Expand Down Expand Up @@ -138,6 +143,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
14F8639621F9A8B800528C8C /* Stack.c in Sources */,
14ABAC2521F9675600EB7005 /* Object.m in Sources */,
148EA2C121F9820200A9F524 /* NSArray+Address.m in Sources */,
149807C421F83BAB000F6532 /* main.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Stack.c
// Algorithm4Objective-C
//
// Created by 朱双泉 on 2019/1/24.
// Copyright © 2019 Castie!. All rights reserved.
//

#include "Stack.h"
#include "ArrayList.h"

void Stack_Print(Stack *stack, StackPrintFunc func)
{
ArrayList_Print(stack, func);
}

Stack *Stack_Create(int capaticy)
{
return ArrayList_Create(capaticy);
}

void Stack_Destroy(Stack *stack)
{
ArrayList_Destory(stack);
}

void Stack_Clear(Stack *stack)
{
ArrayList_Clear(stack);
}

void Stack_Push(Stack *stack, StackValue value)
{
ArrayList_Add(stack, value);
}

StackValue Stack_Pop(Stack *stack)
{
return ArrayList_Remove(stack, ArrayList_Length(stack) - 1);
}

StackValue Stack_Top(Stack *stack)
{
return ArrayList_Get(stack, ArrayList_Length(stack) - 1);
}

int Stack_Length(Stack *stack)
{
return ArrayList_Length(stack);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Stack.h
// Algorithm4Objective-C
//
// Created by 朱双泉 on 2019/1/24.
// Copyright © 2019 Castie!. All rights reserved.
//

#ifndef Stack_h
#define Stack_h

typedef void * StackValue;
typedef void Stack;
typedef void (*StackPrintFunc)(StackValue value);

#include <stdio.h>

void Stack_Print(Stack *stack, StackPrintFunc func);

Stack *Stack_Create(int capaticy);
void Stack_Destroy(Stack *stack);
void Stack_Clear(Stack *stack);

void Stack_Push(Stack *stack, StackValue value);
StackValue Stack_Pop(Stack *stack);
StackValue Stack_Top(Stack *stack);
int Stack_Length(Stack *stack);

#endif /* Stack_h */
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@
#import "Object.h"
#import "NSArray+Address.h"
#import "LinkList.h"
#import "Stack.h"

void testStack() {
Stack *stack = Stack_Create(10);
Stack_Print(stack, NULL); // []
Stack_Push(stack, (void *)10);
Stack_Push(stack, (void *)2);
Stack_Push(stack, (void *)4);
Stack_Push(stack, (void *)5);
Stack_Print(stack, NULL); // [10, 2, 4, 5]
printf("%d\n", (int)Stack_Pop(stack));
printf("%d\n", (int)Stack_Pop(stack));
Stack_Print(stack, NULL); // [10, 2]
printf("%d\n", (int)Stack_Top(stack)); // 2
Stack_Pop(stack);
Stack_Pop(stack);
Stack_Pop(stack);
Stack_Pop(stack);
Stack_Print(stack, NULL); // []
Stack_Push(stack, (void *)4);
Stack_Clear(stack);
Stack_Print(stack, NULL); // []
Stack_Destroy(stack);
}

void printIntFunc(LinkListNodeValue value) {
printf("%d", (int)value);
Expand Down Expand Up @@ -147,7 +171,7 @@ void testArray() {

int main(int argc, const char * argv[]) {
@autoreleasepool {

testStack();
}
return 0;
}

0 comments on commit f53108d

Please sign in to comment.