C language structure part - knowledge points and test questions
Structural body is an important data structure that has a wide range of applications in practice. The requirements of the computer secondary examination pair of structural parts are: (1) definition method of structural type data and reference methods. (2) Conformal, output, deletion, and insertion of the one-way linked list with pointers and structures. The following is summarized and analyzed on these basic knowledge points and related questions, and I hope to help the exam.
First, basic knowledge
(1) Definition of structures
Struct Structural Number, for example: Struct Student
{{
Members list Char Name [20];
Variable table; int Age;
CHAR SEX;
} Stu1, STU2;
Note: After the structure is defined, don't forget the semicolil!
It can also be defined as follows: Struct Student Stu1, STU2;
Definition of structural arrays: struct student x [10];
(2) Access to members of the structure
Both ways: Direct access. Such as: STU1. Agn
Use a pointer access. First define a pointer to the structure: struct student * p;
Then you can pass: (* p). Member variable or P-> member variables.
(3) About a single list
First understand two functions:
Memory allocation function Malloc
Such as: int * p; p = (int *) malloc (sizeof (int)); assigns an integer size memory space.
Note: Malloc has no reference, and allocate memory to enforce type conversion.
Memory release function free
Free (want to release the address of the memory);
For details on the chain table, please refer to Tan Haoqiang's "C Programming (Second Edition)" 11.7 (P273)
Focus on basic ideas and related algorithms, in fact, the procedures in the exam have no difficulties at all. Here I want to say, focus on the order of the pointer movement when the linked list is inserted and deleted. Pay attention to the preservation and placement of the pointer. (The preservation of the header pointer and the layout of the pointer to the position of the pointer). This is the focus of the exam, I believe I am not wrong.
Second, typical test questions and analysis
(1) The result of the following procedure is _______
#include "stdio.h"
Main ()
{struct date
{Int year, month, day;
} Today;
Printf ("% D / N", SIZEOF (STRUCT DATE));
}
A.6 B.8 C.10 D.12
Analysis: This topic examines the number of digits of different variables in memory. Now summarize (for TC2.0 versions): CHAR type variable accounts for 1 byte; INT variable accounts for 2 bytes; long and float variables account for 4 bytes; Double type variables account for 8 bytes.
This topic defines three INT variables, so choose A.
This type of calculation is based on the number of bytes, usually combined with the consortium. Please see the case:
The variable A accounts for the number of bytes is _______
Union u
{char ST [4];
INT I;
Long L;
}
Struct a
{INT C;
Union u u;
} a;
The answer should be 6. Note: The complex allocation memory is allocated in accordance with the number of variables that are most memory. Instead of all variables account for the amount of memory. Special Note: In actual programming, the system does not assign memory to the member variable of the structure before the structural type variable is not defined.
(2) The following program output is _______
Struct stu
{INT X;
INT * Y;
} * p;
INT DT [4] = {10, 20, 30, 40};
Struct Stu a [4] = {50, & DT [0], 60, & DT [1], 70, & DT [2], 80, & DT [3]};
Main ()
{P = a;
Printf ("% d,", p-> x); // statement 1
Printf ("% d,", ( p) -> x); // statement 2
Printf ("% d / n", (* p-> y)); // statement 3
}
A.10, 20, 20 B.50, 60, 21 C.51, 60, 21 D 60, 70, 31
Analysis: This type of entitled access to the structural member variables and the priority of the operator. It should be noted that "à" priority is greater than " " and "*".
In the statement 1, P-> X is executed first, and then 50 after 50, and the result 51 is obtained.
In the statement 2, first ( P), after the pointer moves, the value of the member variable x is 60.
In the statement 3, first obtain the value of * p-> y, that is, the value of DT [1], then , last 21.
聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽
The answer is 50, 70, 31. Think about it. There are several such level exercises below, which can be enhanced.
1. The following statement is provided:
Struct ST
{INT N;
Struct St * next;
}
Static struct st a [3] = {5, & a [1], 7, & a [2], 9, '/ 0'}, * p;
P = & a [0];
Then the following expression of 6 is ________
A. P -> n B. P-> n C. (* p). N D. P -> N
2. The following statement is equipped, the value in the following expression is 3 _______
Struct S
{INT A1;
Struct S * a2;
}
Static struct s a [3] = {1, & a [1], 2, & a [2], 3, & a [0]}, * PTR;
PTR = & a [1];
A. PTR -> A1 B. Ptr -> A1 C. * PTR -> A1 D. PTR -> A1
3. If there is a statement, the value of the following expression is 1002 ______
Struct student
{Int Age;
Int Num;
}
Struct Student Stu [3] = {{1001, 20}, {1002, 19}, {1003, 21}}; struct student * p;
P = STU;
A. (p ) -> Num B. (p ) -> AGE C. (* P). Num D. (* ). Agn
4. If there is any instructions and statements:
Struct student
{Int Age;
Int Num;
} STD, * P;
P = & std;
Then the following is incorrectly incorrectly incorrectly incorrect to the member AGE in Structural Variables STD_______
A. std. AGE B. P -> AGE C. (* P). AGE D. * P. AGE
The answers to the above four are d.
(3) If the following linked table structure is established, the pointers P, Q correspond to the indication points shown in the graph, and cannot insert the nodes referred to in Q, a set of statements at the end of the chain table is _________
A. Q-> Next = NULL; P = P-> Next; P-> Next = q;
B. P = P-> Next; Q-> Next = P-> next; p-> next = q;
C. P = P-> Next; Q-> Next = P; P-> Next = q;
D. p = (* p). Next; (* Q). Next = (* p). Next; (* p). Next = q;
Analysis: This question is the insertion and deletion of the list mentioned earlier. Key understanding of the linkage and deletion order of the node.
Successful order of both pointers. Be cautious when doing this treatment, beware of the loss of junction. This question is C.
(4) The functionality implemented by the following procedure is to remove the node of the specified candidate number in the defined candidates. Please fill in the fill in the program.
Struct Student * Delete (Head, Num);
Struct student * head;
Long Num;
{struct student * p1, * p2;
IF (head == null)
{Printf ("/ nlist null! / N");
Goto end;
}
P2 = HEAD;
While ((Num! = P2 -> NUM) && (_________ [1] _________))
{P1 = p2; p2 = p2-> next;}
IF (Num == P2 -> NUM)
{
IF (p2 == hEAD) head = p2-> next
ELSE ________ [2] ___________;
Printf ("delete:% ld / n", num);
N = n - 1;
}
Else Printf ("% LD Not Found! / N", NUM);
End:
Return (HEAD);
}
Analysis: The deletion of the chain list is generally as follows:
1. First, it is judged whether or not the linked list of the node is empty. If it is empty (head == null), the blanking table information is output.
2. Set two pointer variables P1 and P2 so that P2 points to head pointer head.
3. If the deleted node is the first node in the list, P-> Next should assign a value to the Head.
4. If it is not the first node to delete, the P2 points to the next node. When the operation, the P2 is assigned to P1, and then the P-> NEXT is assigned to P2. In this way, the P1 and P2 are rearward and compared, until the node to delete is found. 5. When you find the node you want to delete, you want to complete the operation of the delete node. At this time, P2-> Next assignments to P -> Next.
Answer: [1] p2-> Next! = Null [2] p1-> Next = p2-> Next
About the linked list also has insert, input, and output, please summarize it.
The list of the chain is mainly understood by the operation process, and the title is not a lot, but there is a certain difficulty. It can be said that the highest difficulty of the secondary exam is the mixing application of the linked list and pointer. Therefore, the pointer portion must be quite clear. In short, you must test the high score, these two parts must be taken!
In fact, this chapter is called "structural and common body". However, there are not many topics in the common body (generally one or two), mainly test the memory distribution problem, and often mixed with structural body. Therefore, be sure to remember the characteristics of the common body, so that the test will be more easy.
In the end, please see a special test "common body" topic:
It is known to definition of the following common numerals:
UNION U_TYPE
{
INT I;
CHAR CH;
} TEMP;
The value of "TEMP. I = 266", TEMP. CH is now ()
A. 266 B. 256 C. 10 D. 1
I think this question can be said to be the most difficult results of the common body. Of course, it can be done, and it doesn't matter if you don't do it. (To be honest, I didn't make it in the first time. ^ _ ^) Oh, yes, the answer is C.
Ok, this part will first talk about it, if there are other problems, welcome to discuss!