数据结构与算法(一)2021.7.9


前言

数据结构和算法概述
数据结构和算法的关系
1)数据data结构(structure)是一门研究组织数据方式的学科,有了编程语言也就有了数据结构。学好数据结构可以编写出更加漂亮,更加有效率的代码。
2)要学习好数据结构就要多多考虑如何将生活中遇到的问题,用程序去实现解决.
3)程序=数据结构+算法
4)数据结构是算法的基础,换言之,想要学好算法,需要把数据结构学到位


提示:以下是本篇文章正文内容

一、数据结构分类

数据结构是算法的基础,一些简单问题可以靠数据结构来解决,复杂问题则需要数据结构+算法的联合来解决。
那数据结构有哪些呢?

数据结构包括 线性结构 和 非线性结构

  • 线性结构

    1. 线性结构作为最常用的数据结构,其特点是数据元素之间存在一对一的线性关系
    2. 线性结构有两种不同的存储结构,即顺序存储结构(数组)链式存储结构(链表)
    3. 顺序存储的线性表称为顺序表,顺序表中的存储元素是连续的
    4. 链式存储的线性表称为链表,链表中的存储元素不一定是连续的,元素节点中存放数据元素以及相邻元素的地址信息
    5. 线性结构常见的有:数组、队列、链表和栈。
  • 非线性结构

    1. 非线性结构包括:二维数组,多维数组,广义表,树结构,图结构。

二、数据结构第一部分:稀疏数组和队列

1.稀疏数组

在这里插入图片描述

那么稀疏数组是什么呢?是什么规则?怎么定义呢?

答:第一行数据记录总行总列 以及有多少值需要记录
其他行记录值的位置关系 以及值内容
如图:

在这里插入图片描述
在这里插入图片描述

实现代码

package com.java.zzw;

/**
 * @author 赵志文
 * @date 2021/8/9
 */
public class SparseArray {
    public static void main(String[] args) {
        //定义初始二维数组
        int origin[][] = new int[11][11];
        //初始化数据
        origin[3][4] = 1;
        origin[5][2] = 2;
        origin[1][5] = 1;

        System.out.println("输出原始二维数组!");
        for (int[] ints : origin) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }

        //创建稀疏数组,需要得到原数组有多少非0记录的个数,所以我们来遍历原数组
        int sum = 0;
        for (int i = 0; i < origin.length; i++) {
            for (int j = 0; j < origin[i].length; j++) {
                if (origin[i][j] != 0){
                    sum++;
                }
            }
        }
        System.out.println("一共检测出有"+sum+"个要保存的数据");

        //接下来根据sum来创建稀疏数组
        int sparseArray[][] = new int[sum+1][3];
        sparseArray[0][0] = origin.length;
        sparseArray[0][1] = origin[0].length;
        sparseArray[0][2] = sum;

        //接下来遍历二维数组,将稀疏数组补全,
        int account = 0;
        for (int i = 0; i < origin.length; i++) {
            for (int j = 0; j < origin[i].length; j++) {
                if (origin[i][j] != 0){
                    account++;
                    sparseArray[account][0] = i;
                    sparseArray[account][1] = j;
                    sparseArray[account][2] = origin[i][j];
                }
            }
        }

        System.out.println("好的,现在稀疏数组出来了,打印它");
        //好的,现在稀疏数组出来了,打印它
        for (int[] ints : sparseArray) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }
        
        //将稀疏数组转回来
        System.out.println("将稀疏数组转回来");
        int next[][] = new int[sparseArray[0][0]][sparseArray[0][1]];
        for (int i = 1; i < sparseArray.length; i++) {
            next[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
        }

        for (int[] ints : next) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }
    }
}

输出原始二维数组!
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	1	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	1	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	2	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
一共检测出有3个要保存的数据
好的,现在稀疏数组出来了,打印它
11	11	3	
1	5	1	
3	4	1	
5	2	2	
将稀疏数组转回来
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	1	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	1	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	2	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	

稀疏数组读写操作(磁盘)

代码如下:

//读取到磁盘中
        //获取file对象
        File file = new File("D:\\map.data");
        //文件写入流
        FileWriter fileWriter = new FileWriter(file);
        for (int[] ints : sparseArray) {
            for (int anInt : ints) {
                fileWriter.write(anInt+"\t");
            }
            fileWriter.write("\n");
        }
        fileWriter.close();

        //还原原始数组
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = "";
        int[][] next2 = null;
        int row = 0;

        while ((line = reader.readLine()) != null){
            String[] split = line.split("\t");
            int[] ints = new int[split.length];
            for (int i = 0; i < ints.length; i++) {
                ints[i] = Integer.parseInt(split[i]);
            }
            row++;

            if (row == 1){
                next2 = new int[ints[0]][ints[1]];
            }else {
                next2[ints[0]][ints[1]] = ints[2];
            }
        }
        reader.close();

        System.out.println("将存入磁盘的稀疏数组转换成二维数组");
        for (int[] ints : next2) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }

3.队列

  1. 队列是一个有序列表,可以用数组或是链表来实现。
  2. 遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出
  3. 示意图: (使用数组模拟队列示意图)
    在这里插入图片描述

数组模拟队列思路

  • 队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如上图,其中maxSize 是该队列的最大容量。

  • 因为队列的输出、输入是分别从前后端来处理,因此需要两个变量front 及rear分别记录队列前后端的下标,front会随着数据输出而改变,而rear则是随着数据输入而改变,如上图所示.

  • 当我们将 数据存入队列时称为”addQueue”,addQueue 的处理需要有两个步骤:思路分析

    1. 将尾指针往后移: rear+1,当front== rear [空]
    2. 若尾指针rear 小于队列的最大下标maxSize-1,则将数据存入rear 所指的数组元素中,否则无法存入数据。rear ==maxSize- 1[队列满]
package com.java.zzw;


import java.util.Scanner;

/**
 * @author 赵志文
 * @date 2021/8/9
 */
public class ArrayQueueDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //测试
        //创建一个队列
        ArrayQueue queue = new ArrayQueue(3);
        char key = ' '; //用于接收用户的输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        while(loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);  //接收一个字符
            switch(key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数:");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    //取出数据可能会有异常,所以用try-catch捕获异常
                    try {
                        int res = queue.getQueue();
                        System.out.printf("取出的数据是%d\n",res);
                    }catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    //查看队列头的数据,同样需要捕获异常
                    try {
                        int res = queue.headQueue();
                        System.out.printf("队列头的数据是%d\n",res);
                    }catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出!!!");
    }

}

//使用数组模拟队列----编写一个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;  //指向队列头部,分析出front是指向队列头的前一个位置
        rear = -1;   //指向队列尾,指向队列尾的数据(就是队列的最后一个数据)
    }

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

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

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

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

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

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



总结

内容:学习了稀疏数组和队列
归纳:数据结构则是指数据的组织形式;数据结构就是用来减少存储空间,或者说,按一定的规则来使得实现某种功能更加方便,提高运行效率。

因为数据结构不同,程序的运行速度可能相差多个数量级。如果你写的程序要处理大量的数据,或者要让数千人同时使用,那么你采用何种数据结构,将决定它是能够运行,还是会因为不堪重负而崩溃。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值