High-level language + data structure finishing

  1. There are 37 keywords in C language. C language identifiers start with letters, and underscores count as letters.

  2. ASCII a 97 A 35 toupper(int c) tolower(int c)

  3. Output format %8.1f 8 refers to the occupied width, 1 refers to the number of digits after the decimal point

  4. The outermost dimension of the array declaration does not need to specify the number of elements in this dimension

  5. The end of the C language string is \0 (ASCII code is 0)

  6. #define statement without semicolon

  7. strcmp compares two strings lexicographically

    strcpy assigns a string to another character array (a string variable)

  8. Operator precedence is associative from right to left

  9. The array cannot be assigned as a whole, and the array name is a pointer constant, which always points to the first address of the array

  10. struct type reference

    struct date{
          
          
        int year;
        int month;
        int day;
    }
    struct date d;
    typedef struct date Date;
    Date dd;
    
  11. Direct member reference: pw

    Indirect member reference: p->w

  12. C does not allow other operations on structure variables, such as various overall operations, overall input and output, etc.

  13. Pointer as a parameter can change the specific value of the actual parameter

  14. document:

    • At any time, a sequential file can only be in one of two modes, read mode or write mode
    • Sequential files can only be operated sequentially. For reading, you can only read sequentially from the first component of the file. For writing, you can only write one component after another at the end of the file.
    • C files are all treated as byte streams and processed in units of bytes. This type of file is called a stream file.
    • C file operations are all implemented through library functions defined by the system
    • Angle brackets only search for the specified file in the default directory, double quotes are now searched in the file directory where the current source file is located, and if not found, search in the default directory
    • EOF: -1, customarily means end of file or file operation failure
    • NULL: 0, it is customary to indicate failure to open the file, etc.
  15. character reading and writing

    int fgetc(FILE * fp);//读字符 ASCII码,EOF失败
    int fputc(int ch,FILE *fp);//写字符,EOF失败
    
  16. String reading and writing

    char *fgets(char *str,int num,FILE *fpointer);//读取字符串放入str字符数组中,遇到换行或者文件结束符结束,或读到n-1还未结束,读入结束后需要加入字符串结束符“\0”,返回值str所致字符数组首地址,NULL则失败
    int *puts(char *str,FILE *fpointer);//将str所指字符串不包含字符串结束符写入所指的文件中,返回值非负则成功,EOF则失败
    
  17. format read and write

    int fscanf(FILE *fp,char *format,arg_list);//将fp所指向的文件按format规定的格式进行转换,读取arg_list对应的数据
    int fprintf(FILE *fp,char *format,arg_list);//返回值实际输出的参数个数,一个负数则失败
    
  18. Fast data read and write

    int fread(void *buf,int size,int count,FILE *fp);//读取到buf中,读取后,将读写指针移动size*count个字节,返回值实际读取的字段个数则成功,EOF则错误
    int fwrite(void *buf,int size,int count,FILE *fp);//将buf所指向的数据写到fp所指的文件中
    
  19. file location

    void rewind(FILE *fp);//是fp所指向的文件的位置指针重新指向文件开始
    int fseek(FILE *fp,long offset,int origin);//fp指向origin+offset的位置上,返回值0成功,非零失败
    long int ftell(FILE *fp);//给出fp所指向文件的位置指针当前所处的位置,返回值为-1(EOF)是失败
    
    • SEEK_SET 0 file start
    • SEEK_CUR 1 The current position of the file
    • SEEK_END 2 end of file
  20. The break statement and the continue statement are both restricted goto statements

  21. The break statement is the innermost while, do, for, and switch statement that includes it to terminate execution and immediately transfer to a program point outside the terminated statement

    The continue statement is to terminate the execution of the innermost while, do, for statement loop body, skip the loop body to the next code, and then continue the execution of the loop. The simple understanding is to terminate this loop, and the rest of the loop needs to continue to execute

  22. The length of the array pointed to by the pointer variable pointing to the array must be the same as the number of columns of the two-dimensional array a

  23. The difference between structs and unions

    • The total space size of the structure is equal to the total length of each member, and the shared body space is equal to the space occupied by the largest member

    • Unions cannot be initialized while structs can

    • Each member of the structure is used to represent the attributes of a specific transaction, and members of the union can represent multiple attributes (the same storage space can store different types of data)

    • A union cannot be used as an argument type of a function and as a result type of a function

    • struct Book{
              
              
          char title;
          int i;
      } book;
      int main (){
              
              
          struct Book book;
          printf("%zu",sizeof(book));
      }
      
      //8
      
    • union Book{
          char title;
          int i;
      } book;
      int main (){
          union Book book;
          printf("%zu",sizeof(book));
      }
      //4
      
    • The automatic storage class and the register storage class belong to the automatic storage method
    • The external storage class and the static storage class belong to the static storage method
    • The type definer is used to define the type name and does not allocate storage space
  24. User area: heap area, stack area, library program code area, static storage area, user program code area

    System area: system programs and data

  25. Local variables can be declared as registers, automatic (default), static

  26. Static local variables are initialized only once when the program starts to execute, and the scope is the same as that of automatic variables

  27. External variables, int x in the declared file, do not add extern, add extern int x in the used file, you can use the same variable

  28. When calling an external function defined in a gaseous source program file in a source program file, it must be described in the source program file with the function prototype and prefixed with extern

  29. #undef terminates the scope of a macro definition

  30. #define MAX(x,y) x>y?x:y

  31. " "First search in the current source file directory, if not found, use the default directory to search

    < > aims to find the specified file in the default directory, the default directory is determined by the user when configuring the programming environment

  32. Conditional compilation will compile the entire source program, and the generated object code program is very long, while using conditional compilation, only a certain segment of the program can be compiled according to the conditions, and the generated object program is shorter.

  33. Cattelan number. That is to say, there are a total of: h(n)=c(2n,n)/(n+1) legal popping sequences

