目录
1. 自定义实现Stack
package day19.exer;
import java.util.Arrays;
/**
* 栈是数据结构一种非常重要的线性表:
* LIFO:后进先出
* FILO:先进后出
*
*/
public class Stack {
private Object[] data;
private int size;
private int capacity;
public Stack() {
// 默认创建一个10容量的栈
this.capacity = 10;
data = new Object[this.capacity];
}
public Stack(int capacity) {
this.capacity = capacity;
data = new Object[this.capacity];
}
//进栈一个元素
public void push(Object e){
// 先判断栈是否已经满了
if (this.isFull()) {
// 扩容
// 扩容的规则是原有容量的1.5倍
int length = this.capacity + (this.capacity >>> 1);
this.resize(length);
} else {
this.data[size++] = e;
}
}
// 判断是否存满
private boolean isFull() {
return this.capacity == this.size;
}
// 扩容或缩容容器的大小
private void resize(int len){
// Object[] arr = new Object[len];
// for (int i = 0; i < data.length; i++) {
// arr[i] = data[i];
// }
// this.data = arr;
this.data = Arrays.copyOf(this.data, len);
this.capacity = len;
System.out.println("容量扩充为:" + this.capacity);
}
// 判断栈是否为空
public boolean isEmpty(){
return this.size == 0;
}
// 出栈一个元素
public Object pop(){
if (this.isEmpty()) {
throw new RuntimeException("对不起,栈中已经没有元素了");
}
return this.data[--this.size];
}
// 查看栈顶元素
public Object peek(){
return this.data[this.size - 1];
}
// 获取栈中元素的个数
public int size(){
return this.size;
}
// 清空栈
public void clear(){
this.size = 0;
}
// 返回栈的字符串形式
public String toString(){
return Arrays.toString(Arrays.copyOf(this.data, this.size));
}
// 对比两个栈是否相等
@Override
public boolean equals(Object o) {
Stack s = (Stack) o;
if (this.size != s.size()) {
return false;
}
for (int i = 0; i < s.size() ; i++) {
if (data[i] != s.data[i]) {
return false;
}
}
return true;
}
}
以下是测试代码:
package day19.exer;
import org.junit.jupiter.api.Test;
public class StackTest {
@Test
void testStack() {
Stack stack = new Stack();
System.out.println(stack.isEmpty());
stack.push("A");
stack.push("B");
stack.push(123);
stack.push(456);
stack.push(789);
System.out.println(stack);
System.out.println(stack.size());
System.out.println(stack.isEmpty());
System.out.println(stack.peek());
}
@Test
void testStack02() {
Stack stack = new Stack();
stack.push("A");
stack.push("B");
stack.push(123);
stack.push(456);
stack.push(789);
System.out.println(stack);
stack.pop();
System.out.println(stack);
stack.pop();
System.out.println(stack);
stack.pop();
System.out.println(stack);
stack.pop();
System.out.println(stack);
stack.pop();
System.out.println(stack);
stack.pop();
System.out.println(stack);
}
@Test
void testStack03() {
Stack stack = new Stack();
stack.push("A");
stack.push("B");
stack.push(123);
stack.push(456);
stack.push(456);
stack.push(456);
stack.push(456);
stack.push(456);
stack.push(456);
stack.push(456);
stack.push(456);
stack.push(456);
stack.push(456);
stack.push(456);
stack.push(789);
System.out.println(stack);
System.out.println(stack.size());
}
}
测试结果:
2. 自定义实现Queue
package day8_2;
import java.util.Arrays;
public class myQueue {
private Object[] data;
private int size;
private int capacity;
public myQueue() {
this.capacity = 10;
data = new Object[this.capacity];
}
public myQueue(int capacity) {
this.capacity = capacity;
data = new Object[this.capacity];
}
//进队列一个元素
public void push(Object e){
// 判断队列是否已满
if (this.isFull()) {
// 扩容
int length = this.capacity + (this.capacity >>> 1);
this.resize(length);
} else {
this.data[size++] = e;
}
}
// 判断是否存满
private boolean isFull() {
return this.capacity == this.size;
}
// 扩容或缩容容器的大小
private void resize(int len){
this.data = Arrays.copyOf(this.data, len);
this.capacity = len;
}
// 判断队列是否为空
public boolean isEmpty(){
return this.size == 0;
}
// 出队列一个元素
public Object pop(){
if (this.isEmpty()) {
throw new RuntimeException("队列中已经没有元素!");
}
this.size--;
Object temp = this.data[0];
System.arraycopy(data, 1, this.data, 0, data.length - 1);
return temp;
}
// 获取队列中元素的个数
public int size(){
return this.size;
}
// 清空队列
public void clear(){
this.size = 0;
}
// 返回队列的字符串形式
public String toString(){
return Arrays.toString(Arrays.copyOf(this.data, this.size));
}
// 对比两个队列是否相等
@Override
public boolean equals(Object o) {
myQueue s = (myQueue) o;
if (this.size != s.size()) {
return false;
}
for (int i = 0; i < s.size() ; i++) {
if (data[i] != s.data[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
myQueue mq = new myQueue();
}
}
3. 自定义实现ArrayList
import java.util.Arrays;
/**
*
* 写的方法和底层的ArrayList有所不同
* 比如返回的类型
*/
public class MyArrayList<E> {
/**
* 数据域
*/
private int size;
private final static int DEFAULT_CAPACITY=10;
private Object[] elementData;
/**
* 构造函数
*/
//无参构造函数,默认容量为10
public MyArrayList(){
elementData=new Object[DEFAULT_CAPACITY];
}
public MyArrayList(int capacity){
if (capacity>=0){
elementData=new Object[capacity];
}else{
throw new IllegalArgumentException("Illegal Argument:"+capacity);
}
}
public void add(E e){
if(isFull()) expandCapacity();
elementData[size++]=e;
}
public void add(int index,E e){
rangeCheck(index);
if (isFull()) expandCapacity();
System.arraycopy(elementData,index,elementData, index+1,size-index);
size++;
}
public boolean remove(E e){
if (e==null){
for (int i=0;i<size;i++){
if (elementData[i]==e){
remove(i);
return true;
}
}
}else {
for (int i=0;i<size;i++){
if (e.equals(elementData[i])){
remove(i);
return true;
}
}
}
return false;
}
public E remove(int index){
rangeCheck(index);
E value=(E)elementData[index];
int numMoved=size-index-1;
System.arraycopy(elementData,index+1,elementData,index,numMoved);
//防止对象游离
elementData[--size]=null;
return value;
}
private boolean isFull(){
return size==elementData.length;
}
//扩容1.5倍
private void expandCapacity(){
Object [] newArr=new Object[getNewCapacity()];
System.arraycopy(elementData,0,newArr,0,elementData.length);
elementData=newArr;
}
//注意 数组扩容有可能超过 int 的最大值MaxValue
//这里我没有考虑这个问题,毕竟如果真的到那个数量
//添加元素 和 删除元素的效率会变得低
private int getNewCapacity(){
int oldCapacity=elementData.length;
int newCapacity=elementData.length+(elementData.length>>1);
if (newCapacity==oldCapacity){//针对原数组大小为0 或是1的情况
return newCapacity+1;
}
return newCapacity;
}
public int getSize(){
return size;
}
private void rangeCheck(int index) throws IndexOutOfBoundsException {
if (index < 0 || index > elementData.length - 1)
throw new IndexOutOfBoundsException("Index" + index + "is out of bounds");
}
public String toString(){
StringBuilder sb=new StringBuilder();
sb.append("[");
for (int i=0;i<size;i++){
sb.append(elementData[i]+",");
}
if (size!=0){
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
return "[]";
}
public E get(int index){
rangeCheck(index);
return (E)elementData[index];
}
public void set(E e,int index){
rangeCheck(index);
elementData[index]=e;
}
public void trimToSize(){
if (size<elementData.length){
elementData= Arrays.copyOf(elementData,size);
}
}
public int getCapacity(){
return elementData.length;
}
}
4. 自定义实现LinkedList
package com.openlab.day19.collect;
public class LinkedList {
private Node first;
private Node last;
private int size;
public LinkedList() {
}
public void add(Object obj) {
// 向尾部添加一个新的节点
Node newNode = new Node();
newNode.value = obj;
// 判断是否是第一次添加
if (first == null) {
this.first = newNode;
this.last = newNode;
} else {
newNode.prev = last;
last.next = newNode;
last = newNode;
}
this.size++;
}
public Object get(int index) {
if (index >= this.size || index < 0) {
throw new RuntimeException("对不起,不存在这个下标");
}
Node temp = first;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
return temp.value;
}
class Node {
Node prev;
Node next;
Object value;
public Node() {
}
public Node(Node prev, Object value, Node next) {
this.prev = prev;
this.next = next;
this.value = value;
}
}
}
有待完善!!!