栈
栈与队列
2021/8/3 13:17
栈 先进后出(FILO)
├┤
├┤
├┤
├┤
├┤
└┘
定义
int a[100];
int top = 0;
遍历
//按数组输出
void print() {
for(int i = 0; i < top; i++) {
cout << a[i] << ' ';
}
}
压入元素
//压入栈顶
void push(int x) {
a[top++] = x;
}
弹出栈顶
//纯粹弹出
void pop() {
a[top--] = 0;
}
//带访问的取出
int pop() {
return a[top--];
}
应用
洛谷T168940括号配对
合法的括号串:在任何位置的前面的右括号数量小于左括号的量,并且在括号传末尾左右括号数量相同.例:((()()))
队列 先进先出(FIFO)
队尾
┬┬┬┬┬┬
┴┴┴┴┴┴
定义
int q[100];
int head = 0, tail = 0;
遍历
for(int i = head; i < tail; i++) {
cout << q[i] << ' ';
}
压入
q[tail++] = x;
弹出
head++;