Java数据结构

稀疏数组

/*
稀疏数组思路分析:
    一、把非零的数放入一个二维数组中(arr[][3]),
    二维数组存放他的  横向坐标       纵向坐标     值
                   arr[0][0]    arr[0][1]   arr[0][2]
    二、遍历这个二维数组 然后在还原原来的二维数组

*/
package 稀疏数组;

public class SparseArray {
    public static void main(String[] args) {
        int[][] array=ShuZu();
        int sum=0;
//        获取有效数据的个数:
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                if (array[i][j]!=0){
                    sum++;
                }
            }
        }
//        创建一个二维数组来存放数据 行和列 值
        int[][] arr=new int[sum+1][3];
        arr[0][0]=11;
        arr[0][1]=11;
        arr[0][2]=sum;

//        遍历原先的数组,把非0的数放入到稀疏数组中;
        int a =1;
        int b =0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j]!=0){

                   arr[a][b]=i;
                   arr[a][b+1]=j;
                   arr[a][b+2]=array[i][j];
                   a++;
                    }
                }
            }

//       新的数组
        System.out.println("-------------新的的二维数组--------------");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]);
                System.out.print(" ");
            }
            System.out.println();

    }
//    还原二维数组
        int[][] newarray=new int[arr[0][0]][arr[0][0]];
        for (int i = 1; i <arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                newarray[arr[i][0]][arr[i][1]]=arr[i][2];
            }
        }
//        还原后的二维数组
        System.out.println("-----------还原后的二维数组------------");
        for (int i = 0; i < newarray.length; i++) {
            for (int j = 0; j < newarray[i].length; j++) {
                System.out.print(newarray[i][j]);
                System.out.print(" ");
            }
            System.out.println();
        }

    }
    public static int[][] ShuZu(){
        int[][] array=new int[11][11];
        array[1][2]=1;
        array[2][3]=2;
        System.out.println("-------------原始的二维数组--------------");
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]);
                System.out.print(" ");
            }
            System.out.println();
        }
        return array;
    }

}

队列

/*
环形数组思路分析:
     一、定义一个初始值  start  、end 、maxLength、
     二、判断数组是否为空: start==end
     三、判断数组是否已满: (end+1)%maxlength==start
     四、获取数组有效的个数:(end+maxlength-start)%maxlength
     五、添加数据 之前要判断是否已满 end++ ==>> end=(end+1)%maxlength
     六、获取数据 之前要判断是否为空 start++ ==>> start=(start+1)%maxlength
*/
package 队列;

import java.util.Scanner;

public class 数组模拟环形队列 {
    public static void main(String[] args) {
        aa aa = new aa();
        aaa:
        for (; ; ) {
            System.out.println("----------###菜单栏###----------");
            System.out.println("----------1 存数据 -----------");
            System.out.println("----------2 取数据 ----------");
            System.out.println("----------3 显数据 ---------");
            System.out.println("----------4 退出   ------------");
            int bb = new Scanner(System.in).nextInt();
            switch (bb) {
                case 1:
                    aa.add();
                    break;
                case 2:
                    aa.get();
                    break;
                case 3:
                    aa.show();
                    break;
                case 4:
                    System.out.println("退出成功...");
                    break aaa;
                default:
                    System.out.println("你的输入有误...");

            }
        }


    }
}

class aa {
    int[] array = new int[4];
    int maxsize = array.length;
    int start = 0;
    int end = 0;


    //    判断是否为空
    public boolean isempty() {
        return start == end;
    }

    //    判断队列是否已满
    public boolean isfull() {
//        利用取余来判断是否满了
        return (end + 1) % maxsize == start;
    }

    //    添加数据
    public void add() {
        if (isfull()) {
            System.out.println("数据已经满了...");
        } else {
            System.out.println("输入你要存入的数据..");
            int num = new Scanner(System.in).nextInt();
            array[end] = num;
            end = (end + 1) % maxsize;
        }
    }

    //    显示数据
    public void show() {
        if (isempty()) {
            System.out.println("列表为空...");
        } else {
            for (int i = start; i < start + size(); i++) {
                System.out.printf("array[%d]=%d\n", i % maxsize, array[i % maxsize]);
            }
        }
    }

    //    取出数据
    public void get() {
        if (isempty()) {
            System.out.println("你的列表为空,无法取出...");
        } else {
            int n = array[start];
            start = (start + 1) % maxsize;
            System.out.println("取出的数是:" + n);
        }
    }

    //    获取有效数据的个数
    public int size() {
        return (end + maxsize - start) % maxsize;
    }
}

单链表

package 单链表;

import java.util.Stack;

