栈:
概念:
特殊的线性表
只能在固定的一段进行插入和删除(栈顶)
原则:后进先出
压栈:入数据在栈顶
出栈:出数据在栈顶
实现:顺序表,尾插+尾删
E push(E item)压栈
E pop()出栈
E peek()查看栈顶元素
Boolean empty()判断栈是否为空
int
search(Object)返回一个对象在此堆栈上的基于1的位置。
public class MyStack {
public static void main(String[] args) {
Stack s=new Stack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.display();
s.pop();
s.display();
System.out.println( s.peek());
}
}
//顺序表实现
class Stack{
int[] elem;
int usedSize;
public static final int CAPACITY=10;
Stack(){
elem=new int[CAPACITY];
}
public void push(int e){
elem[usedSize++]=e;
}
public int pop(){
return elem[usedSize--];
}
public int peek(){
return elem[usedSize-1];
}
public boolean empty(){
return usedSize==0;
}
public int size(){
return usedSize;
}
public void display(){
for(int i=0;i<usedSize;i++){
System.out.print(elem[i]+" ");
}
}
}
面试题:
用队列实现栈
class MyStack {
Queue<Integer> q1;
Queue<Integer> q2;
/** Initialize your data structure here. */
public MyStack() {
q1=new LinkedList<>();
q2=new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
q1.add(x);
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
//出栈
while(q1.size()>1){
q2.add(q1.remove());
}
int x=q1.remove();
Queue<Integer> temp=q1;
q1=q2;
q2=temp;
return x;
}
/** Get the top element. */
public int top() {
while(q1.size()>1){
q2.add(q1.remove());
}
int m=q1.remove();
q2.add(m);
Queue<Integer> temp=q1;
q1=q2;
q2=temp;
return m;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return q1.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
用栈实现队列:
class MyQueue {
Stack<Integer> stack1=new Stack<>();
Stack<Integer> stack2=new Stack<>();
/** Initialize your data structure here. */
public MyQueue() {
}
/** Push element x to the back of queue. */
public void push(int x) {
stack1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(stack2==null){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
/** Get the front element. */
public int peek() {
int m=0;
if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}else{
m=(int)stack2.peek();
}
return m;
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack1.empty()&&stack2.empty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
括号匹配:
class Solution {
public boolean isValid(String s) {
Stack stack=new Stack();
for(int i=0;i<s.length();i++){
if(stack.empty()){
stack.push(s.charAt(i));
}else{
if((char)stack.peek()=='('&&s.charAt(i)==')'
||(char)stack.peek()==')'&&s.charAt(i)=='('
||(char)stack.peek()=='['&&s.charAt(i)==']'
||(char)stack.peek()==']'&&s.charAt(i)=='['
||(char)stack.peek()=='{'&&s.charAt(i)=='}'
||(char)stack.peek()=='}'&&s.charAt(i)=='{'){
stack.pop();
}else{
stack.push(s.charAt(i));
}
}
}
// if(stack.empty()){
// return true;
// }
// return false;
return stack.empty();
}
}