目录
- [数据结构]栈和队列
- 一.栈
- 1.栈的基本概念
- 1.2 栈的常见基本操作
- 1.3 栈的实现
- 1.3.1 入栈
- 1.3.2 出栈
- 1.3.3获取栈顶元素
- 1.3.4 判断栈为空
- 1.3.5 栈实现
- 二.队列
- 2.1 入队
- 2.2 出队
- 2.3 获取队首元素
- 2.4 实现队列
[数据结构]栈和队列
一.栈
1.栈的基本概念
栈(Stack):是只允许在一端进行插入或删除的线性表。首先栈是一种线性表,但限定这种线性表只能在某一端进行插入和删除操作。
栈顶(Top):线性表允许进行插入删除的那一端。
栈底(Bottom):固定的,不允许进行插入和删除的另一端。
空栈:不含任何元素的空表。
栈又称为后进先出(Last In First Out)的线性表,简称LIFO结构
1.2 栈的常见基本操作
InitStack():初始化一个空栈S。
StackEmpty():判断一个栈是否为空,若栈为空则返回true,否则返回false。
Push():进栈,栈的插入操作
Pop():出栈,栈的删除操作
GetTop():读栈顶元素
1.3 栈的实现
1.3.1 入栈
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
int ret = stack.push(4);
System.out.println(ret);
}
1.3.2 出栈
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
int ret1 = stack.pop();
int ret2 = stack.pop();
System.out.println(ret1);
System.out.println(ret2);
}
1.3.3获取栈顶元素
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
int ret1 = stack.pop();
int ret2 = stack.pop();
int ret3 = stack.peek();
System.out.println(ret1);
System.out.println(ret2);
System.out.println(ret3);
}
1.3.4 判断栈为空
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
int ret1 = stack.pop();
int ret2 = stack.pop();
int ret3 = stack.peek();
System.out.println(ret1);
System.out.println(ret2);
System.out.println(ret3);
stack.pop();
boolean flag = stack.empty();
System.out.println(flag);
}
1.3.5 栈实现
public class MyStack<T> {
private T[] elem;//数组
private int top;//当前可以存放数据元素的下标-》栈顶指针
public MyStack() {
this.elem = (T[])new Object[10];
}
/**
* 入栈操作
* @param item 入栈的元素
*/
public void push(T item) {
//1、判断当前栈是否是满的
if(isFull()){
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
//2、elem[top] = item top++;
this.elem[this.top++] = item;
}
public boolean isFull(){
return this.elem.length == this.top;
}
/**
* 出栈
* @return 出栈的元素
*/
public T pop() {
if(empty()) {
throw new UnsupportedOperationException("栈为空!");
}
T ret = this.elem[this.top-1];
this.top--;//真正的改变了top的值
return ret;
}
/**
* 得到栈顶元素,但是不删除
* @return
*/
public T peek() {
if(empty()) {
throw new UnsupportedOperationException("栈为空!");
}
//this.top--;//真正的改变了top的值
return this.elem[this.top-1];
}
public boolean empty(){
return this.top == 0;
}
}
public static void main(String[] args) {
MyStack<Integer> myStack = new MyStack<>();
myStack.push(1);
myStack.push(2);
myStack.push(3);
System.out.println(myStack.peek());
System.out.println(myStack.pop());
System.out.println(myStack.pop());
System.out.println(myStack.pop());
System.out.println(myStack.empty());
System.out.println("============================");
MyStack<String> myStack2 = new MyStack<>();
myStack2.push("hello");
myStack2.push("word");
myStack2.push("thank");
System.out.println(myStack2.peek());
System.out.println(myStack2.pop());
System.out.println(myStack2.pop());
System.out.println(myStack2.pop());
System.out.println(myStack2.empty());
}
二.队列
队列(queue) 是只允许在一端进行插入操作,而在另一端进行删除操作的线性表
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾(Tail/Rear) 出队列:进行删除操作的一端称为队头 (Head/Front)
2.1 入队
public static void main(String[] args) {
Deque<Integer> queue = new LinkedList<>();
queue.offer(1);
queue.offer(2);
queue.offer(3);
queue.offer(4);
}
2.2 出队
public static void main(String[] args) {
Deque<Integer> queue = new LinkedList<>();
queue.offer(1);
queue.offer(2);
queue.offer(3);
queue.offer(4);
System.out.println(queue.poll());
System.out.println(queue.poll());
}
2.3 获取队首元素
public static void main(String[] args) {
Deque<Integer> queue = new LinkedList<>();
queue.offer(1);
queue.offer(2);
queue.offer(3);
queue.offer(4);
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println("-----------------");
System.out.println(queue.peek());
}
2.4 实现队列
class Node {
private int val;
private Node next;
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node(int val) {
this.val = val;
}
}
public class MyQueue {
private Node first;
private Node last;
//入队
public void offer(int val) {
//尾插法 需要判断是不是第一次插入
Node node = new Node(val);
if(this.first == null) {
this.first = node;
this.last = node;
}else {
this.last.setNext(node);//last.next = node;
this.last = node;
}
}
//出队
public int poll() {
//1判断是否为空的
if(isEmpty()) {
throw new UnsupportedOperationException("队列为空!");
}
//this.first = this.first.next;
int ret = this.first.getVal();
this.first = this.first.getNext();
return ret;
}
//得到队头元素但是不删除
public int peek() {
//不要移动first
if(isEmpty()) {
throw new UnsupportedOperationException("队列为空!");
}
return this.first.getVal();
}
//队列是否为空
public boolean isEmpty() {
return this.first == null;
}
}
public static void main(String[] args) {
MyQueue myQueue = new MyQueue();
myQueue.offer(1);
myQueue.offer(2);
myQueue.offer(3);
System.out.println(myQueue.peek());
System.out.println(myQueue.poll());
System.out.println(myQueue.poll());
System.out.println(myQueue.poll());
System.out.println(myQueue.isEmpty());
}