1.数据类型:栈
2.存储方式:链式存储
3.常用名称:链栈
#includeusing namespace std; #define MAX_SIZE 100 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; // Status是函数的类型,其值是函数结果状态代码 typedef int SElemType; // 根据自己的数据类型进行定义 // 链栈结构体 typedef struct StackNode { SElemType data; struct StackNode* next; }StackNode, *LinkStack; // 初始化 Status InitStack(LinkStack& S) { S = NULL; // 构造一个空栈,栈顶指针置为空 return OK; } // 是否为空 Status StackEmpty(LinkStack S) { if (S == NULL) return TRUE; else return FALSE; } // 链栈的入栈 Status Push(LinkStack& S, SElemType e) { StackNode* p = new StackNode; // 生成新的结点p p->data = e; // 将数据加载到p的数据域 p->next = S; // 将新结点插入栈顶; 因为S中存放的地址即下一结点的地址 S = p; // 修改栈顶指针 return OK; } // 链栈的出栈 Status Pop(LinkStack& S, SElemType& e) { if (S == NULL) return ERROR; e = S->data; LinkStack p = S; // 将S的指针指向放入临时变量p中,否则链表丢失 S = S->next; // 将S指针指向下一个结点 delete p; // 释放p return OK; } // 取栈顶元素 SElemType GetTop(LinkStack S) { if (S != NULL) return S->data; } int main() { LinkStack S; InitStack(S); Push(S, 10); Push(S, 30); Push(S, 50); Push(S, 60); int e; while (S!=NULL) { Pop(S, e); cout << e << endl; } return 0; }