public class 单链表的总结 {
    public static void main(String[] args) {
        AAA a0=new AAA(1,"周浩","帅气");
        AAA a1=new AAA(3,"周浩","帅气");
        AAA a2=new AAA(2,"周浩","帅气");
        AAA a3=new AAA(5,"周浩","帅气");
        AAA a4=new AAA(2,"周浩","帅气");
        AAA a5=new AAA(2,"周浩","帅气");
        AAA a6=new AAA(2,"小浩纸~","ku");
        AAA a7=new AAA(10,"小浩纸~","ku");
        dome dome = new dome();
        System.out.println("------------直接插入的链表的最后--------------");
//        dome.add1(a0);
//        dome.add1(a1);
//        dome.add1(a2);
//        dome.add1(a3);
//        显示数据
//        dome.show();
        System.out.println("------------按顺序插入--------------");
        dome.add2(a0);
        dome.add2(a1);
        dome.add2(a2);
        dome.add2(a3);
        dome.add2(a4);
        dome.add2(a5);
        dome.show();
        System.out.println("------------根据no修改后的数据------------");
        dome.update(a6);
        dome.update(a7);
        dome.show();
        System.out.println("------------根据no删除链表数据------------");
        dome.delete(5);
        dome.delete(1);


//        添加
        dome.add2(a3);
        dome.add2(a7);
        dome.show();
        System.out.println("链接节点的个数共有:"+dome.count());

        System.out.println("显示倒数第 1 个链表-----");

        dome.after(dome.count()-1);
        System.out.println("显示倒数第 2 个链表-----");
        dome.after(dome.count()-2);

        System.out.println("----------链表的反转---------");
        dome.show();
        dome.revirs();
        System.out.println("------------");
        dome.show();

        System.out.println("------------利用栈 来逆序打印 且不破会链表的结构");
        dome.nixu();



    }
}

//创建一个类,表示节点
class AAA{
    public int no;
    private String  name;
    private String nickname;
    public  AAA node;

//    构造函数
    public AAA(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }
//    toString

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

//创建一个类来存放增删改查的方法
class dome{
//    创建一个头节点 不存放任何的数据
    private AAA head=new AAA(0,"","");

//    直接将数据添加到最后
    public void  add1(AAA newnode){
        AAA temp=head;
        while(true){
            if (temp.node==null){
                break;
            }
            temp=temp.node;
        }
        temp.node=newnode;
    }

//    根据no将数据插入
    public void add2(AAA newnode){
        AAA temp=head;
        while(true){
            if (temp.node==null){
                temp.node=newnode;
                break;
            }else if (temp.node.no> newnode.no){
                newnode.node=temp.node;
                temp.node=newnode;
                break;
            }else if (temp.node.no==newnode.no){
                System.out.println("编号重复了...."+temp.node.no);
                return;
            }

            temp=temp.node;
        }
    }

//    显示链表的数据
    public void show(){
        AAA temp=head;
        while(true){
            if (temp.node==null){
                break;
            }
            temp=temp.node;
            System.out.println(temp);
        }
    }

//    修改数据
    public void update(AAA newnode){
        AAA temp=head;
        while(true){
            if (temp.node==null){
                System.out.println("没有找到要修改的数据...");
                return;
            }else if (temp.node.no== newnode.no){
                newnode.node=temp.node.node;
                temp.node=newnode;
                return;
            }
            temp=temp.node;
        }
    }
//    删除数据
    public void delete(int num){
        AAA temp=head;
        while(true){
            if (temp.node==null){
                System.out.println("没有你要删除的数据");
                return;
            }else if (temp.node.no==num){
                temp.node=temp.node.node;
                return;
            }
            temp=temp.node;
        }
    }
//    统计节点的个数 带头节点的后就 不统计头节点
    public int count(){
        AAA temp=head;
        int num=0;
        while(true){
            if (temp.node==null){
                break;
            }
            num++;
            temp=temp.node;
        }
        return num;
    }

//    显示链表中倒数第 k 个
    public void after(int k){
        AAA temp=head;
        int i = 0;
        for (; i <= k; i++) {
            if (temp.node==null){
                System.out.println("没有这个数据...");
                return;
            }
            temp=temp.node;

        }
        if (i==0){
            System.out.println("没有这个数据...");
        }else{
            System.out.println(temp);
        }

    }

//    链表的反转
    public void revirs() {
        AAA temp=head.node;
        AAA flag=null;

        AAA tag = new AAA(0, "", "");
        while(true){
            if (temp==null){
                break;
            }
            flag=temp.node;
            temp.node=tag.node;
            tag.node=temp;
            temp=flag;
        }
        head.node=tag.node;
    }
//    逆序打印链表 不破坏链表的结构
//    利用栈 stack 的特点 ,先入后出
    public void nixu(){
        AAA temp=head.node;
        Stack<AAA> stack=new Stack<>();
        while(true){
            if (temp==null){
                break;
            }
            stack.push(temp);
            temp=temp.node;
        }
        while(stack.size()>0){
            System.out.println(stack.pop());
        }

    }


}

双链表

约瑟夫环

栈,栈实现计算器

前缀表达式

中缀表达式

后缀表达式

逆波兰计算器的实现

递归

迷宫回溯

八皇后

排序算法基础

冒泡排序

选择排序

插入排序

希尔排序

快速排序

归并排序

基数排序

各种排序的比较

二叉排序树

删除一颗树的节点

二叉平衡树

图的深度优先和广度优先

动态规划

暴力匹配和KMP算法

贪心算法

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没有心肝,只有干

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值