Course notes on set, data structure (what is data structure, logical structure and storage (physical) structure, operation of data structure) and underlying implementation array

Posted by broomstick on Mon, 10 Jan 2022 19:42:06 +0100

Pointer JAVA training day_ sixteen

Set:

Encapsulate a variety of data structures
Array linked list stack queue binary tree

Data structure:

I What is a data structure

1. In 1968, Professor Donald Ervin Knuth of the United States opened a course on basic algorithms, which was the first in data structure

Data structure: study the relationship between data rather than calculation methods

Program = data structure + algorithm - > worth, won Turing Award

2. Basic concepts of data structure
Data: all symbols that can be input into the computer to describe things
Data element: the basic unit of data, also known as node, node, vertex and record
Data item: the smallest unit of data with independent meaning, also known as field

Data structure: a collection of data elements and data relationships
Algorithm: the function of data structure and the method to solve specific problems

II Logical structure and storage (physical) structure

Logical structure:
Set: data elements belong to the same group, but there is no relationship between elements
Linear structure: there is a one-to-one relationship (table) between data elements
Tree structure: there is a one to many relationship between data elements (upside down tree)
Schema structure: there is a many to many relationship between data elements (map)

Physical (memory) structure:
Sequential structure (array)
Arrays are stored in a continuous memory space and are represented by the relative positions of data elements

Chain structure (linked list)
Data elements are stored in independent memory space. Each independent element is also called a node. A data item is added to each data element to store the addresses of other elements and represent the relationship between elements

Relationship between logical structure and physical structure:
Table - > sequential chain
Tree - > sequential chain
Figure - > sequence + Chain

III Operation of data structure

Create
Increase
Delete
Change
Check
Insert
Traversal
Sort

Sequential structure:
Underlying implementation: array

package data;

import java.util.Arrays;

/*
    Underlying implementation: array
 */
public class Demo01 {
    private static Object[] arr = new Object[5];
    private static int size=0;
    private static int index=0;


    //1. Add an element to the end of the set (this)
    public  void add(Object obj){

        if(size==arr.length){
            arr = Arrays.copyOf(arr,arr.length+1);
        }
        arr[size]=obj;
        size++;
    }

    // 2. Insert the obj element into the position of the index set
    public void add(Object obj,int index){
        if(index>size-1&&index<0){
            return ;
        }

        if(size==arr.length){
            arr = Arrays.copyOf(arr,arr.length+1);
        }
        for (int i = arr.length-1; i > index; i--) {
            arr[i]=arr[i-1];
        }
        arr[index]=obj;
    }

    //3. Delete the element at the specified position (index) and return the deleted element
    public void delete(int index){
        for (int i = index; i < size ; i++) {
            arr[i]=arr[i+1];
        }
        //size--;
    }

    //4. Delete the first specified element (obj)
    public void delete1(Object obj){

        for (int i = 0; i < size; i++) {
            if(arr[i].equals(obj)){
//                for (int j = i; j <size ; j++) {
//                    arr[j]=arr[j+1];
//                }
                delete(i);
                size--;
                break;
            }

        }
    }

    //5. Replace the element at the specified position with obj, and return the replaced element
    public void replace(Object obj,int index){
        for (int i = 0; i < size; i++) {
            if(i==index){
                arr[i]=obj;
            }
        }
    }

    // 6. Get the element at the specified position (index) from the collection
    public void get(int index){
        for (int i = 0; i < size; i++) {
            if(i==index){
                System.out.println(arr[i]);
            }
        }
    }

    //7. Get the number of elements in the set
    public int getLength(){
        return size;
    }

    //8. Judge whether the specified element obj exists in the set
    public boolean contains(Object obj){
        for (int i = 0; i < size; i++) {
            if(arr[i]==obj){
                return true;
            }

        }
        return false;
    }

    //9. Judge whether the set is empty: no valid element is empty
    public boolean isEmpty(){
        if(size==0){
            return true;
        }
        return false;
    }
    //  10. Print out the valid elements in the set
    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < size; i++) {
            stringBuilder.append(arr[i]);
            if(i<size-1){
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]");

        String s = stringBuilder.toString();

        return s;
    }


}

‚Äč

Topics: Java