数据结构和算法Java实现

数据结构和算法

1.稀疏数组和队列

1.稀疏数组

1.应用稀疏数组,保留前面的二维数组

思路:

1.遍历原始二维数组得到有效数据的个数sum

2,根据sum就可以创建稀疏数组spareArr int[sum+1][3]

3.将二维数组的有效数据存入到细数数组

2.将稀疏数组恢复成二维数组

思路

1.先读取稀疏数组第一行,根据第一行的数据,创建原始数组

2.读取全部数据

public class SpareseArray {
   
    public static void main(String[] args) {
   
        //创建一个原始的二维数组11*11
        //0表示没有棋子,1表示黑子,2表示篮子
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] =1;
        chessArr1[2][3] = 2;
        for(int[]row : chessArr1){
   
            for (int data:row){
   
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }

        //将二维数组转稀疏矩阵
        //1.先遍历二维数组得到非零个数
        int sum =0;
        for (int i = 0; i < chessArr1.length; i++) {
   
            for (int j = 0; j < chessArr1[i].length; j++) {
   
                if (chessArr1[i][j]!=0)
                    sum++;
            }
        }
        //2.创建对应的稀疏数组
        int spaceArr[][] = new int[sum+1][3];
        spaceArr[0][0] =11;
        spaceArr[0][1] =11;
        //遍历二维数组,将非零的值存放到稀疏数组
        int count =0;//用于记录
        for (int i = 0; i < chessArr1.length; i++) {
   
            for (int j = 0; j < chessArr1[i].length; j++) {
   
                if (chessArr1[i][j]!=0){
   
                    count++;
                    spaceArr[count][0] =i;
                    spaceArr[count][1]  =j;
                    spaceArr[count][2] = chessArr1[i][j];
                }

            }
        }
        //输出稀疏数组
        System.out.println();
        for (int i = 0; i < spaceArr.length; i++) {
   
            System.out.printf("%d\t%d\t%d\t\n ",spaceArr[i][0],spaceArr[i][1],spaceArr[i][2]);
            System.out.println();

        }
    }
}

2.队列

2.1普通队列
public class ArrayQueueDemo {
    public static void main(String[] args) {

    }

    //使用数组模拟队列-编写一个ArrayQueue类
    class ArrayQueue {
        private int MaxSize;//表示数组的最大容量
        private int front;//队列头
        private int rear;//队列尾
        private int[] arr;

        //创建队列的构造器
        public ArrayQueue(int arrMaxSize) {
            MaxSize = arrMaxSize;
            arr = new int[MaxSize];
            front = -1;//指向队列头部,分析出队列头是指向队列头的前一个位置
            rear = -1;//指向队列尾部(即就是最后一个数据)
        }

        //判断队列是满的
        public boolean isFull() {
            return rear == MaxSize - 1;
        }

        //判断队列是否为空
        public boolean isEmpty() {
            return rear == front;
        }

        //添加数据到队列
        public void addQueue(int n) {
            //判断队列是否满
            if (isFull()) {
                System.out.println("队列满,不能加入数据");
                return;
            }
            rear++;
            arr[rear] = n;
        }

        //获取队列的数据,出队列
        public int getQueue() {
            //判断队列是否为空
            if (isEmpty()) {
                //通过抛出异常
                throw new RuntimeException("队列空");
            }
            front++;
            return arr[front];
        }

        //显示队列的所有数据
        public void showQueue() {
            if (isEmpty()) {
                System.out.println("队列空");
                return;
            }
            for (int i = front + 1; i < rear; i++) {
                System.out.printf("arr[%d]=%d\n", i, arr[i]);
            }

        }

        //显示队列头数据
        public int headQueue() {
            if (isEmpty()) {
                throw new RuntimeException("队列空的");
            }
            return arr[front + 1];
        }
    }
}

2.2环形队列
//判断队列满:(rear+1)%maxsize == front
//判断队列空: rear==front
//有效数据个数(rear+maxsize-front)%maxsize 
//添加操作: arr[rear] =n;rear = (rear+1)%maxsize
//判断为空:int value = arr[front];front=(front+1)%maxSize;return value; 

2.单链表

import java.security.PublicKey;

/**
 * @author lizhaoxuan
 * @data 2020/3/8
 */
