Fundamentals of data structure and algorithm - Wang Zhuo

Fundamentals of data structure and algorithm - Wang Zhuo

The first chapter is introduction

1.1.1 basic concepts and terms

  1. data
    • It is a collection of various symbols that can be input into the computer and processed by the computer
  2. data elements
    • The basic unit of data, usually considered and processed as a whole in computer programs

    • The following table is data, and the second row is data elements

    • Also called an element, record, node, or vertex

      classfull nameAge
      23.1Zhang San21
      20.7Wang Wu20
      19.7Li Si22
  3. data item
    • The smallest indivisible unit that constitutes a data element. Three sheets in a table are data items
  4. data object
    • The collection of data elements with the same properties is a subset of data

      For example:

      • Integer data objects are sets N={0, + 1, - 1, + 2, - 2,...}
      • Student status table can also be regarded as a data object, and data elements with the same nature can be regarded as data objects

1.1.2 basic concepts and terms

1. Data structure interpretation
  • The relationship between data elements is called structure
  • A collection of data elements that have one or more specific relationships with each other
2. The data structure includes three aspects
  • The logical relationship between data elements is also called logical structure
  • The representation of data elements and their relationships in computer memory is called data storage structure
  • Data operation and Implementation
3. Two levels of data structure
  • Logical structure

  • storage structure

    3.1 relationship between logical structure and storage structure:

    • The storage structure is the image of the logical relationship and the image of the element itself
    • Logical structure is the abstraction of data structure, and storage structure is the implementation of data structure
    • Combining the two, the structural relationship between data elements is established

    3.1.1 logical structure

(1) linear structure

A precursor, a successor

(2) nonlinear structure

One to many

3.1.2 four storage structures

  • Sequential storage structure
  • Linked Storage Structure
  • Index storage structure (understand)
  • Hash storage structure (understand)

1.1.3 data types and abstract data types

Concept summary:

Chapter II linear table

2.1 class c language operation Supplement 1

2.1.1 supplement: element type description

Sequence table type definition:

	typedef struct{

ElemType data[];

int length;

}SqList;//Sequence table type

Note: ElemType here represents the meaning of element type, which has no practical significance. When the type is known in the actual situation, the following code can be added:

​ typedef char ElemType;// If the element type is known to be char

If the element types in the table are not single, you can split the above code into two pieces in the following way:

