Java数据结构——类改写为泛型类

文章目录

  • 1. 泛型循环队列
  • 2. 泛型栈

1. 泛型循环队列

package datastructure.queue;

import java.lang.reflect.Array;

/**
 * 泛型循环队列
 * 
 * @author NoBug
 * @param 
 * @time 2022/3/10
 * 
 */
public class GenericSqCircQueue<T> {

	/**
	 * 循环队列的大小
	 */
	public static final int MAX_QUEUE_SIZE = 20;

	/** 工作空间 */
	T[] data;

	/** 对头 */
	int head;

	/** 队尾 */
	int tail;

	/** 构造一个循环队列 */
	@SuppressWarnings("unchecked")
	GenericSqCircQueue(Class<T> type) {
		data = (T[]) Array.newInstance(type, MAX_QUEUE_SIZE);
		head = tail = 0;
	}

	/**
	 * 入循环队
	 * 
	 * @param elem
	 * @return true or false
	 */
	public boolean enSqCircleQueue(T elem) {

		// 队满
		if ((tail + 1) % 100 == head) {
			System.out.println("CircleQueue is full");
			return false;
		}

		data[tail % MAX_QUEUE_SIZE] = elem;
		tail++;

		return true;
	}// of enSCQ

	/**
	 * 出循环队
	 * 
	 * @return 删除的元素
	 */
	public T deSqCircleQueue() {

		// 队空
		if (head == tail) {
			System.out.println("Circle Queue is Empty");
			return null;
		}

		T tempValue = data[head % MAX_QUEUE_SIZE];
		head++;

		return tempValue;
	}// of deSCQ

	/**
	 * 循环队列实际元素个数
	 * 
	 * @return 元素个数
	 */
	public int lengthOfCircleQueue() {
		return (tail - head + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
	}

	/**
	 * toString
	 */
	public String toString() {
		String resultString = "";
		if (head == tail) {
			return "Empty";
		}
		for (int i = head; i < tail; i++) {
			resultString += data[i % MAX_QUEUE_SIZE] + " ";
		}

		return resultString;
	}

	public static void main(String[] args) {
		GenericSqCircQueue<Integer> queue = new GenericSqCircQueue<Integer>(Integer.class);

		// test1
		System.out.println("打印循环队列:" + queue.toString());
		System.out.println("循环队列元素个数:" + queue.lengthOfCircleQueue());

		// test2
		queue.enSqCircleQueue(1);
		queue.enSqCircleQueue(2);
		queue.enSqCircleQueue(6);
		queue.enSqCircleQueue(8);
		queue.enSqCircleQueue(5);
		System.out.println("打印循环队列:" + queue.toString());
		System.out.println("循环队列元素个数:" + queue.lengthOfCircleQueue());

		// test3
		queue.deSqCircleQueue();
		System.out.println("打印循环队列:" + queue.toString());
		System.out.println("循环队列元素个数:" + queue.lengthOfCircleQueue());
	}

}
打印循环队列:Empty
循环队列元素个数:0
打印循环队列:1 2 6 8 5 
循环队列元素个数:5
打印循环队列:2 6 8 5 
循环队列元素个数:4

2. 泛型栈

package datastructure.stack;

import java.lang.reflect.Array;

public class GenericSqStack<T> {

	/** 栈最大长度 */
	public static final int STACK_SIZE = 15;

	/** 栈实际长度 */
	public int stackSize;

	/** 栈工作空间 */
	public T[] data;

	/**
	 * 构造一个空栈
	 */
	@SuppressWarnings("unchecked")
	GenericSqStack(Class<T> type) {
		this.stackSize = 0;
		this.data = (T[]) Array.newInstance(type, STACK_SIZE);
	}// of SqStack

	/**
	 * 便历,输出,重写toString()
	 * 
	 * @author NoBug
	 * @return result data数组里面的值,以字符串形式输出
	 */
	public String toString() {
		String result = "";
		for (int i = 0; i < this.stackSize; i++) {
			result += data[i] + " ";
		}

		return result;
	}// of toString

	/**
	 * 入栈push
	 * 
	 * @param elem 表示待入栈的元素
	 * @return 成功或失败
	 */
	public boolean push(T elem) {
		if (this.stackSize == STACK_SIZE) {
			System.out.println("Stack full.");
			return false;
		} // Stack full
		this.data[this.stackSize] = elem;
		this.stackSize++;

		return true;
	}// of push

	/**
	 * 出栈pop
	 * 
	 * @author NoBug
	 * @return 删除栈顶,并返回栈顶值
	 */
	public T pop() {
		if (this.stackSize == 0) {
			System.out.println("StackEmpty.");
			return null;
		} // of StackEmpty

		T elem = data[this.stackSize - 1];
		this.stackSize--;

		return elem;
	}// of pop

	/**
	 * 判断栈是否为空
	 * 
	 * @author NoBug
	 * @return true or false 空或者不空
	 */
	public boolean isEmpty() {
		if (this.stackSize == 0) {

			return true;
		} // Empty
		else {

			return false;
		}
	}// of isEmpty

	public static void main(String[] args) {
		GenericSqStack<Integer> stack = new GenericSqStack<Integer>(Integer.class);
		stack.push(1);
		stack.push(2);
		stack.push(3);
		stack.push(4);

		System.out.println("入栈,并打印栈:" + stack.toString());
		int temp = stack.pop();
		System.out.println("出栈,并打印栈顶元素:" + temp);
		System.out.println("栈是否为空:" + stack.isEmpty());
	}

}
入栈,并打印栈:1 2 3 4 
出栈,并打印栈顶元素:4
栈是否为空:false

你可能感兴趣的:(Java数据结构,Java基础篇,java,数据结构,链表)