数据结构作业5

第一题 数组底层 队列

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;
    }
        /**
        *@Description 入队算法
        *@Param
        *@Return
        *@Author Mr.Lu
        *@Date 2021/5/26
        *@Time 8:35
        */
    public void push(E data){
        if (isFull()){
            throw new RuntimeException("队列已满,入队失败");
        }
        rear++;
        this.data[rear] = data;
    }

        /**
        *@Description 判断队列是否已满
        *@Param
        *@Return
        *@Author Mr.Lu
        *@Date 2021/5/26
        *@Time 8:35
        */
    public boolean isFull(){
        return this.rear + 1 >= data.length;
    }

    public boolean isEmpty(){
        return this.head == this.rear;
    }
        /**
        *@Description 出队算法
        *@Param
        *@Return
        *@Author Mr.Lu
        *@Date 2021/5/26
        *@Time 8:37
        */
    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<>();
        //插入4个数据
        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);
    }

    /**
     *@Description 参数是一个元素 方法体内新建一个Queuenode 添加在rear(队尾指针)指针之后 rear指针移动
     *@Author Mr.Lu
     *@Date 2021/5/17
     *@Time 20:20
     */

    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++;
    }

    /**
     *@Description 获得队首元素
     *@Return Object
     *@Author Mr.Lu
     *@Date 2021/5/17
     *@Time 20:21
     */
    public Object peek() throws Exception {
        if(isEmpty())
            new EmptyStackException();
        Object temp = head.getE();
        return temp;
    }
        /**
        *@Description 弹出方法
        *@Param 
        *@Return 
        *@Author Mr.Lu
        *@Date 2021/5/26
        *@Time 22:06
        */
    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<>();
        //push 9个元素进去
        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();
        //弹出9个
        circularQueue.pop();
        circularQueue.pop();
        circularQueue.pop();
        circularQueue.pop();
        circularQueue.pop();
        circularQueue.pop();
        circularQueue.pop();
        circularQueue.pop();
        circularQueue.pop();
        circularQueue.print();
        //此时再push 如果是普通队列 会显示已满 但是循环队列不会
        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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、查找 1. 算法设计题 :已知n元顺序表a0, a1, … , an-1按关键字递增有序存储。给定关键字值key,编写算法用对分查找求下标i,满足ai-1<key且aikey。 2. 编程题:输入n个两两互不相等的整数,以这些整数为关键字建立平衡的二叉排序树。判断该二叉树是否为平衡的,输出判断结果;输出该二叉树的中序遍历关键字访问次序。 3. 从空树起连续插入以下20个关键字构建m=4的B-树。 50, 15, 09, 18, 03, 85, 33, 72, 48, 22, 91, 88, 11, 99, 06, 56, 68, 77, 43, 36。 4. 16个关键字组成的5阶B-树如下图所示,请按关键 字递减的次序删除所有结点至空树,画出每删除1个关键字后得到B-树,直至空树。 5. 12个关键字如本电子教案例1所示,设H(K)=K mod 13,地址空间范围0~15,用二次探测再散列解决冲突。画出哈希表;若各元素等概率查找,求成功查找时的平均查找长度。 二、内部排序 1.算法设计与分析题:将直接插入排序的内循环改造为使用对分查找实现元素插入,请写出基于对分查找的插入排序算法并给出其时间复杂度分析。 2.算法设计:将教案给出的非递归直接插入排序和冒泡排序算法用递归算法实现。 3.算法设计:带附加头结点单链表将各数据结点按关键字升序连接。 4.编程题:键盘输入n个无符号整数,用链式基数排序实现由小到大排序,输出排序结果。 提示:对于C语言32bit宽的unsigned类型,可以采用16进制形式来实现基数排序,即32bit共有8个16进制位,每个16进制位进行一趟分配和收集,共8趟。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值