IDE:VS2005 stack.h /********************************* * * * *Data: 08-27-2009 *Path: #include "Stack.h" * ***********************************/ #ifndef STACK_H #define STACK_H typedef char DataType; typedef struct node { DataType data; struct node *next; }StackNode; typedef StackNode *LinkStack; // 创建栈 LinkStack CreateStack(); // 输出栈 int PrintStack(const StackNode *top); // 进栈 int PushStack(LinkStack &top, const DataType data); // 退栈 int PopStack(LinkStack &top); // 栈是否为空 //int StackEmpty(const StackNode *top); // 释放栈 int ReleaseStack(LinkStack &top); #endif stack.cpp #include "Stack.h" #include <cstdio> #include <cstdlib> #include <cassert> LinkStack CreateStack() { char ch=' '; LinkStack top = NULL; while((ch=getchar())!='/n') { PushStack(top, ch); } return top; } int PrintStack(const StackNode *top) { if (NULL != top) { const StackNode *temp = top; do { printf("%c ", temp->data); temp = temp->next; }while(NULL != temp); } printf("/n"); return 1; } int PushStack(LinkStack &top, const DataType data) { StackNode *node = (LinkStack)malloc(sizeof(StackNode)); if (NULL == node) { return 0; } node->data = data; node->next = top; top = node; return 1; } int PopStack(LinkStack &top) { if (NULL == top) { return 0; } LinkStack temp = top->next; free(top); top = temp; return 1; } int GetStackTop(const StackNode *top, DataType &data) { if (NULL == top) { return 0; } data = top->data; return 1; } int ReleaseStack(LinkStack &top) { if (NULL == top) { return 1; } LinkStack temp; do { temp = top; top = top->next; free(temp); } while (NULL !=top); return 1; } Main.cpp #include "Stack.h" void main(void) { // 创建链表 LinkStack stack = CreateStack(); PrintStack(stack); PushStack(stack, 'h'); PrintStack(stack); PopStack(stack); PrintStack(stack); ReleaseStack(stack); }