ArticleDirectory
- 1. program the creation, length measurement, and printing of a single-chain table.
- 2. Program to delete a node from a single-chain table.
- 3. Write a program to insert a single-chain table.
- 4. program the sorting of Single-Chain tables.
- 5. Write and implement reverse placement of a single-chain table.
- 6. Program to delete the Header element of a single-chain table.
- 7. A single-chain table is provided. If you do not know the value of node N, you can retrieve the intermediate node and write an algorithm.
- 8. Given a one-way linked list, design a time-optimized and space-optimized algorithm to find the M-last element of the linked list. Implement your algorithms and handle relevant errors. M is defined as when m = 0, return the last element of the linked list.
Next, I will introduce the data structure, the second important section in the C ++ interview question, which mainly includes the use of strings, heap, stack, and sorting methods. Let's take a look at the single-chain table.
1. program the creation, length measurement, and printing of a single-chain table.
Answer:
# Include <iostream> using namespace STD; // The typedef struct student {int data; struct student * Next;} node; // create a single-chain table node * Create () {node * head, * P, * s; int X, cycle = 1; head = (node *) malloc (sizeof (node )); // create a header node P = head; while (cycle) {printf ("\ nplease input the data:"); scanf ("% d", & X ); if (X! = 0) {S = (node *) malloc (sizeof (node); // create a node S-> DATA = x; printf ("\ n % d ", s-> data); P-> next = s; P = s;} else {cycle = 0;} head = head-> next; P-> next = NULL; printf ("\ n YYY % d", head-> data); Return (head);} // the length of a single-chain table. Int length (node * head) {int n = 0; node * P; P = head; while (P! = NULL) {P = p-> next; n ++;} return (n) ;}// a single-chain table prints void print (node * head) {node * P; int N; n = length (head); printf ("\ Nnow, these % d records are: \ n", n); P = head; If (Head! = NULL) P = p-> next; while (P! = NULL) {printf ("\ n uuu % d", p-> data); P = p-> next ;}}
2. Program to delete a node from a single-chain table.
Resolution:If you delete a header node, for example:
Point the head pointer to the next node of the header node. Free P1 at the same time, as shown in:
If an intermediate node is deleted, as shown in:
The next of P2 points to the next OF P1 at the same time, free P1, as shown in:
Answer:
// Delete node * remove (node * head, int num) {node * P1, * P2; P1 = head; while (num! = P1-> Data & P1-> next! = NULL) // find the node whose data is num {P2 = p1; P1 = p1-> next;} If (num = p1-> data) // If the num node exists, delete {If (p1 = head) {head = p1-> next; free (P1 );} else {P2-> next = p1-> next;} else {printf ("\ n % d cocould not been found", num);} return (head );}
3. Write Program Insert a single-chain table.
Resolution:Insert a single-chain table, as shown in:
If it is inserted before the header node, next of P0 points to P1, And the header node points to P0, as shown in:
If an intermediate node is inserted, as shown in:
Let P2 point next to P0 and P0 point to P1, as shown in:
If the last node is inserted, as shown in:
Let P1 point next to P0 and P0 point to null, as shown in:
Answer:
// Insert node * insert (node * head, int num) into a single-chain table {node * P0, * P1, * P2; P1 = head; p0 = (node *) malloc (sizeof (node); P0-> DATA = num; while (P0-> DATA> P1-> Data & P1-> next! = NULL) {P2 = p1; P1 = p1-> next;} If (P0-> data <= p1-> data) {If (Head = p1) {P0-> next = p1; head = P0;} else {P2-> next = P0; P0-> next = p1 ;}} else {p1-> next = P0; p0-> next = NULL;} return (head );}
4. program the sorting of Single-Chain tables.
Answer:
// Node * sort (node * head) {node * P, * P2, * P3; int N; int temp; n = length (head ); if (Head = NULL | head-> next = NULL) // if there is only one or no node, return head; P = head; For (Int J = 1; j <n; ++ J) {P = head; For (INT I = 0; I <n-J; ++ I) {If (p-> DATA> P-> next-> data) {temp = p-> data; P-> DATA = p-> next-> data; p-> next-> DATA = temp;} p = p-> next ;}} return (head );}
5. Write and implement reverse placement of a single-chain table.
Resolution:Shows the single-chain table model:
To reverse a single-chain table, first point the next of P2 to P1, as shown in:
Point P1 to P2, and P2 to P3, as shown in:
Let the next repeat P2 point to P1, P1 point to P2, and P2 point to P3.
Answer:
// Node * reverse (node * head) {node * P1, * P2, * P3; If (Head = NULL | head-> next = NULL) return head; P1 = head; P2 = p1-> next; while (P2) {P3 = P2-> next; P2-> next = p1; P1 = P2; p2 = P3;} head-> next = NULL; head = p1; return head ;}
6. Program to delete the Header element of a single-chain table.
Answer:
// Delete the Header element void removehead (node * head) {node * P; P = head; head = head-> next; free (p );}
7. A single-chain table is provided. I don't know the value of node N. How can I retrieve the intermediate node only once and write it out? Algorithm .
Resolution:Set two pointers, for example, * P and * q. P moves two positions each time, that is, P = p-> next. Q moves one position each time, that is, q = Q-> next. When P reaches the last node, Q is the intermediate node.
Answer:
// A single-chain table is provided. I don't know the value of node N. How can I retrieve the void searchmid (node * head, node * mid) {node * P, * q; P = head; q = head; while (p-> next! = NULL) {P = p-> next; q = Q-> next; Mid = Q ;}}
8. Given a one-way linked list, design a time-optimized and space-optimized algorithm to find the M-last element of the linked list. Implement your algorithms and handle relevant errors. M is defined as when m = 0, return the last element of the linked list.
Resolution:This is a problem. What we need is the M-last element. So if we start from an element and traverse M elements, we will just reach the end of the linked list, this element is the element to be searched. Maybe it is not the best way to push back from the end of the linked list, so we can start counting from the linked list header.
Thought 1: We can first traverse the total length N of the linked list, and then find the nth-M element in the sequence variable. Then this is the element we are looking.
Train of Thought 2: We use two pointers, the current position pointer P and the pointer Q pointing to the nth element. We need to ensure that the two pointers differ by M elements, then move them at the same speed. If Q reaches the end of the linked list, the p Pointer Points to the M-last element.
Answer:
// Thought 1 node * searchlastelement1 (node * head, int m) {If (Head = NULL) return NULL; node * P = head; int COUNT = 0; while (P! = NULL) {P = p-> next; count ++;} If (count <m) return NULL; P = head; For (INT I = 0; I <count-m; I ++) {P = p-> next;} return P;} // thought 2 node * searchlastelement2 (node * head, int m) {If (Head = NULL) return NULL; node * P, * q; P = head; For (INT I = 0; I <m; I ++) {If (p-> next! = NULL) {P = p-> next;} else {return NULL;} q = head; while (p-> next! = NULL) {P = p-> next; q-> next;} return Q ;}
This section is complete. The dormitory is powered off and goes to bed ..