public class SingleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "松江", "及时雨");
        HeroNode hero2 = new HeroNode(2, "松江1", "及时雨1");
        HeroNode hero3 = new HeroNode(3, "松江2", "及时雨2");
        HeroNode hero4 = new HeroNode(4, "松江3", "及时雨3");
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addByOrder(hero1);
        singleLinkedList.addByOrder(hero4);
        singleLinkedList.addByOrder(hero2);
        singleLinkedList.addByOrder(hero3);
        singleLinkedList.addByOrder(hero2);

        singleLinkedList.list();

    }
}
//定义SingleLinkedList 管理我们的英雄
class  SingleLinkedList{
    //先初始化一个头节点,不存放任何数据
    private HeroNode head = new HeroNode(0,"","");
    //添加节点到单链表,不考虑编号顺序
    public void add(HeroNode heronode){
        HeroNode temp =head;
        while (true){
            if (temp.next==null)
                break;
            temp =temp.next;
        }
        temp.next = heronode;

    }
    public void addByOrder(HeroNode heroNode){
        HeroNode temp = head;
        boolean flag = false;
        while (true){
            if (temp.next==null){
                break;
            }
            if (temp.next.no>heroNode.no)
                break;
            else if (temp.next.no == heroNode.no){
                flag = true;
                break;

            }
            temp = temp.next;
        }
        if (flag)
            System.out.println("is exist!");
        else{
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }
    //修改节点的信息

    //显示链表
    public void list(){
        if (head.next==null){
            System.out.println("list is null");
            return;
        }
        HeroNode temp= head.next;
        while (true){
            if (temp==null)
                break;
            System.out.println(temp);
            temp = temp.next;
        }

    }
    //修改节点信息,根据no判断
    public void update(HeroNode newheroNode){
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        boolean flag = false;
        while (true){
            if (temp==null)
                break;
            if (temp.no==newheroNode.no){
                //找到
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if (flag){
            temp.name = newheroNode.name;
            temp.nickname = newheroNode.nickname;
        }
        else{
            System.out.println(未找到);
        }
    }
}
//定义heroNode,每个就是一个结点
class HeroNode{
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname +
                '}';
    }
}

3.双向链表

package com.atguigu.linkedlist;

class DoubleLinkedList {
    private HeroNode2 head = new HeroNode2(0, "", "");

    DoubleLinkedList() {
    }

    public HeroNode2 getHead() {
        return this.head;
    }

    public void list() {
        if (this.head.next == null) {
            System.out.println("链表为空");
        } else {
            for(HeroNode2 temp = this.head.next; temp != null; temp = temp.next) {
                System.out.println(temp);
            }

        }
    }

    public void add(HeroNode2 heroNode) {
        HeroNode2 temp;
        for(temp = this.head; temp.next != null; temp = temp.next) {
        }

        temp.next = heroNode;
        heroNode.pre = temp;
    }

    public void update(HeroNode2 newHeroNode) {
        if (this.head.next == null) {
            System.out.println("链表为空~");
        } else {
            HeroNode2 temp = this.head.next;

            boolean flag;
            for(flag = false; temp != null; temp = temp.next) {
                if (temp.no == newHeroNode.no) {
                    flag = true;
                    break;
                }
            }

            if (flag) {
                temp.name = newHeroNode.name;
                temp.nickname = newHeroNode.nickname;
            } else {
                System.out.printf("没有找到 编号 %d 的节点,不能修改\n", newHeroNode.no);
            }

        }
    }

    public void del(int no) {
        if (this.head.next == null) {
            System.out.println("链表为空,无法删除");
        } else {
            HeroNode2 temp = this.head.next;

            boolean flag;
            for(flag = false; temp != null; temp = temp.next) {
                if (temp.no == no) {
                    flag = true;
                    break;
                }
            }

            if (flag) {
                temp.pre.next = temp.next;
                if (temp.next != null) {
                    temp.next.pre = temp.pre;
                }
            } else {
                System.out.printf("要删除的 %d 节点不存在\n", no);
            }

        }
    }
}

4.单向环形链表

Josephu(约瑟夫环)

/**
 * @author lizhaoxuan
 * @data 2020/3/8
 */
public class Josepfu {
    public static void main(String[] args) {

        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(25);
        circleSingleLinkedList.showBoy();
        circleSingleLinkedList.countBoy(1,2,25);

    }
}
class CircleSingleLinkedList{
    private Boy first = new Boy(-1);
    public void addBoy(int nums ){
        if (nums<1){
            System.out.println("nums的值不正确");
            return;
        }
        Boy curBoy =null;//辅助指针
        for (int i=1;i<=nums;i++){
            Boy boy = new Boy(i);
            if (i==1){
                first =boy;
                first.setNext(first);
                curBoy = first;
            }else{
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy =boy;
            }
        }
    }
    //遍历当前环形链表
    public void showBoy(){
        if (first==null){
            System.out.println("没有任何小孩");
            return;
        }
        Boy curBoy =first;
        while (true){
            System.out.printf("小孩的编号%d\n",curBoy.getNo());
            if (curBoy.getNext()==first)
                break;
            curBoy = curBoy.getNext();
        }
    }
    public void countBoy(int startNo,int countNums,int nums){
        if (first==null||startNo<1||startNo>nums){
            System.out.println("参数输入有误");
            return;
        }
        Boy Helper =first;
        while (true){
            if (Helper.getNext()==first)
                break;
            Helper =Helper.getNext();
        }
        for (int i = 0; i < startNo-1; i++) {
            first =first.getNext();
            Helper = Helper.getNext();
        }
        while (true){
            if (Helper==first)
                break;
            for (int i = 0; i < countNums-1; i++) {
                first =first.getNext();
                Helper =Helper.getNext();
            }
            System.out.printf("小孩出圈%d\n",first.getNo());
            first = first.getNext();
            Helper.setNext(first);
        }
        System.out.printf("最后的小孩%d\n",Helper.getNo());
    }
}
class Boy{
    private int no;//编号
    private Boy next;

    public Boy(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }
}

5. 栈

import java.util.Scanner;

/**
 * @author lizhaoxuan
 * @data 2020/3/8
 */
public class ArrayStackDemo {
   
    public static void main(String[] args) {
   
        ArrayStack stack = new ArrayStack(4);
        String key = "";
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);

        while(loop) {
   
            System.out.println("show: 表示显示栈");
            System.out.println("exit: 退出程序");
            System.out.println("push: 表示添加数据到栈(入栈)");
            System.out.println("pop: 表示从栈取出数据(出栈)");
            System.out.println("请输入你的选择");
            key = scanner.next();
            switch(key.hashCode()) {
   
                case 111185:
                    if (key.equals("pop")) {
   
                        try {
   
                            int res = stack.pop();
                            System.out.printf("出栈的数据是 %d\n", res);
                        } catch (Exception var8) {
   
                            System.out.println(var8.getMessage
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值