序言
用链表实现的栈结构,遵循先进后出
栈的结构图
栈的结构
typedef struct STACK_LIST {
Node *pFront;
Node *pTail;
} *PSTACK_LIST, STACK_LIST;
栈的基本操作
void init_stack_list(PSTACK_LIST *pstack_list);
void push_stack_list(PSTACK_LIST pstack_list, ELEMENT *data);
void pop_stack_list(PSTACK_LIST pstack_list, ELEMENT *data);
bool is_empty_stack_list(PSTACK_LIST pstack_list);
void clear_stack_list(PSTACK_LIST pstack_list);
void destroy_stack_list(PSTACK_LIST *pstack_list);
void print_stack_list(PSTACK_LIST pstack_list);
栈的实现
void init_stack_list(PSTACK_LIST *pstack_list) {
*pstack_list = malloc(sizeof(pstack_list));
(*pstack_list)->pFront = NULL;
(*pstack_list)->pTail = NULL;
};
void push_stack_list(PSTACK_LIST pstack_list, ELEMENT *data) {
Node *pInsertNode = malloc(sizeof(Node));
pInsertNode->data = *data;
pInsertNode->next = pstack_list->pFront;
pstack_list->pFront = pInsertNode;
};
void pop_stack_list(PSTACK_LIST pstack_list, ELEMENT *data) {
PNode delNode = pstack_list->pFront;
pstack_list->pFront = pstack_list->pFront->next;
*data = delNode->data;
free(delNode);
};
bool is_empty_stack_list(PSTACK_LIST pstack_list) {
if (pstack_list->pFront == NULL) {
return true;
}
return false;
};
void clear_stack_list(PSTACK_LIST pstack_list) {
while (!is_empty_stack_list(pstack_list)) {
int a = 0;
pop_stack_list(pstack_list, &a);
}
}
void destroy_stack_list(PSTACK_LIST *pstack_list) {
clear_stack_list(*pstack_list);
free(*pstack_list);
printf("stack list has been destroyed\n");
}
void print_stack_list(PSTACK_LIST pstack_list) {
while (!is_empty_stack_list(pstack_list)) {
int a = 0;
pop_stack_list(pstack_list, &a);
printf("%d\n", a);
}
};
github:https://github.com/HumorSmith/DataStructure/tree/master/stack