本文最后更新于 394 天前,其中的信息可能已经有所发展或是发生改变。
文章目录[隐藏]
有一段时间没更新博客了,这学期JAVA实验也是又用上了头哥这坨。。。
题目太长而且没什么作用,就只放题目和代码了
题目太长而且没什么作用,就只放题目和代码了
第1关:实现基于数组的栈
/**
* @Author: Vastsea(lyx8851@qq.com)
* @Date: 2024-03-22 17:56:34
* @LastEditors: Vastsea(lyx8851@qq.com)
* @LastEditTime: 2024-03-22 19:06:21
* @Description:
* Link:https://59888888.xyz/
* Phone:18893558104
*/
package step1;
import java.util.NoSuchElementException;
/**
* Created by sykus on 2018/1/26.
*/
public class MyStack<T> {
private T[] S;
private int top;//栈顶元素下标,初始为-1
public MyStack() {
this(1);
}
public MyStack(int capacity) {
S = (T[]) new Object[capacity];
top = -1;
}
/**
* 入栈操作,把item压入栈中
*
* @param item
*/
public void push(T item) {
int len = S.length;
if (top == len - 1) {
resize(2 * len);
}
/********** Begin *********/
S[++top] = item;
/********** End *********/
}
/**
* 返回栈顶元素并从栈中移除
*
* @return
*/
public T pop() {
if (isEmpty()) {
throw new NoSuchElementException("栈为空!");
}
/********** Begin *********/
return S[top--];
/********** End *********/
}
/**
* 判断栈是否为空
*
* @return
*/
public boolean isEmpty() {
if (top < 0)
return true;
else
return false;
}
/**
* 动态扩展数组大小
*
* @param capacity
*/
private void resize(int capacity) {
assert capacity > top;
T[] tmp = (T[]) new Object[capacity];
for (int i = 0; i <= top; i++) {
tmp[i] = S[i];
}
S = tmp;
}
}
第2关:实现基于链表的栈
/**
* @Author: Vastsea(lyx8851@qq.com)
* @Date: 2024-03-22 17:56:34
* @LastEditors: Vastsea(lyx8851@qq.com)
* @LastEditTime: 2024-03-22 19:06:21
* @Description:
* Link:https://59888888.xyz/
* Phone:18893558104
*/
package step2;
import java.util.NoSuchElementException;
/**
* Created by sykus on 2017/12/29.
*/
public class MyStack<E> {
private Node<E> head;//头结点
private Node<E> top;//栈顶
private int size;//栈中元素个数
public MyStack() {
head = new Node<E>();
head.next = null;
top = null;//栈顶初始化为null
size = 0;
}
/**
* 把item压入栈中
*
* @param item
*/
public void push(E item) {
/********** Begin *********/
Node tmp = new Node<E>();
tmp.item = item;
tmp.next = top;
top = tmp;
size++;
/********** End *********/
}
/**
* 返回它栈顶元素并删除
*/
public E pop() {
if (isEmpty())
throw new NoSuchElementException("栈为空!");
/********** Begin *********/
E tmp = top.item;
top = top.next;
size--;
return tmp;
/********** End *********/
}
/**
* 返回栈中元素个数
*
* @return
*/
public int size() {
return size;
}
/**
* 判断一个栈是否为空
*
* @return
*/
public boolean isEmpty() {
return (null == head);
}
//链表结点内部类
private static class Node<E> {
private E item;
private Node<E> next;
}
}
第3关:基于数组的队列
/**
* @Author: Vastsea(lyx8851@qq.com)
* @Date: 2024-03-22 17:56:34
* @LastEditors: Vastsea(lyx8851@qq.com)
* @LastEditTime: 2024-03-22 19:06:21
* @Description:
* Link:https://59888888.xyz/
* Phone:18893558104
*/
package step3;
/**
* Created by zengpeng on 2018/1/30.
*/
public class MyQueue<T> {
private T[] Q;
private int head;
private int tail;
private int size;
public MyQueue() {
this(1);
}
public MyQueue(int capacity) {
Q = (T[]) new Object[capacity];
size = 0;
head = tail = 0;
}
/**
* 入队操作
*
* @param item
*/
public void enqueue(T item) {
/********** Begin *********/
Q[tail++] = item;
size++;
/********** End *********/
}
/**
* 出队操作
*
* @return
*/
public T dequeue() {
/********** Begin *********/
size--;
return Q[head++];
/********** End *********/
}
/**
* 判断队列是否为空
* @return
*/
public boolean isEmpty() {
return (head == tail) && (size < Q.length);
}
public int size() {
return size;
}
}
第4关:基于链表的队列
/**
* @Author: Vastsea(lyx8851@qq.com)
* @Date: 2024-03-22 17:56:34
* @LastEditors: Vastsea(lyx8851@qq.com)
* @LastEditTime: 2024-03-22 19:06:21
* @Description:
* Link:https://59888888.xyz/
* Phone:18893558104
*/
package step4;
import java.util.NoSuchElementException;
/**
* Created by sykus on 2017/12/29.
*/
public class MyQueue<T> {
private Node<T> head;// 头结点,不存数据
private Node<T> front;//指向队头结点
private Node<T> tail;//指向队尾结点
private int size;
public MyQueue() {
head = new Node<T>();
front = tail = null;
size = 0;
}
/**
* 入队
*
* @param item
*/
public void enqueue(T item) {
/********** Begin *********/
Node tmp = new Node<T>();
tmp.item = item;
tmp.next = null;
if (isEmpty()) {
head.next = front = tail = tmp;
}else{
tail.next = tmp;
tail = tmp;
}
size++;
/********** End *********/
}
/**
* 出队
*
* @return
*/
public T dequeue() {
if (isEmpty())
throw new NoSuchElementException("队列为空!");
/********** Begin *********/
T tmp = front.item;
front = front.next;
head.next = front;
if (front == null) {
tail = null;
}
size--;
return tmp;
/********** End *********/
}
/**
* 返回队列中元素数量
*
* @return
*/
public int size() {
return size;
}
/**
* 判断一个队列是否为空
*
* @return
*/
public boolean isEmpty() {
return (front == null);
}
/**
* 链表结点内部类
*/
private static class Node<E> {
private E item;
private Node<E> next;
}
}
整个活,玩一下哈哈