struct mylist{
int key; /*可根据实际情况修改类型*/
struct mylist *next;
};
typedef struct {
int size;
struct mylist head;
} MyStack;
MyStack* StackCreate() {
MyStack *obj = NULL;
obj = (MyStack *)malloc(sizeof(MyStack));
if (!obj) {
return NULL;
}
memset(obj,0,sizeof(MyStack));
obj->head.next = NULL;
obj->size = 0;
return obj;
}
void StackPush(MyStack* obj, int val) {
struct mylist *list = NULL;
list = (struct mylist *)malloc(sizeof(struct mylist));
if (list == NULL || obj == NULL){
return;
}
list->key = val;
list->next = obj->head.next;
obj->head.next = list;
obj->size++;
}
void StackPop(MyStack* obj) {
struct mylist *list, *p;
if(!obj || obj->size == 0) {
return;
}
obj->size--;
list = obj->head.next;
obj->head.next = list->next;
free(list);
}
int StackTop(MyStack* obj) {
if (obj == NULL || obj->head.next == NULL) {
return -1;
}
return obj->head.next->key;
}
bool StackEmpty(MyStack* obj) {
if (obj == NULL) {
return true;
}
return obj->size > 0 ? false:true;
}
void StackFree(MyStack* obj) {
struct mylist *p,*q;
if (!obj) {
return;
}
for(p = obj->head.next; p != NULL; ) {
q = p;
p=p->next;
free(q);
}
}