(链)栈的基本操作 C++

来源:互联网 发布:淘宝店名在哪里修改 编辑:程序博客网 时间:2024/06/16 09:08
#include "iostream"using namespace std;//=====================栈(后进先出)====================/*栈也是线性表,限定在栈顶进行插入&删除,时间复杂度均为O(1)栈:最大存储大小+数据*///=================栈的顺序存储==================const int MAXSIZE = 20;//栈的数据结构:struct Sqstack{int data[20];int top;Sqstack(int x) :top(x){};};/*栈的遍历:*/bool StackVisit(const Sqstack *S){if (S->top == -1)return false;else{for (int i = 0; i <= S->top; i++)cout << S->data[i] << " ";cout << endl;return true;}}/*进栈操作push:1.栈顶指针 S->top 先自增1,给需要进栈的元素腾出内存空间。2.再赋值。就是给对应的数组元素赋值:S->data[S->top]=e*/bool myPush(Sqstack *S, int elem){if (S->top == MAXSIZE - 1){cout << "栈已满!" << endl;return false;}else{S->top++;S->data[S->top] = elem;return true;}}/*出栈操作pop:先把要出栈的元素获取到,然后再指针自减,把空间释放出来。*/bool MyPop(Sqstack *S){if (S->top == -1){cout << "空栈!" << endl;return false;}else{S->top--;return true;}}/*获取顺序栈的栈顶元素:*/bool GetTop(Sqstack *S, int &elem){if (S->top == -1){cout << "空栈!" << endl;return false;}else{elem = S->data[S->top];return true;}}//判断是否为空栈bool StackEmpty(Sqstack *S){if (S->top == -1)return true;elsereturn false;}//置空栈bool ClearStack(Sqstack *S){S->top = -1;return true;}//两栈共享空间//=======================================//=============栈的链式存储============/*链栈也是一种单链表,栈顶指针即为单链表头指针,不需要头结点,不存在慢栈情况。空栈即top==NULL链栈:top指针+节点(数据)*/struct StackNode  //链栈节点{int val;StackNode *next;StackNode(int x) :val(x), next(NULL){};};typedef struct LinkStack //头结点{StackNode *top;int count;//记录栈中元素个数};LinkStack* InitLinkStack(){LinkStack *p = new LinkStack;p->count = 0;p->top = NULL;return p;}//链栈的进栈操作Push/*1.处理新结点s的后继,这个后继就是原本的栈顶结点。2.将栈顶指针 top 重新指向新结点s就行了。*/bool myLinkPush(LinkStack *S, int data){StackNode *p = new StackNode(data);p->next = S->top;S->top = p;S->count++;return true;}//链栈的出栈操作Pop:/*1.首先设定一个工作结点 p,并且将栈顶结点赋值给它。2.然后将栈顶指针下移一位,指向下一结点。代码表示为 S->top=S->top->next;3.最后把结点 p delete掉,count--。*/bool myLinkPop(LinkStack *S){if (S->top == NULL){cout << "空栈!" << endl;return false;}else{StackNode *p = S->top;S->top = S->top->next;delete p;return true;}}//链栈的遍历:bool VisitLinkStack(LinkStack *S){if (S->top==NULL){cout << "空栈!" << endl;return false;}else{StackNode *p = S->top;while (p != NULL){cout << p->val << " ";p = p->next;}cout << endl;return true;}}//判断链栈是否为空bool LinkStackEmpty(LinkStack *S){if (S->count == 0)return true;elsereturn false;}//链栈置空:方法是设置两个工作结点,开始循环。在释放p之前,让q成为p的后继。bool ClearLinkStack(LinkStack *S){if (S->top == NULL)return true;else{StackNode *p, *q;p = S->top;while (p){q = p;p = p->next;delete q;}S->count = 0;S->top = NULL;return true;}}//=============================================//递归:定义必须至少有一个条件,满足时递归不再进行,即不再引用自身而是返回值退出//斐波那契的递归函数int Fbi(int i){if (i < 2)return i == 0 ? 0 : 1;elsereturn Fbi(i - 1) + Fbi(i - 2);}//将中缀表达式转化为后缀表达式:/*从左到右遍历中缀表达式的每个数字和符号,若是数字就输出,即成为后缀表达式的一部分;若是符号,则判断其与栈顶符号的优先级,是右括号或优先级低于找顶符号(乘除优先加减)则栈顶元素依次出找并输出,并将当前符号进栈,一直到最终输出后缀表达式为止*/int main(){//==============栈的顺序存储===========// Sqstack *S = new Sqstack(-1);// int n = 0;// while (cin >> n)// myPush(S, n);// // MyPop(S);// StackVisit(S);//============链栈===================LinkStack *S = InitLinkStack();int n;while (cin>>n)myLinkPush(S, n);//myLinkPop(S);ClearLinkStack(S);VisitLinkStack(S);system("pause");return 0;}

原创粉丝点击