1.动态数组
package cn.xxx.array;
public class Array<E> {
// 存储用的数组
private E[] data;
// 容器已使用的大小
private int size;
/**
* 构造方法
*
* @param capacity
* 传入数组容量
*/
@SuppressWarnings("unchecked")
public Array(int capacity) {
data = (E[]) new Object[capacity];
size = 0;
}
/**
* 无参构造方法,初始容量10
*/
public Array() {
this(10);
}
/**
* 获取元素个数
*
* @return 元素个数
*/
public int getSize() {
return size;
}
/**
* 获取数组容量
*
* @return 容量大小
*/
public int getCapacity() {
return data.length;
}
/**
* 返回数组是否为空
*
* @return 为空返回true,不为空返回false
*/
public boolean isEmpty() {
return size == 0;
}
/**
* 向数组末尾添加元素
*
* @param e
* 待添加元素
*/
public void addLast(E e) {
add(size, e);
}
/**
* 在所有元素前添加一个新元素
*
* @param e
* 待添加元素
*/
public void addFirst(E e) {
add(0, e);
}
/**
* 向第index插入元素e
*
* @param index
* 插入位置
* @param e
* 待插入元素
*/
public void add(int index, E e) {
if (index < 0 || index > size)
throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
if (size == data.length)
resize(2 * data.length);
for (int i = size - 1; i >= index; i--)
data[i + 1] = data[i];
data[index] = e;
size++;
}
/**
* 获取指定位置元素
*
* @param index
* 指定位置
* @return 指定位置元素
*/
public E get(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Get failed. Index is illegal!");
}
return data[index];
}
/**
* 获取尾元素的值
*
* @return 尾元素值
*/
public E getLast() {
return get(size - 1);
}
/**
* 获取首元素的值
*
* @return 首元素值
*/
public E getFirst() {
return get(0);
}
/**
* 修改指定位置元素
*
* @param index
* 指定位置
* @param e
* 修改元素值
*/
public void set(int index, E e) {
if (index < 0 || index >= size)
throw new IllegalArgumentException("Set failed. Index is illegal.");
data[index] = e;
}
/**
* 查找数组中是否有元素e
*
* @param e
* 带查找元素
* @return 存在返回true,不存在返回false
*/
public boolean contains(E e) {
for (int i = 0; i < size; i++) {
if (data[i].equals(e)) {
return true;
}
}
return false;
}
/**
* 从数组中删除元素e
*
* @param e
* 待删除元素
* @return 成功返回true,失败返回false
*/
public boolean removeElement(E e) {
int index = find(e);
if (index != -1) {
remove(index);
return true;
}
return false;
}
/**
* 查找指定元素位置
*
* @param e
* 待查找元素
* @return 存在返回元素位置,不存在返回-1
*/
public int find(E e) {
for (int i = 0; i < size; i++) {
if (data[i].equals(e)) {
return i;
}
}
return -1;
}
/**
* 删除指定位置元素
*
* @param index
* 指定位置
* @return 被删除元素
*/
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Remove failed. Index is illegal!");
}
E ret = data[index];
for (int i = index + 1; i < size; i++)
data[i - 1] = data[i];
size--;
data[size] = null;
// 防止复杂度震荡
if (size == data.length / 4 && data.length / 2 != 0) {
resize(data.length / 2);
}
return ret;
}
/**
* 删除首元素
*
* @return 被删除首元素
*/
public E removeFirst() {
return remove(0);
}
/**
* 删除尾元素
*
* @return 被删除尾元素
*/
public E removeLast() {
return remove(size - 1);
}
/**
* 将数组空间的容量变成newCapacity大小
*
* @param newCapacity
* 新容量
*/
@SuppressWarnings("unchecked")
private void resize(int newCapacity) {
E[] newData = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
data = newData;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length));
res.append('[');
for (int i = 0; i < size; i++) {
res.append(data[i]);
if (i != size - 1)
res.append(", ");
}
res.append(']');
return res.toString();
}
}
2.栈
package cn.xxx.stack;
public interface Stack<E> {
/**
* 获取栈结构元素个数
* @return 元素个数
*/
public int getSize();
/**
* 判断栈结构是否为空
* @return 为空返回true,不为空返回false
*/
public boolean isEmpty();
/**
* 将元素压入栈中
* @param e 待添加元素
*/
public void push(E e);
/**
* 元素出栈
* @return 出栈的元素值
*/
public E pop();
/**
* 得到栈顶元素
* @return 栈顶元素值
*/
public E peek();
}
package cn.xxx.stack;
import cn.xxx.array.Array;
public class ArrayStack<E> implements Stack<E> {
private Array<E> array;
public ArrayStack(int capacity){
array = new Array<>(capacity);
}
public ArrayStack(){
array = new Array<>();
}
@Override
public int getSize(){
return array.getSize();
}
@Override
public boolean isEmpty(){
return array.isEmpty();
}
public int getCapacity(){
return array.getCapacity();
}
@Override
public void push(E e){
array.addLast(e);
}
@Override
public E pop(){
return array.removeLast();
}
@Override
public E peek(){
return array.getLast();
}
@Override
public String toString(){
StringBuilder res = new StringBuilder();
res.append("Stack: ");
res.append('[');
for(int i = 0 ; i < array.getSize() ; i ++){
res.append(array.get(i));
if(i != array.getSize() - 1)
res.append(", ");
}
res.append("] top");
return res.toString();
}
}
3.队列
package cn.xxx.queue;
public interface Queue<E> {
/**
* 获取队列元素个数
* @return 元素个数
*/
public int getSize();
/**
* 判断队列是否为空
* @return 为空返回true,不为空返回false
*/
public boolean isEmpty();
/**
* 入队列
* @param e 待添加的元素值
*/
public void enqueue(E e);
/**
* 出队列
* @return 被移除的元素值
*/
public E dequeue();
/**
* 查看队首元素
* @return 队首元素值
*/
public E getFront();
}
package cn.xxx.queue;
import cn.xxx.array.Array;
public class ArrayQueue<E> implements Queue<E> {
private Array<E> array;
public ArrayQueue(int capacity){
array = new Array<>(capacity);
}
public ArrayQueue(){
array = new Array<>();
}
@Override
public int getSize(){
return array.getSize();
}
@Override
public boolean isEmpty(){
return array.isEmpty();
}
public int getCapacity(){
return array.getCapacity();
}
@Override
public void enqueue(E e){
array.addLast(e);
}
@Override
public E dequeue(){
return array.removeFirst();
}
@Override
public E getFront(){
return array.getFirst();
}
@Override
public String toString(){
StringBuilder res = new StringBuilder();
res.append("Queue: ");
res.append("front [");
for(int i = 0 ; i < array.getSize() ; i ++){
res.append(array.get(i));
if(i != array.getSize() - 1)
res.append(", ");
}
res.append("] tail");
return res.toString();
}
}
package cn.xxx.queue;
public class LoopQueue<E> implements Queue<E> {//循环队列
private E[] data;
private int front, tail;
private int size;
@SuppressWarnings("unchecked")
public LoopQueue(int capacity){
data = (E[])new Object[capacity + 1];
front = 0;
tail = 0;
size = 0;
}
public LoopQueue(){
this(10);
}
public int getCapacity(){
return data.length - 1;
}
@Override
public boolean isEmpty(){
return front == tail;
}
@Override
public int getSize(){
return size;
}
@Override
public void enqueue(E e){
if((tail + 1) % data.length == front)
resize(getCapacity() * 2);
data[tail] = e;
tail = (tail + 1) % data.length;
size ++;
}
@Override
public E dequeue(){
if(isEmpty())
throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
E ret = data[front];
data[front] = null;
front = (front + 1) % data.length;
size --;
if(size == getCapacity() / 4 && getCapacity() / 2 != 0)
resize(getCapacity() / 2);
return ret;
}
@Override
public E getFront(){
if(isEmpty())
throw new IllegalArgumentException("Queue is empty.");
return data[front];
}
private void resize(int newCapacity){
@SuppressWarnings("unchecked")
E[] newData = (E[])new Object[newCapacity + 1];
for(int i = 0 ; i < size ; i ++)
newData[i] = data[(i + front) % data.length];
data = newData;
front = 0;
tail = size;
}
@Override
public String toString(){
StringBuilder res = new StringBuilder();
res.append(String.format("Queue: size = %d , capacity = %d\n", size, getCapacity()));
res.append("front [");
for(int i = front ; i != tail ; i = (i + 1) % data.length){
res.append(data[i]);
if((i + 1) % data.length != tail)
res.append(", ");
}
res.append("] tail");
return res.toString();
}
}