文件结构:
Stack.h 公用头
Stack_Static_Sequence.c
Stack_Dynamic_Sequence.c
Stack_Chain.c
Bridging-Header.h(与swift桥接文件)
Stack.h
______________________________________
#ifndef Stack_h
#define Stack_h
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
/* 1 - Static Sequence */
#define maxsize 100
//#define GameObject int
typedef int GameObject;
bool isEmpty();
bool isFull();
bool push(GameObject item);
GameObject pop();
void printAll();
void test();
/* 2 - Dynamic Sequence */
typedef struct{
GameObject data[maxsize];
int top;
} Stack;
Stack* initStack(size_t size);
void destroyStack(Stack* s);
bool isEmptyDS(Stack* s);
bool isFullDS(Stack* s);
bool pushDS(Stack* s, GameObject item);
GameObject popDS(Stack* s);
void printAllDS(Stack* s);
int getLengthDS(Stack* s);
/* 3 - Chain */
bool pushC(GameObject item);
void popC();
bool isEmptyC();
GameObject getTopItem();
void destroyStackC();
void printAllC();
#endif /* Stack_h */
Stack_Static_Sequence.c
____________________________________
#include "Stack.h"
static int top = -1;
static GameObject data[maxsize];
bool isEmpty(){
if(top == -1){
return true;
} else {
return false;
}
}
bool isFull(){
if (top == maxsize - 1){
return true;
} else {
return false;
}
}
bool push(GameObject item){
if (isFull()) { return false; }
data[++top] = item;
return true;
}
GameObject pop(){
if (isEmpty()) { return false; }
return data[top--];
}
void printAll(){
printf("堆栈元素为,从栈顶开始:\n");
int i = top ;
while(i!=-1){
printf("%d\n", data[i]);
i--;
}
}
void changeSize(size){
}
void test(){
push(1);
push(4);
push(6);
push(8);
printAll();
pop();
pop();
printAll();
}
Stack_Dynammic_Sequence.c
____________________________
#include "Stack.h"
int stackSize;
Stack* initStack(size_t size){
Stack* s = malloc(sizeof(Stack));
if (!s) {
printf("not enough space");
} else {
stackSize = size;
s->top = -1;
}
return s;
}
void destroyStack(Stack* s){
free(s);
}
bool isEmptyDS(Stack* s){
if (s->top == -1){
return true;
} else {
return false;
}
}
bool isFullDS(Stack* s){
if (s->top == stackSize - 1){
return true;
} else {
return false;
}
}
bool pushDS(Stack* s, GameObject item){
if (isFullDS(s)) {
return false;
}
s->data[++s->top] = item;
return true;
}
GameObject popDS(Stack* s){
if (isEmptyDS(s)){
return NULL;
}
return s->data[s->top--];
}
void printAllDS(Stack* s){
printf("堆栈元素为,从栈顶开始:\n");
int i = s->top;
while(i!=-1){
printf("%d\n", s->data[i]);
i--;
}
}
int getLengthDS(Stack* s){
return s->top + 1;
}
Stack_Chain.c
_______________________________
#include "Stack.h"
typedef struct StackNode{
GameObject value;
struct StackNode* next;
}Node;
Node* topNode;
bool pushC(GameObject item){
Node* newNode = malloc(sizeof(Node));
if (!newNode) {
printf("not enough space\n");
return false;
}
newNode->value = item;
newNode->next = topNode;
topNode = newNode;
return true;
}
void popC(){
Node* firstNode = topNode;
topNode = firstNode->next;
free(firstNode);
}
bool isEmptyC(){
if (topNode == NULL) {
return true;
} else {
return false;
}
}
GameObject getTopItem(){
if (isEmptyC()){
return NULL;
}
return topNode;
}
void destroyStackC(){
free(topNode);
}
void printAllC(){
printf("堆栈元素为,从栈顶开始:\n");
Node* p = topNode;
while(p!=NULL){
printf("%d\n",p->value);
p = p->next;
}
}