自定义容器

1.自定义栈

栈(Stack)是一种受限的线性数据结构,所谓受限是指栈只暴露栈顶和栈底的操作,其底层是由数组实现的。栈的特性是先进后出

在这里插入图片描述
用数组定义:

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;
	}	
	
	// 判断栈是否为空
	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;
	}
}

2.自定义队列:

1.队列是一种由数组和链表作为底层构造的只暴露头和尾操作API的数据结构
2.特点:先进先出

在这里插入图片描述
通过数组实现:

public class Queue {

	private Object[] data;
	private int size;
	private int capacity;
	
	public Queue() {
		// 默认创建一个10容量的队列
		this.capacity = 10;
		data = new Object[this.capacity];
	}
	
	public Queue(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){
		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 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)	{
		Queue s = (Queue) 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;
	}
}

通过Stack实现:

public class Queue2 {
	
	private Stack stackA;					
	private Stack stackB;		

	public Queue2() {
		stackA = new Stack();
		stackB = new Stack();
	}
	
	// 进队一个元素
	public void offer(Object e){
		this.stackA.push(e);
	}	
	
	// 出队一个元素
	public Object poll(){
		remove();
		return stackB.pop();
	}

	private void remove() {
		if (this.stackB.size() == 0) {
			while (this.stackA.size() != 0) {
				this.stackB.push(this.stackA.pop());
			}
		}
	}	
	
	// 查看队首元素
	public Object element(){
		if (this.stackB.size() != 0) {
			return this.stackB.peek();
		} else {
			remove();
		}
		return this.stackB.peek();
	}		
	
	// 获取队列中元素的个数
	public int size(){
		remove();
		return this.stackB.size();
	}		
	
	// 判断队列是否为空
	public boolean isEmpty(){
		return this.stackB.size() == 0 && this.stackA.size() == 0;
	}
	
	// 清空队列
	public void clear(){
		this.stackA.clear();
		this.stackB.clear();
	}	
	
	// 返回队列的字符串形式
	public String toString(){
		return "展示B中的值";
	}
	
	public boolean equals(Object o) {
		return true;
	}
	
}```


3.自定义ArrayList:
	数组定义:
	
```java
public class ArrayList {
	
	private Object[] objs;
	private int size;
	private int capacity;
	
	public ArrayList() {
		this.capacity = 10;
		this.objs = new Object[this.capacity];
	}
	
	public ArrayList(int capacity) {
		this.capacity = capacity;
		this.objs = new Object[this.capacity];
	}
	
	public void add(Object obj) {
		if (this.isFull()) {
			// 扩容
			this.grow();
		}
		this.objs[size++] = obj;
	}

	private void grow() {
		// 1.5 倍
		int newCapaCity = this.capacity + (this.capacity >>> 1);
		System.out.println("扩容开始,容量由"+ this.capacity +"变为:"+ newCapaCity);
		this.objs = Arrays.copyOf(this.objs, newCapaCity);
		this.capacity = newCapaCity;
		System.out.println("扩容成功");
	}

	private boolean isFull() {
		return this.size == this.capacity;
	}
	
	public void add(int index, Object obj) {
		if (this.isFull()) {
			this.grow();
		}
		for (int i = size; i > index ; i--) {
			this.objs[i] = this.objs[i - 1];
		}
		this.objs[index] = obj;
		this.size++;
	}

	public void remove(int index) {
		for (int i = index; i < this.size - 1; i++) {
			this.objs[index] = this.objs[index+1];
		}
		this.size--;
	}

	public void remove(Object obj) {
		int m=0;
		for(int i=0;i<this.size-1;i++){
			if(this.objs[i]==obj){
				m=i;
			}
		}
		for (int i = m; i < this.size - 1; i++) {
			this.objs[m] = this.objs[m+1];
		}
		this.size--;
	}
	
	public void update(int index, Object obj) {
		this.objs[index] = obj;
	}

	public Object load(Object Object) {
		
		return null;
	}

	public Object get(int index) {
		if (index < size) {
			return null;
		}
		return this.objs[index];
	}

	public Object[] list() {
		return Arrays.copyOf(this.objs, size);
	}

	public void clear() {
		this.size = 0;
	}

	public String toString() {
		return "ObjectMangerImpl [objs=" + Arrays.toString(objs) + ", size=" + size + ", capacity=" + capacity + "]";
	}
	
	public int size() {
		return this.size;
	}
	
}

4.自定义链表:

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
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;
		}
	}

5.自定义LinkedList
数组定义:


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;
	}
	
	//按元素查找
    public Node getNodeByEle(Object ele) {
        Node currentNode = first;
        while (currentNode != null) {
            if (ele.equals(currentNode.ele)) {
                return currentNode;
            }
            currentNode = currentNode.next;
        }
        return null;
    }

  // 删除第一个节点
    public void removeFirst() {
        if (size == 0) {
            throw new IllegalArgumentException("链表中没有节点,无法删除");
        }
        first.ele = null;
        first = first.next;
        first.prev = null;
        size--;
    }

    // 删除最后一个节点
    public void removeLast() {
        if (size == 0) {
            throw new IllegalArgumentException("链表中没有节点,无法删除");
        }
        last.ele = null;
        last = last.prev;
        last.next = null;
        size--;
    }

    // 删除非头尾节点元素,
    public void removeInside(Node node) {
        node.ele = null;
        node.prev.next = node.next;
        node.next.prev = node.prev;
        size--;
    }
    
// 删除节点
    public  void removeNode(Node node) {
        if (node == first) {
            removeFirst();
        } else if (node == last) {
            removeLast();
        } else {
            removeInside(node);
        }
    }

    // 根据元素删除节点
    public void removeNodeByEle(Object ele) {
        Node node = getNodeByEle(ele); // 先查询该节点是否存在
        if (node == null) {
            throw new IllegalArgumentException("不存在该节点信息,无法删除");
        }
        removeNode(node);
    }

    // 根据索引删除节点
    public void removeNodeByIndex(int index) {
        Node node = getNodeByIndex(index); // 先查询该节点是否存在
        if (node == null) {
            throw new IllegalArgumentException("不存在该节点信息,无法删除");
        }
        removeNode(node);
    }

 // 获取链表长度
    public int getLength() {
        return size;
    }
    
public String toString() {
		return "ObjectMangerImpl [objs=" + Arrays.toString(objs) + ", size=" + size + ", capacity=" + capacity + "]";
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值