栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
1、栈
public class Stack {
private int[] arr;
private int flag = 0;
public Stack(int size){
arr = new int[size];
}
public void add(int val){
if(flag == arr.length){
System.out.println("栈满");
//扩容机制
int[] brr = new int[arr.length*2];
for(int i = 0;i < arr.length;i++){
brr[i] = arr[i];
}
arr = brr;
}
arr[flag] = val;
flag++;
}
public void get(){
if(flag == 0){
System.out.println("栈空");
return;
}
flag--;
System.out.println(arr[flag]);
}
}
2、队列
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
public class Queue {
private int[] arr;
private int front;
private int rear;
public Queue(int size){
arr = new int[size];
}
public void add(int val){
if(front - rear == arr.length){
//扩容机制
int[] brr = new int[arr.length*2];
for(int i = rear;i < front;i++){
brr[i] = arr[i%arr.length];
}
arr = brr;
}
arr[front % arr.length] = val;
front++;
}
public void get(){
if(front == rear){
System.out.println("队列空");
return;
}
System.out.println(arr[rear % arr.length]);
rear++;
}
}
3、测试类
public class Test {
public static void main(String[] args) {
Stack stack = new Stack(10);
stack.add(5);
stack.add(6);
stack.add(7);
stack.get();
stack.get();
stack.get();
stack.get();
stack.get();
stack.get();
Queue queue = new Queue(10);
queue.add(10);
queue.add(10);
queue.add(10);
queue.add(10);
queue.get();
queue.get();
queue.get();
queue.get();
queue.get();
queue.get();
queue.get();
queue.get();
queue.get();
}
}
1、首先是栈
import java.util.Stack;
public class StackTest {
public static void main(String[] args) {
//1、empty() 判空
//2、peek() 查看栈顶元素,不弹栈
//3、pop() 弹栈
//4、push() 压栈
//5、search()返回对象在栈中的位置
Stack<Integer> stack = new Stack<>();
System.out.println("判断栈是否为空:"+stack.empty());
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
System.out.println("判断栈是否为空:"+stack.empty());
System.out.println(stack.peek());
System.out.println(stack.peek());
System.out.println(stack.peek());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.search(1));
}
}
import java.util.LinkedList;
import java.util.Queue;
public class QueueTest {
public static void main(String[] args) {
//LinkedList ---链表 ---实现了Queue接口
Queue<String> queue = new LinkedList<>();
//offer()、add()
//---向队列中添加数据,
//add()在存储失败时抛出异常,
//offer()在存储失败时会返回flase;
//poll()、remove()
//---删除数据,
//remove()在删除失败时抛出异常,
//poll()在删除失败时返回null;
//peek()、element()
//---查看队列的头部元素,
//element()在查找失败时会抛出异常,
//peek()在查找失败时会返回null;
queue.offer("a");
queue.offer("b");
queue.offer("c");
queue.offer("d");
queue.offer("e");
//注意这里的for-each遍历只是顺序输出元素,和出队无关
for(String str : queue){
System.out.println(str);
}
System.out.println("--------------");
queue.poll();
for(String str : queue){
System.out.println(str);
}
System.out.println("--------------");
System.out.println(queue.peek());
for(String str : queue){
System.out.println(str);
}
}
}
1、找到栈中最小值
import java.util.Stack;
public class MinStack {
Stack<Integer> num = new Stack<>();
Stack<Integer> min = new Stack<>();
//入栈
public void push(Integer data){
//数据入栈
num.push(data);
//判断min栈中有没有值,如果没有就写入,有就比较栈顶大小
if(min.empty() || data <= min.peek()){
//当小于栈顶元素的时候入栈
min.push(data);
}
}
//出栈
public int pop(){
if(num.peek() == min.peek()){
min.pop();
}
return num.pop();
}
//返回最小值
public void min(){
if(!min.empty()){
System.out.println(min.peek());
}
}
}
2、判断出栈顺序是否正确
import java.util.Stack;
public class RightStack {
public boolean right(int[] pushed,int[] poped){
Stack<Integer> stack = new Stack<>();
int i = 0;
for(int num : pushed){
stack.push(num);
//栈不为空,且栈顶元素与弹栈序列相等,弹栈,并指向下一位
while(!stack.isEmpty() && stack.peek() == poped[i]){
stack.pop();
i++;
}
}
//根据弹栈序列弹栈后,若栈空则证明弹栈顺序正确
return stack.isEmpty();
}
}
3、两个栈实现一个队列
import java.util.Stack;
public class StackToQueue {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
//入栈
public void push(Integer data){
stack1.push(data);
}
//出栈
public int pop(){
Integer re = null;
if(!stack2.empty()){
re = stack2.pop();
}else{
while(!stack1.empty()){
re = stack1.pop();
stack2.push(re);
}
if(!stack2.empty()){
re = stack2.pop();
}
}
return re;
}
}
4、两个队列实现一个栈
import java.util.LinkedList;
import java.util.Queue;
public class QueueToStack {
Queue<Integer> queue1 = new LinkedList<>();
Queue<Integer> queue2 = new LinkedList<>();
//入栈
public void push(Integer data){
queue1.offer(data);
}
//弹栈
public int pop(){
Integer data = null;
while(!queue1.isEmpty()){
data = queue1.poll();
//最后一个数据弹栈
if(queue1.isEmpty()){
break;
}
queue2.offer(data);
}
while(!queue2.isEmpty()){
queue1.offer(queue2.poll());
}
return data;
}
}