以下是完整代码:
/* * this file is an implementation of stack with linked list * file name: stack.c * author: John Woods * date: 2015/5/9 * statement: anyone can use this file for any purpose */#include#include #define BOOL int#define TRUE 1#define FALSE 0typedef struct SNode { int value; struct SNode * next;}* SNode;typedef struct Stack { int depth; SNode top;}* Stack;/* operation declaration */void stackInit(Stack * p_myStack);void pop(Stack myStack);void push(Stack myStack);void stackClear(Stack myStack);void stackDestroy(Stack * p_myStack);BOOL isExist(Stack myStack);/* menu */void menu();/* entrance: main function */int main(void) { Stack myStack = NULL; int choice; char c; while(TRUE) { menu(); while(!scanf("%d", &choice)) while('\n' != (c=getchar()) && EOF != c); switch(choice) { case 1: stackInit(&myStack); break; case 2: stackClear(myStack); break; case 3: push(myStack); break; case 4: pop(myStack); break; case 5: stackDestroy(&myStack); break; default: exit(EXIT_SUCCESS); break; } } return 0;}/* menu implementation */void menu() { printf("\n\t****************MENU****************\n"); printf("\t* 1.initial stack 2.clear stack *\n"); printf("\t* 3.push element 4.pop element *\n"); printf("\t* 5.destroy stack 6.exit *\n"); printf("\t****************MENU****************\n"); printf("Your choice:");}/* operation implementation */void stackInit(Stack * p_myStack) { if(isExist(*p_myStack)) { printf("This stack is already exist, cannot initial it!\n"); return; } if(NULL == (*p_myStack = (Stack)malloc(sizeof(struct Stack)))) { printf("Out of memory!\n"); return; } (*p_myStack)->top = NULL; (*p_myStack)->depth = 0; printf("Initial successfully!\n");}void pop(Stack myStack) { SNode pNode = NULL; int out; char c; if(!isExist(myStack)) { printf("This stack is not exist! Please initial it first!\n"); return; } if(0 == myStack->depth) { printf("This stack is empty! Cannot pop the top value!\n"); return; } while(TRUE) { out = myStack->top->value; pNode = myStack->top; myStack->top = myStack->top->next; myStack->depth--; free(pNode); pNode = NULL; printf("The value has been popped is %d\n", out); if(0 == myStack->depth) { printf("This stack is empty now, cannot continue popping!\n "); break; } while('\n' != (c=getchar()) && EOF != c); printf("Go on?(y/n):"); if('y' != (c=getchar()) && 'Y' != c) break; }}void push(Stack myStack) { SNode pNode = NULL; int value; char c; if(!isExist(myStack)) { printf("This stack is not exist! Please initial it first!\n"); return; } while(TRUE) { if(!(pNode = (SNode)malloc(sizeof(struct SNode)))) { printf("Out of memory!\n"); return; } printf("Please input the value:"); while(!scanf("%d", &value)) while('\n' != (c=getchar()) && EOF != c); pNode->value = value; pNode->next = myStack->top; myStack->top = pNode; myStack->depth++; pNode = NULL; printf("Push successfully!\n"); while('\n' != (c=getchar()) && EOF != c); printf("Go on?(y/n):"); if('y' != (c=getchar()) && 'Y' != c) break; }}void stackClear(Stack myStack) { if(!isExist(myStack)) { printf("This stack is not exist! Please initial it first!\n"); return; } SNode pNode = NULL; while(myStack->top) { pNode = myStack->top; myStack->top = myStack->top->next; free(pNode); } myStack->top = NULL; myStack->depth = 0; printf("Clear successfully!\n");}void stackDestroy(Stack * p_myStack) { if(!isExist(*p_myStack)) { printf("This stack is not exist! Please initial it first!\n"); return; } stackClear(*p_myStack); free(*p_myStack); *p_myStack = NULL; printf("Destroy successfully!\n");}BOOL isExist(Stack myStack) { if(NULL == myStack) return FALSE; else return TRUE;}
这里想说的是typedef,typedef会“封装”出一个新的数据类型,与#define有很大的不同
/* typedef */typedef struct SName{ int data;} * SName;/* 若定义如下变量 */SName temp = (SName)malloc(sizeof(struct SName));/* 虽然 temp 实际上是个指针,但若当做参数被其它函数调用,其值仍是不会被修改的,在stack.c的stackInit()和stackDestroy()中传递参数要注意 */