数据结构 栈and队列
栈
基本特点
- 后进先出(Last In First Out)
- 只在栈顶进行插入和删除等操作
栈的基本数据结构(顺序栈)
struct stack { int *base;//尾指针,指向栈底 int *top;//头指针,一般指向栈顶上一个元素 int stacksize;//栈的最大容量 };
制作栈
void InitStack(struct Stack &S,int maxsize) { S.base=new int[maxsize];//给栈开辟空间 S.top=S.base;//置空栈 S.stacksize=maxsize; }
栈的基本操作(数据结构版)
主函数
SStack S; InitStack(S,100);
入栈
//入栈 void Push (struct Stack &S,int n) { if(S.top-S.base==S.stacksize) { cout<<"NO"<<endl; return; } *S.top=n;//top是指针,赋值时候需要加上* S.top++;//此时是指针操作,不需要加* }
出栈
//出栈 int Pop(struct Stack &S) { if(S.top==S.base) { cout<<"NO"<<endl; return -1; } S.top--;//top指针指向实际顶部元素 return *S.top; }
查询栈顶元素
int Search(struct stack &S) { if(S.top==S.base) { return -1; } int e; S.top--; e=*S.top++; return e; }
销毁
void delete_stack(struct stack &S,int maxsize) { free(S.base); S.stacksize=0; S.base=S.top=NULL; }
显示栈内所有元素
void showStack (struct Stack &S) { int a =S.top-S.base; int i; for (i=0;i<a;i++) { cout<<S.base<<" "; S.base++; } cout<<endl; }
栈的基本操作(c++应用版)
用题目匹配括号演示
#include <bits/stdc++.h> using namespace std; int main() { stack<char> st;//制作栈 string a; getline(cin,a);//题目要求可以输入空格 for(int i=0; i<a.length(); i++) { if(a[i]=='('||a[i]=='['||a[i]=='{') { st.push(a[i]);//入栈 } if(a[i]=='}'||a[i]==']'||a[i]==')') { if(!st.empty())//如果栈不为空 { char t=st.top(); //访问顶部元素 if(a[i]=='}'&&t!='{') { cout<<"no"<<endl; return 0; } if(a[i]==']'&&t!='[') { cout<<"no"<<endl; return 0; } if(a[i]==')'&&t!='(') { cout<<"no"<<endl; return 0; } st.pop();//出栈 } else { cout<<"no"<<endl; return 0; } } } if(!st.empty()) { cout<<"no"<<endl; } else { cout<<"yes"<<endl; } return 0; }
技巧题目:7-5 出栈序列的合法性
技巧:不可能出现:大小中
#include<bits/stdc++.h> using namespace std; int main() { int m,n,k; cin>>m>>n>>k; int a[k][n]; for(int i=0; i<k; i++) { for(int j=0; j<n; j++) { cin>>a[i][j]; } } int s[k]; for(int i=0; i<k; i++) { stack<int> st; int index=0; for(int j=1; j<=n; j++) { if(st.size()<m) { st.push(j); } else break; while(!st.empty()) { if(st.top()!=a[i][index]) { break; } else { st.pop(); index++; continue; } } } if(st.empty()) { s[i]=1; } else { s[i]=0; } } for(int i=0; i<k; i++) { if(s[i])cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
练习> SUDTOJ 题目链接
7-3 后缀式求值
我们人类习惯于书写“中缀式”,如 3 + 5 * 2 ,其值为13。 (p.s. 为什么人类习惯中缀式呢?是因为中缀式比后缀式好用么?)
而计算机更加习惯“后缀式”(也叫“逆波兰式”,Reverse Polish Notation)。上述中缀式对应的后缀式是: 3 5 2 * +
现在,请对输入的后缀式进行求值。
输入格式:
在一行中输入一个后缀式,运算数和运算符之间用空格分隔,运算数长度不超过6位,运算符仅有+ - * / 四种。
输出格式:
在一行中输出后缀式的值,保留一位小数。
输入样例:
3 5.4 2.2 * +
输出样例:
14.9
#include<bits/stdc++.h> using namespace std; int main() { string str; stack<double> st; while(cin>>str){ if (str == "+" || str == "-" || str == "*" || str == "/"){ double a,b; a=st.top(); st.pop(); b=st.top(); st.pop(); if(str=="+") st.push(a+b); else if(str=="-") st.push(b-a); else if(str=="*") st.push(a*b); else if(str=="/") st.push(b/a); } else{ st.push(stod(str)); } } cout << round(st.top() * 10) / 10.0 << endl; // 控制小数点1位 // cout << fixed << setprecision(1) << st.top();// 控制小数点1位 return 0; }
进制转换(经典题目)
队列
队列就是先进先出
7-10 队列模拟
设从键盘输入一整数序列a1,a2,...an,试编程实现:当ai>0 时,ai 进队,当ai<0 时,将队首元素出队,当ai=0 时,表示输入结束。要求将队列处理成环形队列,使用环形队列算法库中定义的数据类型及算法,程序中只包括一个函数(main 函数),入队和出队等操作直接在main 函数中调用即可。当进队出队异常(如队满)时,要打印出错信息。
输入格式:
输入一系列整数,以0结束。环形队列最多可存储10个元素。
输出格式:
输出最后队列中的元素。如果队列满,则显示“队列已满”,如果出队列时无元素可出,显示“wrong”
输入样例1:
在这里给出一组输入。例如:
1 2 3 -4 -5 6 7 -8 0
输出样例1:
在这里给出相应的输出。例如:
The Queue is:6 7
输入样例2:
在这里给出一组输入。例如:
1 2 -4 -5 -6 7 -8 0
输出样例2:
在这里给出相应的输出。例如:
wrong
输入样例3:
在这里给出一组输入。例如:
1 2 3 4 5 6 7 8 9 10 11 12 0
输出样例3:
在这里给出相应的输出。例如:
Queue is full!
The Queue is:1 2 3 4 5 6 7 8 9 10
#include<bits/stdc++.h> using namespace std; int main() { int t; queue<int> q; while(cin>>t){ if(t==0) break; if(q.size()>=10){ cout<<"Queue is full!"<<endl; break; } if(t>0){ q.push(t); } else{ if(q.empty()){ cout<<"wrong"<<endl; return 0; } else { q.pop(); } } } cout<<"The Queue is:"; while(!q.empty()){ cout<<q.front()<<" "; q.pop(); } return 0; }
本文作者:kingwzun
本文链接:https://www.cnblogs.com/kingwz/p/15187853.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步