第一题 数组底层 队列
import java.util.Scanner;
public class QueueTest {
public static void main(String[] args) {
MyQueue<Object> myQueue = new MyQueue<>();
myQueue.InitialInterFace();
}
}
class MyQueue<E>{
private Object[] data;
private int head;
private int rear;
private int Initialcapacity = 10;
private boolean flag;
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = head+1; i <= rear ; i++){
if (data[i] != null){
sb.append(data[i]);
sb.append(",");
}
}
String s = sb.substring(0,sb.length()-1);
return "MyQueue{" +
"data=" + s +
'}';
}
public void print(){
StringBuilder sb = new StringBuilder();
for (int i = head+1; i <= rear ; i++){
if (data[i] != null){
sb.append(data[i]);
sb.append(",");
}
}
String s = sb.substring(0,sb.length()-1);
System.out.println("MyQueue{" +
"data=" + s +
'}');
}
public int getHead() {
return head;
}
public void setHead(int head) {
this.head = head;
}
public int getRear() {
return rear;
}
public void setRear(int rear) {
this.rear = rear;
}
public MyQueue(){
data = new Object[Initialcapacity];
head = -1;
rear = -1;
flag = true;
}
public MyQueue(int InitialCapacity){
data = new Object[InitialCapacity];
head = -1;
rear = -1;
flag = true;
}
public void push(E data){
if (isFull()){
throw new RuntimeException("队列已满,入队失败");
}
rear++;
this.data[rear] = data;
}
public boolean isFull(){
return this.rear + 1 >= data.length;
}
public boolean isEmpty(){
return this.head == this.rear;
}
public E pop(){
if (isEmpty()){
throw new RuntimeException("队列为空,出队失败");
}
head++;
return (E) this.data[head];
}
public E peek(){
if (isEmpty()){
throw new RuntimeException("队列为空,获取队头失败");
}
return (E) this.data[head+1];
}
public void InitialInterFace(){
System.out.println("================主菜单================");
System.out.println("1、入队列");
System.out.println("2、出队列");
System.out.println("3、显示队首元素");
System.out.println("4、显示队尾元素");
System.out.println("5、显示当前队列");
System.out.println("6、退出");
System.out.println("================主菜单================");
System.out.println("请选择需要的功能:");
while (flag){
Scanner sc = new Scanner(System.in);
int msgFromKey = sc.nextInt();
if (msgFromKey == 1){
System.out.println("请输入入队元素:");
String msg = sc.next();
this.push((E) msg);
System.out.println("入队成功,请继续选择功能");
}
else if (msgFromKey == 2){
System.out.println("弹出的元素为:");
System.out.println(this.pop());
System.out.println("弹出成功,请继续选择功能");
}
else if (msgFromKey == 3){
System.out.println("队首元素为:");
System.out.println(this.peek());
System.out.println("显示成功,请继续选择功能");
}
else if (msgFromKey == 4){
System.out.println("队尾元素为:");
System.out.println(this.data[this.getRear()]);
System.out.println("显示成功,请继续选择功能");
}
else if (msgFromKey == 5){
System.out.println("当前队列为:");
this.print();
System.out.println("显示完毕,请继续选择功能");
}
else if (msgFromKey == 6){
System.out.println("Bye Bye ~");
System.out.println("Bye Bye ~");
System.out.println("Bye Bye ~");
flag = false;
}
else {
System.out.println("输入有误,请重新输入");
}
}
}
}
第一题 链表底层 队列
import java.util.EmptyStackException;
public class LinkedQueueTest {
public static void main(String[] args) throws Exception {
MyLinkedQueue<Object> myLinkedQueue = new MyLinkedQueue<>();
myLinkedQueue.push(1);
myLinkedQueue.push(2);
myLinkedQueue.push(3);
myLinkedQueue.push(4);
System.out.print("最初队列-> ");
myLinkedQueue.print();
System.out.print("队首-> ");
System.out.println(myLinkedQueue.peek());
System.out.print("弹出元素-> ");
System.out.println(myLinkedQueue.pop());
System.out.print("弹出一个元素后的队列-> ");
myLinkedQueue.print();
myLinkedQueue.pop();
myLinkedQueue.pop();
System.out.print("弹出三个元素的队列-> ");
myLinkedQueue.print();
}
}
class MyLinkedQueue<E>{
private class QueueNode {
public E e;
public QueueNode next;
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
public QueueNode getNext() {
return next;
}
public void setNext(QueueNode next) {
this.next = next;
}
public QueueNode(E e, QueueNode next) {
this.e = e;
this.next = next;
}
public QueueNode(E e) {
this(e, null);
}
public QueueNode() {
this(null, null);
}
@Override
public String toString() {
return e.toString();
}
}
private int size;
private QueueNode dummyHead;
private QueueNode head;
private QueueNode rear;
public MyLinkedQueue(){
dummyHead = new QueueNode(null,null);
size = 0;
head = dummyHead;
rear = dummyHead;
}
public boolean isEmpty() {
return size == 0||(head == null && rear == null);
}
public void push(E e){
QueueNode queueNode = new QueueNode(e,null);
if (size == 0) {
head = queueNode;
rear = queueNode;
}else {
rear.next = queueNode;
rear = rear.next;
}
size++;
}
public Object peek() throws Exception {
if(isEmpty())
new EmptyStackException();
Object temp = head.getE();
return temp;
}
public E pop(){
if (isEmpty()){
throw new RuntimeException("队列空,无法再弹出");
}
QueueNode temp = head;
if (size == 1) {
head = dummyHead;
rear = dummyHead;
} else {
head = head.next;
}
size--;
return (E) temp.getE();
}
public void print(){
QueueNode start = head;
while (start != null){
System.out.print(start.getE()+"|");
start = start.next;
}
System.out.println();
}
}
第二题 循环队列
public class CircularQueueTest {
public static void main(String[] args) {
CircularQueue<Object> circularQueue = new CircularQueue<>();
circularQueue.push(1);
circularQueue.push(2);
circularQueue.push(3);
circularQueue.push(4);
circularQueue.push(5);
circularQueue.push(6);
circularQueue.push(7);
circularQueue.push(8);
circularQueue.push(9);
circularQueue.print();
circularQueue.pop();
circularQueue.pop();
circularQueue.pop();
circularQueue.pop();
circularQueue.pop();
circularQueue.pop();
circularQueue.pop();
circularQueue.pop();
circularQueue.pop();
circularQueue.print();
circularQueue.push(1);
circularQueue.push(1);
circularQueue.push(1);
circularQueue.push(1);
circularQueue.push(1);
circularQueue.print();
}
}
class CircularQueue<E>{
private static final int QUEUE_CAPACITY = 10;
private Object[] data;
private int head;
private int rear;
public CircularQueue(){
data = new Object[QUEUE_CAPACITY];
head = 0;
rear = 0;
}
public void push(E e){
if ((rear+1) % QUEUE_CAPACITY == head){
throw new RuntimeException("队列已满");
}
rear = (rear + 1) % QUEUE_CAPACITY;
data[rear] = e;
}
public E pop(){
if (rear == head) {
throw new RuntimeException("队列为空");
}
head = (head + 1) % QUEUE_CAPACITY;
return (E) data[head];
}
public void print(){
if (rear == head) {
System.out.println("空队列");
return;
}
for (int i = 1; i <= (rear - head + QUEUE_CAPACITY) % QUEUE_CAPACITY ; i++){
System.out.print(data[(i+head) % QUEUE_CAPACITY] + " ");
}
System.out.println();
}
}