Frequently Asked Questions

The relationship and difference between AOV and AOE

Common Algorithm Topics

Find the height of the tree

Using a recursive method, if it is empty, return 0, otherwise, calculate the height of the left subtree, calculate the height of the right subtree, and return the maximum height of the left and right subtrees + 1;

Find the number of nodes in the tree

Adopt the hierarchical traversal algorithm of the tree, and add 1 to the number of nodes when leaving the queue

Determine if an undirected graph is a tree

Conditions: connected, acyclic,

Determine if an undirected graph has a cycle

Undirected graphs can use depth-first search to find out whether there is a cycle. When the next neighbor of the current node being searched (the parent vertex of the current vertex is not counted) has been visited, there will be a cycle. It can be realized by simply modifying the code of recursive DFS to determine whether there is a cycle in the current graph. Add a father array

Judging that a directed graph has a cycle

Use the topological sequence to judge whether there is a cycle. If you can output a topological sequence containing all vertices, it proves that there is no cycle, otherwise there is a cycle.

maximum spanning tree, minimum spanning tree

Delete the last node of the first sequence, not applicable to stack and recursion

Keep going down to the right, if it is empty, then go to the lower left corner, if they are all empty, then return to the node p, which is the last node

Find the number of leaf nodes of a specified layer node in a binary tree

Determine whether there is a path of length k between any two given vertices in the undirected graph, and all satisfying points

int visited[MAXSIZE]
//出发点为i,终点为j,长度为k 
int exist_path_len(ALGraph G,int i,int j,int k) 
{
    
    
	if(i==j&&k==0)
		return 1;
	else if(k>0)
	{
    
    
		visited[i]=1;
		for(p=G.vertices[i].firstarc;p;p=p->nextarc)
		{
    
    
			int temp=p->adjvex;
			if(!visited[temp]&&exist_path_len(temp,j,k-1))
				return 1; 
		} 
		visited[i]=0;
//这里需要把已经访问的点重新置为0,因为如果当前不存在长度为k
//到达j点,那么这个点还是可以使用的,因为有可能从其他点出发
//可以到达j点并且长度为k 
	} 
	return 0;
}


Determine what is a complete binary tree

Algorithm for finding the parent node of a node x in a binary tree

Find the level of the node in the binary tree

int getNodeLevel(BTNODE *bt, BTNODE *p)
{
    
    
    if (bt == NULL)
        return 0;
    else
    {
    
    
        if (bt == p)
            return 1;
        int L = getNodeLevel(bt->lchild, p);
        int R = getNodeLevel(bt->rchild, p);
        if (L || R)
            return 1 + (L > R ? L : R);
        else
            return 0;
    }
}

Add new edges and find a new minimum spanning tree

Find the Connected Components of an Undirected Graph

Count the layers of the binary tree

private int countLevel(TreeNode root){
    
    
        if(root == null){
    
    
            return 0;
        }
        return Math.max(countLevel(root.left),countLevel(root.right)) + 1;
}

Using queues and hashmaps is similar to hierarchical traversal, except that when entering the queue, it is like a hashmap

public static int getMaxWidth2(Node head) {
    
    
    if (head == null) {
    
    
        return 0;
    }
    //当前层最后一个结点
    Node curEnd = head;
    //下一层最后一个结点
    Node nextEnd = null;
    //当前层结点数
    int curLevelNodes = 0;
    //结点数最多的层的结点数
    int max = Integer.MIN_VALUE;
    Queue<Node> queue = new LinkedList<>();
    queue.add(head);
    while (!queue.isEmpty()) {
    
    
        head = queue.poll();
        curLevelNodes++;
        if (head.left != null) {
    
    
            queue.add(head.left);
            nextEnd = head.left;
        }
        if (head.right != null) {
    
    
            queue.add(head.right);
            nextEnd = head.right;
        }
        if (head == curEnd) {
    
    
            max = Math.max(max, curLevelNodes);
            curLevelNodes = 0;
            curEnd = nextEnd;
            nextEnd = null;
        }
    }
    return max;
}

Two linked lists intersect to find the intersection point

insert image description here

Recursively determine whether a binary tree is symmetric

public static boolean isSymmetrical(BinaryTree root1, BinaryTree root2) {
    
    
        if (root1 == null && root2 == null) {
    
    
            return true;
        }
        if (root1 == null || root2 == null) {
    
    
            return false;
        }
        if (root1.val != root2.val) {
    
    
            return false;
        }
        //判断A的左边和B的右边是否相等,判断A的右边和B的左边是否相等,都相等就满足
        return isSymmetrical(root1.left, root2.right) && isSymmetrical(root1.right, root2.left);

output all paths

Guess you like

Origin blog.csdn.net/weixin_44904205/article/details/123933475