Sparse array with the annular array

Relational data structures and algorithms

Data structure (data structure) is a discipline Research Organization data mode, with the programming language and will have a data structure. You can write the code to learn the data structure with a beautiful home, more efficient

To learn data structures necessary to consider how a lot of problems encountered in daily life, with the program to a settlement

Algorithms + data structures = programs

Data structure is the basis of the algorithm, in other words, you want to learn algorithms, data structures need to be put in place to learn

Data structure comprising: linear and nonlinear structural configuration

Linear structure:
  1. As the linear structure most commonly used data structures, which is characterized by the presence of one linear relationship (a [0] = 30) between data elements
  2. There are two different linear structure storage structure , i.e. sequential storage structure and chain storage structure. Linear table stored in the sequence table sequence referred to, the storage order of the elements in the table is continuous
  3. Linear chain address information table stored in the list referred to, the storage elements of the list are not necessarily consecutive, the data elements stored in the node element and an adjacent element
  4. Common linear structure: array, queues, linked lists, and stacks
Nonlinear structure

Nonlinear structure comprising: a two-dimensional array, a multidimensional array, the generalized table, the tree structure, the structure of FIG.

Sparse array

When most of the elements in an array is 0, or a value of the same array, the sparse array can be used to hold the array

Sparse array of treatment methods are:

  1. An array of records, a total of several odd row, how many different values
  2. The ranks of the value and worth of records with different elements in an array of small-scale, thereby reducing the size of the program

Two-dimensional array to a sparse array

  1. Traversing the original two-dimensional array, the number of valid data obtained sum
  2. Is a sum + 1, 3 sum fixed number of columns can be created according to the number of rows of sparse array sparseArr
  3. The two-dimensional array of valid data stored in the sparse array

Turn the raw sparse array of two-dimensional data

1. First, read the first line of sparse array, according to the data of the first line, creating an original two-dimensional array,

2. In the data after reading a few lines of sparse arrays, and assigned to the original two-dimensional data

package demo1;

public class SparseArray {
    public static void main(String[] args) {
        //二维数组,数组里面装了数组
        //定义一个行列为11的二维数组,第一个[]代表行,第二个代表列
        int chessArr1[][] = new int[11][11];
        //0:表示没有棋子,1表示黑子,2表示蓝子
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[3][4] = 2;
//      System.out.println(chessArr1[1][2]);
        //遍历数组,原始二维数组
        for(int i=0;i<chessArr1.length;i++) {
            for(int j=0; j<chessArr1[i].length;j++) {
                System.out.print(chessArr1[i][j]+"  ");
            }
            System.out.println();
        }
        //将二维数组转为稀疏数组的思路
        //1.先遍历二维数组 得到非0数据的个数
        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=sum+1;
                }
            }
        }
        //2.创建对应的稀疏数组
        //给稀疏数组赋值,
        int sparseArr[][] = new int[sum+1][3];
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;
        // 遍历二维数组,将非0的值存放到sparseArr中
        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++;
                    //给sparseArr数组赋值
                    //第一个稀疏数组的值放在第一行
                    //所以需要递增
                    sparseArr[count][0]=i;//稀疏数组的第一列存储的是行数
                    sparseArr[count][1]=j;//稀疏数组的第二列存储的是列数
                    sparseArr[count][2]=chessArr1[i][j];//稀疏数组的第三列存储的是值
                }
            }
        }
        System.out.println("稀疏数组--------");
        for(int i=0;i<sparseArr.length;i++) {
            for(int j=0; j<sparseArr[i].length;j++) {
                System.out.print(sparseArr[i][j]+"  ");
            }
            System.out.println();
        }
        //把稀疏数组转换成二维数组
        //定义新的二维数组,行数应该为稀疏数组的第一列,列数为第二列
        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
        //定义好的二维数组,把稀疏数组转换为二维数组
        //稀疏数组sparseArr[i][0]对应是chessArr2的行
        //稀疏数组sparseArr[i][1]对应的是chessArr2的列
        //sparseArr[i][2];对应的是稀疏数组的值
        for(int i=1;i<sparseArr.length;i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]]=sparseArr[i][2];
        }
        //遍历二维数组
        System.out.println("稀疏数组转二维数组");
        for(int i=0;i<chessArr2.length;i++) {
            for(int j=0; j<chessArr2[i].length;j++) {
                System.out.print(chessArr2[i][j]+"  ");
            }
            System.out.println();
        }
    } 
}

img

queue

A queue is an ordered list, you can use an array or a linked list to achieve

Follow the FIFO principle, the data stored in the first queue, the first removed, after the stored data after removal.

An annular array of analog queue implementation

A queue full condition (rear + 1)% maxSize == front [full]

The number of valid data in the queue (rear + maxSize - front)% maxSize

package demo1;

import java.util.Scanner;

public class CircleArrayQueueDemo {

    public static void main(String[] args) {
        CircleArray queue = new CircleArray(4);
        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):查看队列头的数据");
            System.out.println("请输入您的操作");
            key = scanner.next().charAt(0);
            switch(key) {
            case 's':
                queue.showQueue();
                break;
            case 'e':
                scanner.close();
                loop=false;
                break;
            case 'a':
                System.out.println("请输入你要添加的数据");
                int i = scanner.nextInt();
                 queue.addQueue(i);
                break;
            case 'g':
                try {
                    int res = queue.getQueue();
                    System.out.println("取出的数据是"+res);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            case 'h':
                try {
                    int res = queue.getQueue();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            default:
                break;
            
            }
        }
        
        System.out.println("程序退出");

    }

}

class CircleArray{
    private int maxSize;//数组最大的容量
    private int front;//front指向队列的第一个元素初始值为0
    private int rear;//rear 指向队列的最后一个元素的后一个位置,rear初始值为0
    private int[] arr;//该数据用于存放数据,模拟队列
    //创建队列的构造器
    public CircleArray(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }
    //判断队列是否满
    public boolean isFull() {
        return (rear+1) % maxSize == front;
    } 
    //判断队列是否为空
    public boolean isEmpty() {
        return front == rear;
    }
    //添加数据到队列
    public void addQueue(int n) {
        //判断队列是否满
        if(isFull()) {
            System.out.println("队列满,不能加入数据");
        }
        arr[rear]=n;//rear指针在最后元素的后一位
        rear=(rear+1)%maxSize;
    }
    //获取队列的数据,出队列
    public int getQueue() {
        //取出队列,先判断是否为空,为空,不能取
        if(isEmpty()) {
            throw new RuntimeException("队列空,不能取");
        }
        int value = arr[front];
        front=(front+1)%maxSize;//front后移
        return value;
    }
    //显示队列的所有数据
    public void showQueue() {
        if(isEmpty()) {
            System.out.println("队列空的,没有数据");
            return;
        }
        for(int i = front; i< front +size();i++) {
            System.out.printf("arr[%d]=%d\n",i%maxSize,arr[i%maxSize]);
        }
    }
    public int size() {
        return(rear + maxSize - front) % maxSize;
    }
    //显示队列的头数据
    public int headQueue() {
        if(isEmpty()) {
            System.out.println("队列空的,没有数据");
            throw new RuntimeException("队列空的,没有数据");
        }
        return arr[front];
    }
}

Guess you like

Origin www.cnblogs.com/train99999/p/11094469.html