Data structure -- C-like language operation supplement

Keywords: data structure

Element type description

Sequence table type definition

typedef strut {            //Define structure
   ElemType date[];       //Array for storing data elements
   //ElemType is generic and represents the type of element. It can be changed as needed during actual input
   //Example: when the data elements are A, B, C and D, the ElemType is replaced by char
   //Example: when representing univariate polynomial coefficients, ElemType is replaced by float
   //Example: when defining the sequence table of univariate polynomials, the data elements include coefficients and exponents. First define a structure type. See Chapter 2 for the detailed code
   int length;            //Current length
}SqList;                  //Sequence table type

Definition of array

※ static allocation of array in C language

typedef strut {           
    ElemType date[MaxSize];//Array to store the address of the first element, that is, the base address
    int length;            //Integer variable
}SqList;                   //Sequence table type

※ dynamic array allocation in C language

typedef strut {           
    ElemType *date;         //Pointer variable, representing array
    //This method requires a dynamic memory allocation function
    //Sqlist L; L is the sequence table. L.data stores the data elements and L.Length stores the number of elements in the current sequence table
    //L.date=(ElemType*)malloc(sizeof(ElemType) * MaxSize)
    //(ElemType *) means cast to the required type
    //malloc(m): open up an address space of M bytes and return the first address of this space (the parameter should be an integer)
    //sizeof(x): calculates the length of the variable x
    //free(p): release the storage space of the variable indicated by pointer p, that is, completely delete a variable
    //To use the memory dynamic allocation function, you need to load the header file: < stdlib. H >
    int length;            //Integer variable
}SqList;                   //Sequence table type

Dynamic storage allocation in C + +

new Type name T(Initial value list)
  //int *p = new int;  Or int *p = new int(10);
  //Function:
  //  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
  //  Failed: 0 (NULL)

To free the memory pointed to by the pointer p, use the delete pointer p. p must be the return value of the new operation.

Parameter passing in C + +

  • When calling a function, the arguments passed to the formal parameters must be consistent with the three formal parameters,
    That is, the type, number and order are consistent.

  • There are two ways to pass parameters:

    • Value transfer method (parameters are integer, real, character, etc.)
    • Address:
      ① Parameter is pointer variable;
      ② Parameter is reference type;
      ③ The parameter is an array name.

Value transfer mode

The value of the argument is passed to the corresponding copy of the function's local workspace, which is used by the function to perform the necessary functions. The function modifies the value of the copy, and the value of the argument remains unchanged.

#include <iostream.h>

void swap(float m,float n)   //m. N is a formal parameter
{
    float temp;
    temp = m;
    m = n;
    n = temp;                //Exchange the values of m and n
}

void main()
{
    float a,b;               //Define two arguments a and B
    cin>>a>>b;
    swap(a,b);               //Pass a to m and b to n
    cout<<a<<endl<<b<<endl;  //The value of ab did not change
}

Address transfer mode - pointer variable as parameter

Case 1: parameter changes affect arguments

#include<iostream.h>

void swap(float *m,float *n)//Passed to two pointer variables
{
    float t;                //Define real variables
    t = *m;                 //Exchange what the pointer variable refers to
    *m = *n;
    *n = t;
}

void mian()
{
    float a,b,*p1,*p2;
    cin>>a>>b;
    p1 = &a;             //p1 points to a
    p2 = &b;             //p1 points to b
    swap(p1,p2);         //The calling function passes two pointers as arguments
    cout<<a<<endl<<endl;
}

Case 2: parameter changes do not affect arguments

#include<iostream.h>

void swap(float *m,float *n)//Passed to two pointer variables
{
    float *t;               //Define pointer variables
    t = m;                 //Swap pointer variables themselves
    m = n;
    n = t;
}

void mian()
{
    float a,b,*p1,*p2;
    cin>>a>>b;
    p1 = &a;             //p1 points to a
    p2 = &b;             //p1 points to b
    swap(p1,p2);         //The calling function passes two pointers as arguments
    cout<<a<<endl<<endl;
}

Address transfer method - array name as parameter

  • The first address of the array is passed
  • Any changes made to the formation parameter group will be reflected in the argument array
#include <iostream.h>    
    void sub(char b[])   //Pass the array of storage addresses. b [] can be replaced by * b
    {
        b[] = "world";   //Reassign the b array
    }

void main(void)          
{
    char a[10] = "hello";//Define a character type a array, which stores a string composed of five characters
    sub(a);              //Using the function, the first address is passed
    cout<<a<<endl;       //a array changed
}

Example: use the array as the parameter of the function, store n integers in the array in reverse order, and input and output are completed in the main function.

#include <iostream.h>
#difine N 10

int max(int a[]);
void main()
{
    int a[10];
    int i,m;
    for(i = 0;i < N;i++)
        cin>>a[i];
    m = max(a);
    cout<<"the max number is: "<<m;
}

int max(int b[])
{
    int i,n;
    n=b[0];
    for(i = 0;i < N;i++)
        if(n < b[i])
            n = b[i]
    return n;
}

Address passing method - reference type as parameter

#include<iostream.h>

void swap(float &m,float &n)//Define two references, m refers to a and N refers to b
{
    float temp;             
    temp = m;               //Operations on m and n are operations on a and b
    m = n;
    n = temp;
}

void mian()
{
    float a,b;
    cin>>a>>b;
    swap(p1,p2);         
    cout<<a<<endl<<endl;
}

Three descriptions of formal parameters of reference types:
(1) Passing a reference to a function has the same effect as passing a pointer, and the formal parameters and arguments change.
(2) The reference type is used as a formal parameter, and no copy of the argument is generated in memory. It operates directly on the argument; When a general variable is used as a parameter, the formal parameter and the actual parameter occupy different storage units, so the value of the formal parameter variable is a copy of the actual parameter variable. Therefore, when the amount of data passed by parameters is large, the time and space efficiency of passing parameters by reference is better than that by general variables.
(3) Although the pointer parameter can also achieve the effect of using reference, it needs to repeat the operation in the form of "pointer variable name" in the called function, which is easy to produce errors and the readability of the program is poor; On the other hand, at the calling point of the calling function, the address of the variable must be used as the argument.

Posted by ram4nd on Fri, 03 Dec 2021 21:07:49 -0800