Binary Search Code . Int bfind (int * a, int Len, int Val) { Int M = Len/2; Int L = 0; Int r = Len; While (L! = M & R! = M) { If (A [m]> Val) { R = m; M = (m + l)/2; } Else if (a [m] <Val) { L = m; M = (m + r)/2; } Else Return m; } Return-1; // not found } Write the code to find the number of occurrences of the substring in the parent string. Int count1 (char * STR, char * s) { Char * S1; Char * S2; Int COUNT = 0; While (* Str! = '\ 0 ') { S1 = STR; S2 = s; While (* S2 = * S1 & (* S2! = '\ 0') & (* S1! = '0 ')) { S2 ++; S1 ++; } If (* S2 = '\ 0 ') Count ++; STR ++; } Return count; } Locate the first matched substring. If the return value is S1 length, len1 indicates that no substring is found. Size_t find (char * S1, char * S2) { Size_t I = 0; Size_t len1 = strlen (S1) Size_t len2 = strlen (S2 ); If (len1-len2 <0) return len1; For (; I { Size_t M = I; For (size_t J = 0; J { If (S1 [m]! = S2 [J]) Break; M ++; } If (j = Len) Break; } Return I} Write a quick sort or a sortAlgorithmCode Quick sorting: Int partition (int * a, int L, int R) { Int I = L-1, j = r, V = A [R]; While (1) { While (A [++ I] While (A [-- J]> V) if (j <= I) break; If (I> = J) Break; Swap (A [I], a [J]); } Swap (A [I], a [R]); Return I; } Void qsort (int * a, int L, int R) { If (L> = r) return; Int I = partition (A, L, R ); Qsort (A, L, I-1 ); Qsort (A, I + 1, R ); } Bubble Sorting: Void buble (int * a, int N) { For (INT I = 0; I { For (Int J = 1; J { If (A [J] { Int temp = A [J]; A [J] = A [J-1]; A [J-1] = temp; } } } } Insert sorting: Void insertsort (int * a, int N) { Int key; For (Int J = 1; J { Key = A [J]; For (INT I = J-1; I >=0 & A [I]> key; I --) { A [I + 1] = A [I]; } A [I + 1] = key; } } Appears frequently Implement strcmp Functions Int strcmp11 (char * l, char * r) { Assert (L! = 0 & R! = 0 ); While (* l = * R & * l! = '\ 0') l ++, r ++; If (* L> * r) Return 1; Else if (* l = * r) Return 0; Return-1; } String flip Void reserve (char * Str) { Assert (STR! = NULL ); Char * P1 = STR; Char * P2 = str-1; While (* ++ P2); // generally, strlen cannot be used. P2-= 1; While (P1 { Char c = * P1; * P1 ++ = * P2; * P2 -- = C; } } reverse struct list_node { list_node (int A, list_node * B): Data (A), next (B) // for test convenience, {}< br> int data; list_node * Next; }; void reserve (list_node * phead) {< br> list_node * P = phead-> next; If (P = NULL | p-> next = NULL) return; // only the first node or one node list_node * P1 = p-> next; P-> next = NULL; while (P1! = NULL) {< br> P = p1-> next; P1-> next = phead-> next; phead-> next = p1; P1 = P; }< BR >} TestProgram: List lt; Lt. phead = new list_node (0, 0 ); Lt. phead-> next = new list_node (1, 0 ); Lt. phead-> next = new list_node (2, 0 ); Lt. phead-> next = new list_node (3, 0 ); Lt. Reserve (); List_node * P = lt. phead; While (P) { Cout <data <p = p-> next; } Switches and deletes nodes in the linked list. // bidirectional loop list_node * earse (list_node * node) {< br> // If (node = rear) return node-> next; // you can or cannot judge the header node. It is best to add list_node * Next = node-> next; next-> Prev = node-> Prev; node-> Prev-> next = next; delete node; retrun next; }< br> // single cycle list_node * earse (list_node * node) {< br> // If (node = rear) return node-> next; // you can or cannot judge the header node. It is best to add list_node * P = rear; while (p-> next! = Node) P = p-> next; P-> next = node-> next; delete node; retrun p-> next; } convert a numeric string to a number. "1234" --> 1234 int AtoII (char * s) {< br> assert (s! = NULL); int num = 0; int temp; while (* s> '0' & * S <'9 ') {< br> num * = 10; num + = * s-'0'; S ++; }< br> return num; }< br> frequent occurrences . Add or multiply Integers of any length. Void bigadd (char * num, char * STR, int Len) { For (INT I = Len; I> 0; I --) { Num [I] + = STR [I]; Int J = I; While (Num [J]> = 10) { Num [j --]-= 10; Num [J] + = 1; } } } . Write the function to copy the memory. Void * memcpy (void * DST, const void * SRC, unsigned int Len) { Register char * D; Register char * s; If (LEN = 0) Return DST; If (DST> SRC) // consider Overwriting { D = (char *) DST + len-1; S = (char *) SRC + len-1; While (LEN> = 4) // expand the loop to improve execution efficiency { * D -- = * s --; * D -- = * s --; * D -- = * s --; * D -- = * s --; Len-= 4; } While (Len --) { * D -- = * s --; } } Else if (DST <SRC) { D = (char *) DST; S = (char *) SRC; While (LEN> = 4) { * D ++ = * s ++; * D ++ = * s ++; * D ++ = * s ++; * D ++ = * s ++; Len-= 4; } While (Len --) { * D ++ = * s ++; } } Return DST; } Appears frequently Compile the constructor, destructor, and value assignment functions of the string class. The prototype of the known string class is: Class string { Public: String (const char * STR = NULL); // common Constructor String (const string & other); // copy the constructor ~ String (void); // destructor String & operate = (const string & other); // value assignment function PRIVATE: Char * m_data; // used to save strings }; Answer: // Common Constructor String: string (const char * Str) { If (STR = NULL) { M_data = new char [1]; // score point: An empty string is automatically applied for storing the end sign '\ 0'. // Extra points: determines if m_data is added with null * M_data = '\ 0 '; } Else { Int length = strlen (STR ); M_data = new char [Length + 1]; // It is better if null can be added. Strcpy (m_data, STR ); } } // String destructor String ::~ String (void) { Delete [] m_data; // or delete m_data; } // Copy the constructor String: string (const string & Other) // score point: the input parameter is of the const type. { Int length = strlen (other. m_data ); M_data = new char [Length + 1]; // points for adding null to m_data Strcpy (m_data, other. m_data ); } // Value assignment function String & string: operate = (const string & Other) // score point: the input parameter is of the const type. { If (this = & Other) // score point: Check auto-assigned values Return * this; Delete [] m_data; // score point: Release the original memory resource Int length = strlen (other. m_data ); M_data = new char [Length + 1]; // points for adding null to m_data Strcpy (m_data, other. m_data ); Return * This; // score point: return the reference of this object } Analysis: The interviewer who can accurately compile string class constructor, copy constructor, assign value function and destructor has at least 60% of the basic C ++ skills! This class includes the pointer class member variable m_data. When the class includes pointer class member variables, you must reload the copy constructor, value assignment function, and destructor, this is not only a basic requirement for C ++ programmers, but also a special clause in Objective C ++. Implement strcpy Char * strcpy (char * strdest, const char * strsrc) { Assert (strdest! = NULL) & (strsrc! = NULL )); Char * address = strdest; While (* strdest ++ = * strsrc ++ )! = '\ 0 '); Return address; } Write a function to shift n loops of a char string to the right. For example, if "abcdefghi" is n = 2, it should be "hiabcdefgh" after the shift" The function header is as follows: // Pstr is a pointer to a string ending with '\ 0' // Steps is the n that requires moving Void loopmove (char * pstr, int steps) { // Fill in... } Answer: Answer 1: Void loopmove (char * pstr, int steps) { Int n = strlen (pstr)-steps; Char TMP [max_len]; Strcpy (TMP, pstr + n ); Strcpy (TMP + steps, pstr ); * (TMP + strlen (pstr) = '\ 0 '; Strcpy (pstr, TMP ); } Correct answer 2: Void loopmove (char * pstr, int steps) { Int n = strlen (pstr)-steps; Char TMP [max_len]; Memcpy (TMP, pstr + N, steps ); Memcpy (pstr + steps, pstr, N ); Memcpy (pstr, TMP, steps ); } |