LinkStack.h头文件 #ifndef LINKSTACK_H_H #define OK 0 #define ERROR -1 typedef int type; typedef struct LNode { type data; struct LNode *next; }*LinkStack; int init_linkStack(LinkStack*); int make_node(LNode**); int destroy_linkStack(LinkStack*); int clear_linkStack(LinkStack); int create_linkStack(LinkStack); int print_linkStack(LinkStack); int stack_empty(LinkStack); int stack_length(LinkStack); int get_top(LinkStack, type*); int push(LinkStack, type); int pop(LinkStack, type*); #endif LinkStack.cpp文件 #include<stdio.h> #include<stdlib.h> #include "LinkStack.h" int init_linkStack(LinkStack *lstack) { if(!((*lstack) = (LinkStack)malloc(sizeof(LNode)))) return ERROR; (*lstack)->next = NULL; (*lstack)->data = 0; return OK; } int make_node(LNode **node) { if(!((*node) = (LNode*)malloc(sizeof(LNode)))) return ERROR; return OK; } int destroy_linkStack(LinkStack *lstack) { if((*lstack) == NULL) return ERROR; LNode *tmp; while((*lstack) != NULL) { tmp = *lstack; (*lstack) = (*lstack)->next; free(tmp); } (*lstack) = NULL; return OK; } int clear_linkStack(LinkStack lstack) { if(lstack->next == NULL) return ERROR; LNode *tmp = lstack->next; LNode *tmp1; while(tmp->next != NULL) { tmp1 = tmp; tmp = tmp->next; free(tmp1); } lstack->next = NULL; return OK; } int create_linkStack(LinkStack lstack) { if(lstack == NULL) return ERROR; type num; LNode *node; printf("input num: "); scanf("%d",&num); while(num != 0) { if(ERROR == make_node(&node)) return ERROR; node->data = num; node->next = lstack->next; lstack->next = node; lstack->data++; scanf("%d",&num); } return OK; } int print_linkStack(LinkStack lstack) { if(lstack == NULL || lstack->next == NULL) return ERROR; LNode *tmp = lstack->next; while(tmp != NULL) { printf("%d ",tmp->data); tmp = tmp->next; } printf("/n"); return OK; } int stack_empty(LinkStack lstack) { if(lstack == NULL) return ERROR; if(0 == lstack->data) return true; return false; } int stack_length(LinkStack lstack) { if(lstack == NULL) return ERROR; return lstack->data; } int get_top(LinkStack lstack, type *e) { if(lstack == NULL || lstack->next == NULL) return ERROR; *e = lstack->next->data; return OK; } int push(LinkStack lstack, type e) { if(lstack == NULL) return ERROR; LNode *node; if(ERROR == make_node(&node)) return ERROR; node->data = e; node->next = lstack->next; lstack->next = node; lstack->data++; return OK; } int pop(LinkStack lstack, type *e) { if(lstack == NULL || lstack->next == NULL) return ERROR; LNode *tmp; *e = lstack->next->data; tmp = lstack->next; lstack->next = tmp->next; free(tmp); lstack->data--; return OK; }