typedef struct{

float p;

int e;


typedef struct{

Polynomial *elem;//First address, elem save address

int length;


2.1.2 supplement: array definition
  • Definition method 1: array static allocation

    typedef struct{ 
        ElemType data[MaxSize]; 
        int length;
    }SqList;//Sequence table type
  • Definition method 2: array dynamic allocation (the above elements have multiple types, that is, the dynamic allocation method is used to define the array)

    typedef struct{
        ElemType *data;//Array header address
        int length;
    }SqList;//Sequence table type

    In this way, we don't know how big the memory is. Next, we will use the memory allocation function to allocate space:

    SqList L;//L is the sequence table to be operated, that is, SqList = (ElemType*)malloc(sizeof(ElemType)*MaxSize);
2.1.3 dynamic memory allocation of C language
  • malloc(m) function opens up an address space of M bytes and returns the first address of this space

  • sizeof(x) operation to calculate the length of variable x, that is, the number of bytes to be occupied by the variable * the number of variables

  • 50. Data is the first address of the array (no space). ElemType * makes the element type of the first address of the newly allocated space the element type of the array,

    An "=" can allocate the space for placing array element types to L

  • The free § function releases the storage space of the variable indicated by the pointer p, that is, completely deletes a variable

2.1.4 supplement: dynamic storage allocation of C + + (simpler than dynamic allocation of C)

new type name T (initial value list)


Apply for memory space for storing T-type objects, and assign initial values according to the initial value list

Result value:

Success: pointer of type T to the newly allocated memory

Success: 0 (NULL)

int *p1 = new int;//Or int *p1 = new int(10)

delete pointer P


Free the memory pointed to by the pointer P. P must be the return value of the new operation

2.2 class C language operation Supplement 2

2.1.1 parameter transfer in C + +
  • Value transfer method (parameters are integer, real, character, etc.)

    void swap(float m,float n)
        float temp;
        temp = m;
        m = n;
        n = temp;
    void main()
        float a,b;

    Description: the function modifies the value of the formal parameter. After the space is released, the formal parameter is released, and the value of the actual parameter remains unchanged

  • Transmission address

    • Parameter is a pointer variable

      • Parameter changes affect arguments

        void swap(float *m,float *n)
            float t;
            t = *m;
            *m = *n;
            *n = t;Finger exchange m and n Content referred to
        void main()
            float a,b,*p1,*p2;
            p1 = &a;p2 = &b;//p1 and p2 point to variables a and b respectively
      • Parameter changes do not affect arguments

        void swap(float *m,float *n){
            float *t;
            t = m;
            m = n;
            n = t;//The address pointed to by m and n is exchanged, not the content of the address
        void main()
            float a,b,*p1,*p2;
            p1 = &a;p2 = &b;
    • Parameters are reference variables (references, that is, providing an object with an alternative name)

      void main(){
      int i = 5;
      int &j = i;
      i = 7;//Share the same space, but there are two names i and j at the same time. Therefore, when you change one value, the other value will also change
      cout<<"i = "<<i<<"j"<<j;
    • The parameter is the array name (the first address of the array is passed, and any changes to the shape parameter group will be reflected in the argument array)

      void sub(char b[]){
              b[] = "world";
      void main(void){
          char a[10] = "hello";

2.3 realization of basic operation of linear table

2.3.1 basic operation of linear table
  • Basic operation of linear table

    • InitList(&L)
    • DestroyList(&L)
    • ClearList(&L)
    • ListInsert(&L,i,e)
    • ListDelete & L, i, & E) / / delete the ith position element in linear table L and return it with e
      • IsEmpty(L)
      • ListLength(L)
    • Locaterelem (L, e) / / L finds an element equal to the given value E. if it succeeds, it returns the sequence number of the element in the table; otherwise, it returns 0
    • GetElem (L, i, & E) / / return the ith position element in linear table l to e
  • Supplement: predefined constants and types used in operation algorithms

    //The result of the function is a status code

    #define TRUE 1

    #define FALSE 0

    #define OK 1

    #define ERROR 0

    #define INFEASIBLE -1

    #define OVERFLOW -2

    //Status is the type of function and its value is the result status code of the function

    typedef int Status;

    typedef char ElemType;

2.3.2 realization of basic operation of sequence table
  • Initialization of linear table L (reference for parameters)

    Status InitList_Sq(SqList &L){
        L.elem = new ElemType[MAXSIZE];
        if(!L.elem) exit(OVERFLOW);
        L.length = 0;
        return 0;
  • Search of sequence table

    int LocateElem(SqList L,ElemType e){
        //Find the data element with the value e in the linear table and return its sequence number (which element is the first)
        for(i = 0;i<L.length;i++)
            if(L.elem[i] = e)
                return i+1;
        return 0;
  • Insertion of sequence table

    The idea of the algorithm:

    1. Determine whether the insertion position i is legal
    2. Judge whether the storage space of the sequence table is full. If it is full, ERROR is returned
    3. Move the elements of positions n to i backward one position in turn to vacate the i position
    4. Place the new element e to be inserted in position i
    5. Add 1 to the table length and return OK after successful insertion
    Status ListInsert_SqList(&L,int i,ElemType e){
        if(i<1||i>L.length+1)return ERROR;// 1 i value is illegal
        if(L.length==MAXSIZE)return ERROR;// 2 the current storage space is full
            L.elem[j+1]=L.elem[j];// 3 insert position and subsequent elements move back
        L.elem[i-1]=e;// 4 place the new element e in position i
        L.length++;// 5. Table 1
        return OK;
  • Deletion of sequence table

    Algorithm idea:

    1. Judge whether the deletion position i is legal (the legal value is 1 < = i < = n)
    2. Keep the element to be deleted in e
    3. Move the elements i+1 to n forward one position in turn
    4. If the length of the table is reduced by 1, and the deletion is successful, return to OK
    Status ListDelete Sq(SqList &L,int i){
        if((i<1)||(i>L.length)) return ERROR;// 1 i value is illegal
            L.elem[j-1]=L.elem[j]; // 2 move the element forward after the deleted element
        L.length--; // Table 3 length minus 1
       return 0;

Tags: data structure

Posted on Wed, 13 Oct 2021 23:56:50 -0400 by SNN