手写C版本的stack
记录一个小demo,留着以后批判。
实现了除了运算符重载以外的所有功能(大概),deque扩容有问题,push和pop设计导致的,可以通过修改宏SIZE的大小来避免扩容,其他的都没什么问题。stack使用的是extern const struct stackFuntion stack; **类似于C++的namespace**中的通用接口。
用到的宏
1 2 3
| #define TYPE int #define SIZE 51200 #define INT_MIN -2147483648
|
data定义
1 2 3 4 5 6 7 8 9
| typedef struct deque{ struct deque *prev; struct deque *next; unsigned int nowdeque; unsigned int top; unsigned int size; unsigned int empty; TYPE data[0]; }deque;
|
funtion定义
1 2 3 4 5 6 7 8 9
| typedef struct stackFuntion{ deque* (*initStackData)(void); TYPE (*top)(deque* this); unsigned int (*empty)(deque* this); unsigned int (*size)(deque* this); void (*push)(deque** this,TYPE value); TYPE (*pop)(deque** this); void (*swap)(deque** stackData1,deque** stackData2); }stackFuntion;
|
stack初始化
1 2 3
| const stackFuntion stack = { initStackData,tops,emptys,sizes,push,pop,swaps };
|
测试函数
1 2 3 4 5 6 7 8 9 10 11 12
| void StackTest(){ deque* p = stack.initStackData(); for(int i = 0; i < 50; i++){ stack.push(&p,i); printf("i = %d\tstack.top() = %d\n",i,stack.top(p)); } TYPE a = stack.pop(&p); printf("a = %d\n",a); printf("size = %d\n",stack.size(p)); printf("empty = %d\n",stack.empty(p)); }
|
完整代码
Stack.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include "stack.h" #include "stdlib.h" #include "stdio.h"
deque* initStackData(){ deque* this = malloc(sizeof(deque)+sizeof(TYPE)*SIZE); this->size = 0; this->top = 0; this->empty = 1; this->nowdeque = 1; this->next = NULL; this->prev = NULL; return this; }
TYPE tops(deque* this){ if(this->empty == 0)return INT_MIN; else return this->data[this->top - 1]; }
unsigned int emptys(deque* this){ return this->empty; }
unsigned int sizes(deque* this){ return this->size+(this->nowdeque-1)*SIZE; }
void push(deque** this,TYPE value){ if((*this)->size == SIZE){ deque* tmp = (deque*)malloc(sizeof(deque)+ sizeof(TYPE)*SIZE); tmp->top = 0; (*this)->next = tmp; tmp->prev = (*this); tmp->nowdeque = (*this)->nowdeque+1; tmp->size = 0; tmp->data[tmp->size++] = value; tmp->empty = 0; *this = tmp; }else{ (*this)->top++; (*this)->data[(*this)->size++] = value; } }
TYPE pop(deque** this){ if((*this)->nowdeque != 1 && (*this)->size == 1){ deque* tmp = (*this)->prev; tmp->nowdeque = (*this)->nowdeque+1; tmp->size = 0; tmp->next = NULL; free(*this); (*this) = tmp; tmp = NULL; } if((*this)->size != 0){ (*this)->empty = (*this)->size > 0 ? 0 : 1; return (*this)->data[--(*this)->size]; } else{ printf("pop error,stack is empty\n"); return INT_MIN; } }
void swaps(deque** stackData1,deque** stackData2){ deque* tmp = (*stackData1); stackData1 = stackData2; stackData2 = &tmp; }
const stackFuntion stack = { initStackData,tops,emptys,sizes,push,pop,swaps };
|
stack.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#ifndef stack_h #define stack_h
#include <stdio.h> #include "stdlib.h" #include "stdio.h" #define TYPE int #define SIZE 51200 #define INT_MIN -2147483648 typedef struct deque{ struct deque *prev; struct deque *next; unsigned int nowdeque; unsigned int top; unsigned int size; unsigned int empty; TYPE data[0]; }deque;
typedef struct stackFuntion{ deque* (*initStackData)(void); TYPE (*top)(deque* this); unsigned int (*empty)(deque* this); unsigned int (*size)(deque* this); void (*push)(deque** this,TYPE value); TYPE (*pop)(deque** this); void (*swap)(deque** stackData1,deque** stackData2); }stackFuntion;
#endif
|
Main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <stdlib.h> #include <stdio.h> #include <string.h> #include "stack.h"
extern const struct stackFuntion stack;
void StackTest(){ deque* p = stack.initStackData(); for(int i = 0; i < 50; i++){ stack.push(&p,i); printf("i = %d\tstack.top() = %d\n",i,stack.top(p)); } TYPE a = stack.pop(&p); printf("a = %d\n",a); printf("size = %d\n",stack.size(p)); printf("empty = %d\n",stack.empty(p)); }
int main(){ StackTest(); return 0; }
|