import java.util.LinkedList;
/**
-
基于LinkedList实现栈
-
在LinkedList实力中只选择部分基于栈实现的接口
*/
public class StackList {
private LinkedList ll = new LinkedList();
//入栈
public void push(E e){
ll.addFirst(e);
}
//查看栈顶元素但不移除
public E peek(){
return ll.getFirst();
}
//出栈
public E pop(){
return ll.removeFirst();
}
//判空
public boolean empty(){
return ll.isEmpty();
}
//打印栈元素
public String toString(){
return ll.toString();
}
}
队列的顺序存储结构实现
public class Queue {
private Object[] data=null;
private int maxSize; //队列容量
private int front; //队列头,允许删除
private int rear; //队列尾,允许插入
//构造函数
public Queue(){
this(10);
}
public Queue(int initialSize){
if(initialSize >=0){
this.maxSize = initialSize;
data = new Object[initialSize];
front = rear =0;
}else{
throw new RuntimeException(“初始化大小不能小于0:” + initialSize);
}
}
//判空
public boolean empty(){
return rear==front?true:false;
}
//插入
public boolean add(E e){
if(rear== maxSize){
throw new RuntimeException(“队列已满,无法插入新的元素!”);
}else{
data[rear++]=e;
return true;
}
}
//返回队首元素,但不删除
public E peek(){
if(empty()){
throw new RuntimeException(“空队列异常!”);
}else{
return (E) data[front];
}
}
//出队
public E poll(){
if(empty()){
throw new Ru 需要zi料+ 绿色徽【vip1024b】
ntimeException(“空队列异常!”);
}else{
E value = (E) data[front]; //保留队列的front端的元素的值
data[front++] = null; //释放队列的front端的元素
return value;
}
}
//队列长度
public int length(){
return rear-front;
}
}
循环队列的顺序存储结构实现
import java.util.Arrays;
public class LoopQueue {
public Object[] data = null;
private int maxSize; // 队列容量
private int rear;// 队列尾,允许插入
private int front;// 队列头,允许删除
private int size=0; //队列当前长度
public LoopQueue() {
this(10);
}
public LoopQueue(int initialSize) {
if (initialSize >= 0) {
this.maxSize = initialSize;
data = new Object[initialSize];
front = rear = 0;
} else {
throw new RuntimeException(“初始化大小不能小于0:” + initialSize);
}
}
// 判空
public boolean empty() {
return size == 0;
}
// 插入
public boolean add(E e) {
if (size == maxSize) {
throw new RuntimeException(“队列已满,无法插入新的元素!”);
} else {
data[rear] = e;
rear = (rear + 1)%maxSize;
size ++;
return true;
}
}
// 返回队首元素,但不删除
public E peek() {
if (empty()) {
throw new RuntimeException(“空队列异常!”);
} else {
return (E) data[front];
}
}
// 出队
public E poll() {
if (empty()) {
throw new RuntimeException(“空队列异常!”);
} else {
E value = (E) data[front]; // 保留队列的front端的元素的值
data[front] = null; // 释放队列的front端的元素
front = (front+1)%maxSize; //队首指针加1
size–;
return value;
}
}
// 队列长度
public int length() {
return size;
}
//清空循环队列
public void clear(){
Arrays.fill(data, null);
size = 0;
front = 0;
rear = 0;
}
}
队列的链式存储结构实现
public class LinkQueue {
// 链栈的节点
private class Node {
E e;
Node next;
public Node() {
}
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
}
private Node front;// 队列头,允许删除
private Node rear;// 队列尾,允许插入
private int size; //队列当前长度
public LinkQueue() {
front = null;
rear = null;
}
//判空
public boolean empty(){
return size==0;
}
//插入
public boolean add(E e){
if(empty()){ //如果队列为空
front = new Node(e,null);//只有一个节点,front、rear都指向该节点
rear = front;
}else{
Node newNode = new Node(e, null);
rear.next = newNode; //让尾节点的next指向新增的节点
rear = newNode; //以新节点作为新的尾节点
}
size ++;
return true;
}
总结
本文从基础到高级再到实战,由浅入深,把MySQL讲的清清楚楚,明明白白,这应该是我目前为止看到过最好的有关MySQL的学习笔记了,我相信如果你把这份笔记认真看完后,无论是工作中碰到的问题还是被面试官问到的问题都能迎刃而解!
MySQL50道高频面试题整理:
e){
if(empty()){ //如果队列为空
front = new Node(e,null);//只有一个节点,front、rear都指向该节点
rear = front;
}else{
Node newNode = new Node(e, null);
rear.next = newNode; //让尾节点的next指向新增的节点
rear = newNode; //以新节点作为新的尾节点
}
size ++;
return true;
}
总结
本文从基础到高级再到实战,由浅入深,把MySQL讲的清清楚楚,明明白白,这应该是我目前为止看到过最好的有关MySQL的学习笔记了,我相信如果你把这份笔记认真看完后,无论是工作中碰到的问题还是被面试官问到的问题都能迎刃而解!
MySQL50道高频面试题整理:
[外链图片转存中…(img-t8AdnxUT-1710355099399)]