Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Experience of learning pointer and linked list in C language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

one. Pointer principle

A pointer is a variable that stores the memory address of a computer. Reading data from the memory that the pointer points to is called the value of the pointer. Pointers can point to specific types of variable addresses, such as int, long, and double. Pointers can also be void types, NULL pointers, and uninitialized pointers.

two. Linked list principle

The linked list is distributed separately by n nodes, each connected by a pointer. Each node consists of a precursor node and a rear drive node, the first node has no precursor node and the tail node has no rear drive node. Since the linked list consists of many nodes, the node consists of two parts: one is the data field, which is used to store valid data; the other is the pointer domain, which is used to point to the next node; the node should be constructed first, and then all the nodes are connected to form the linked list.

# include

Int main ()

{

Int*ptr; / / declare an int pointer

Int val = 1; / / declare an int value

Ptr = & val; / / assigns a reference to the int value to the pointer

Int deref = * ptr; / / A pair of pointers take values and print what is stored in the pointer address

Printf ("deref address =% ld, value =% d\ n", ptr, deref)

In line 2, we declare an int pointer through the * operator. Then we declare an int variable and assign it to 1. Then we initialize our int pointer with the address of the int variable. Next, take the value of the int pointer and initialize the int pointer with the memory address of the variable. Finally, we print out the value of the variable with a content of 1.

The & val in line 6 is a reference. After the val variable declares and initializes memory, we can reference the memory address of the variable directly by using the address operator before the variable name.

In line 8, we once again use the * operator to value the pointer to directly get the data in the memory address that the pointer points to. Because the type declared by the pointer is int, the value fetched is the int value stored at the memory address that the pointer points to.

Here you can compare the relationship between pointers, references, and values to envelopes, mailbox addresses, and houses. A pointer is like an envelope on which we can fill in the mailing address. A reference (address) is like an email address, it is the actual address. The value is like the house corresponding to the address. We can erase the address on the envelope and write down another address we want, but this behavior has no effect on the house.

three. Experience

When we are not learning linked lists, we always use an array if we want to store a large number of data of the same type or structure. For example, if we want to store the scores of a class of students in a certain subject, we always define a float array, but when using an array, there is always a question that bothers us: how big should the array be?

In many cases, you are not sure how big an array to use, such as the above example, you may not know the number of students in the class, so you need to define the array large enough. In this way, your program applies for a fixed amount of memory space that you think is large enough at run time. Even if you know the number of students in the class, if the number increases or decreases for some special reason, you have to modify the program to expand the storage range of the array. This fixed-size memory allocation method is called static memory allocation. However, this method of memory allocation has serious defects, especially when dealing with some problems: in most cases, a lot of memory space is wasted, and in a few cases, when the array you define is not large enough, it may cause subscript out-of-bounds errors, or even lead to serious consequences.

Through this study, I understand how to learn the principle of pointer and linked list, and have a new understanding.

Attachment: student Information Management system

/ *

* Student information management procedures

* manage students' personal information and grades in various subjects

, /

# include

# include

# include

# include

Typedef struct Node Node

/ / define the score information node

/ / Chinese, maths, English and overall grades respectively

Struct Score

{

Int chinese,math,english,sum

}

/ / define the student information node

/ / name, class, student number, grade and pointer to the next node, respectively

/ / four global variables, header node, and temporary node variables are defined.

Struct Node

{

Char name [20], classs [20], number [20]

Struct Score score

Struct Node* next

} * head,*u,*p,*q

/ / define the number of multiple students and the average passing rate of excellence in each subject.

Int n,C,M,E,Cj,Cy,Mj,My,Ej,Ey

Char num [20]

/ / enter the menu function

Void Welcome ()

{

Printf ("\ t\ t #\ n")

Printf ("\ t\ t # Welcome to use the student score management system #\ n")

Printf ("\ t\ t # #\ n")

Printf ("\ t\ t # 1. Read file #\ n")

Printf ("\ t\ t # #\ n")

Printf ("\ t\ t # 2. Save the file #\ n")

Printf ("\ t\ t # #\ n")

Printf ("\ t\ t # 3. Add student grades #\ n")

Printf ("\ t\ t # #\ n")

Printf ("\ t\ t # 4. Modify student grades #\ n")

Printf ("\ t\ t # #\ n")

Printf ("\ t\ t # 5. Delete student grades #\ n")

Printf ("\ t\ t # #\ n")

Printf ("\ t\ t # 6. Query personal scores #\ n")

Printf ("\ t\ t # #\ n")

Printf ("\ t\ t # 7. Query the grade of this class #\ n")

Printf ("\ t\ t # #\ n")

Printf ("\ t\ t # 8. Query the school grade #\ n")

Printf ("\ t\ t # #\ n")

Printf ("\ t\ t # 9. Exit the management system #\ n")

Printf ("\ t\ t # #\ n")

Printf ("\ t\ t #\ n\ n")

Printf ("\ t\ t Please enter instruction: (1-9)")

}

/ / construct node function

Node* new_node (Node* uu)

{

Uu = (Node*) malloc (sizeof (Node))

Uu- > next = NULL

Return uu

}

/ / add student information

Void Add ()

{

/ / create a new node

U = new_node (u)

Printf ("\ nPlease enter the information of the student you want to join:\ n")

Printf ("\ nname:")

Scanf ("% s", u-> name)

Printf ("\ nClass:")

Scanf ("% s", u-> classs)

Printf ("\ nStudent ID:")

Scanf ("% s", u-> number)

Printf ("\ nAchievement in Chinese, maths and English:")

Scanf ("% d%d%d", & u-> score.chinese,&u- > score.math,&u- > score.english)

/ / calculate the total score

U-> score.sum = u-> score.chinese + u-> score.math + u-> score.english

/ / insert the tail pointer of the new node to the second node (break)

U-> next = head- > next

/ / put the new node behind the head node

Head- > next = u

Printf ("\ nMusi-> added successfully!\ n")

}

/ / modify the information according to the student number

/ / like the lookup function, traverse from the second node in turn, if the update is found

Void Mod ()

{

N = 0

Printf ("\ nPlease enter the student number you want to modify:")

Scanf ("% s", num)

For (u = head; u! = NULL;u = u-> next)

{

If (strcmp (u-> number,num) = = 0)

{

N = 1

Printf ("\ nPlease enter new grades in Chinese, maths and English:")

Scanf ("% d%d%d", & u-> score.chinese,&u- > score.math,&u- > score.english)

U-> score.sum = u-> score.chinese + u-> score.math + u-> score.english

Printf ("\ nMusi-> modified successfully!\ n")

Break

}

}

If (! n)

Printf ("\ nMurray-> has no information about this student!\ n")

}

/ / Delete student information according to student number

/ / start traversing from the beginning node, and delete this node if you find it

Void Del ()

{

N = 0

Printf ("\ nPlease enter the student number of the student you want to delete:")

Scanf ("% s", num)

For (u = head; u! = NULL;u = u-> next)

{

If (strcmp (u-> number,num) = = 0)

{

N = 1

P-> next = u-> next

Free (u)

Printf ("\ nMusi-> deleted successfully!\ n")

Break

}

P = u

}

If (! n)

Printf ("\ nMurray-> has no information about this student!\ n")

}

Void Sort ()

{

Int i,j

/ / record the total number of students

N = 0

For (u = head- > next; u! = NULL;u = u-> next)

Nasty +

/ / the bubbling method is used to arrange each node according to the ascending order of the class and the descending order of the total score.

For (iTun1)

If (strcmp (p-> classs,q- > classs) > 0 | | strcmp (p-> classs,q- > classs) = = 0 & & p-> score.sum

< q->

Score.sum)

{

U-> next = Q

P-> next = Q-> next

Q-> next = p

}

U = u-> next

}

}

}

/ / find a student's score by student number

Void Que_One ()

{

/ / Mark variable to record whether the search is successful

N = 0

Printf ("\ nPlease enter the student number of the student you want to query:")

Scanf ("% s", num)

/ / traverse from the second node to the last node

For (u = head- > next; u! = NULL;u = u-> next)

{

/ / if the student number of the current node is the same as that of the student number you are looking for, this student information is output.

If (strcmp (u-> number,num) = = 0)

{

N = 1

Printf ("\ n")

Puts ("Class name, Chinese, Mathematics and English Total score")

Printf ("%-11s%-15s", u-> classs,u- > name)

Printf ("%-6d%-6d%-6d%-6d\ n", u-> score.chinese,u- > score.math,u- > score.english,u- > score.sum)

Break

}

}

If (! n)

Printf ("\ nMurray-> has no information about this student!\ n")

}

Void Analyze_Sco (Node * uu)

{

/ / A pair of nodes found to calculate the average score of each subject.

/ / seek excellence rate and pass rate

C + = uu- > score.chinese

M + = uu- > score.math

E + = uu- > score.english

If (uu- > score.chinese > = 60)

Cj++

If (uu- > score.chinese > = 90)

Cy++

If (uu- > score.math > = 60)

Mj++

If (uu- > score.math > = 90)

My++

If (uu- > score.english > = 60)

Ej++

If (uu- > score.english > = 90)

Ey++

}

/ / print the average passing rate of each subject, the excellent rate

Void Print_Sco ()

{

Printf ("Chinese average score:%-6.2f, passing rate:%-6.2f, excellent rate:%-6.2f.\ n\ n", (float) 100*Cj/n, (float) 100*Cy/n)

Printf ("Math average:%-6.2f, passing rate:%-6.2f, excellence rate:%-6.2f.\ n\ n", (float) 100*Mj/n, (float) 100*My/n)

Printf ("English average:%-6.2f, passing rate:%-6.2f, excellent rate:%-6.2f.\ n\ n", (float) 100*Ej/n, (float) 100*Ey/n)

}

/ / find the information of the students in a certain class

Void Que_Cla ()

{

/ / sort a pair of linked list nodes

Sort ()

N = C = M = E = Cj = Cy = Mj = My = Ej = Ey = 0

Printf ("\ nPlease enter the class you want to query:")

Scanf ("% s", num)

Printf ("\ n")

For (u = head- > next; u! = NULL;u = u-> next)

{

/ / students who are not in this class skip it.

If (strcmp (u-> classs,num))

Continue

/ / print head information if it is the first student

If (! n)

Puts ("Student ID, name, Chinese, Mathematics and English Total score")

Nasty +

Printf ("%-11s%-15s", u-> number,u- > name)

Printf ("%-6d%-6d%-6d%-d\ n", u-> score.chinese,u- > score.math,u- > score.english,u- > score.sum)

Analyze_Sco (u)

}

If (! n)

{

Printf ("No student information for this class!\ n")

Return

}

/ / print the eigenvalues of each grade of the students in this class

Printf ("\ nThere are% d students in this class.\ n\ n", n)

Print_Sco ()

}

/ / print the information of all the students in the school.

/ / the specific situation is the same as printing class student information.

Void Que_All ()

{

Sort ()

N = C = M = E = Cj = Cy = Mj = My = Ej = Ey = 0

Printf ("\ n")

If (head- > next = = NULL)

{

Printf ("--> No student information!\ n")

Return

}

Puts ("Class Student number, name, Chinese, Mathematics and English Total score")

For (u = head- > next; u! = NULL;u = u-> next)

{

Nasty +

Printf ("%-12s%-12s%-15s", u-> classs,u- > number,u- > name)

Printf ("%-6d%-6d%-6d%-d\ n", u-> score.chinese,u- > score.math,u- > score.english,u- > score.sum)

Analyze_Sco (u)

}

Printf ("\ nThere are% d students in the school.\ n\ n", n)

Print_Sco ()

}

/ / Save the file

Void Save ()

{

Char c

Printf ("\ nconfirm save? (y _ N):")

Scanf ("% * c% c" & c)

If (c = ='N')

Return

FILE * fp

If ((fp=fopen ("C:\\ data.txt", "w")) = = NULL)

{

Printf ("\ nMurray-> cannot open the file\ n")

Return

}

/ / write data header information

Fputs ("Class number, name, Chinese, Mathematics and English Total score", fp)

If (head- > next! = NULL)

Fputs ("\ n", fp)

/ / write files in turn from the beginning node

For (u = head- > next; u! = NULL;u = u-> next)

{

Fprintf (fp, "%-11s%-11s%-15s", u-> classs,u- > number,u- > name)

Fprintf (fp, "%-6d%-6d%-6d%-d", u-> score.chinese,u- > score.math,u- > score.english,u- > score.sum)

If (u-> next! = NULL)

Fprintf (fp, "\ n")

}

Fclose (fp)

Printf ("\ nMurray-> score was successfully deposited in C:\ data.txt\ n")

}

/ / read the file

Void Open ()

{

Printf ("\ nPlease put the data in the directory C:\ data.txt and press any key to confirm.\ n")

Getch ()

FILE * fp

/ / read files from the root directory of disk c

If ((fp=fopen ("C:\\ data.txt", "r")) = = NULL)

{

Printf ("\ nMutual-> file not found!\ n")

Return

}

Char tmp [100]

/ / read 65 menu header characters and store them in the tem character array

Fgets (tmp,66,fp)

/ / read to jump out of the loop at the end of the file

While (! feof (fp))

{

U = new_node (u)

Fscanf (fp, "% s%s%s", u-> classs,u- > number,u- > name)

Fscanf (fp, "% d%d%d%d", & u-> score.chinese,&u- > score.math,&u- > score.english,&u- > score.sum)

/ / create a linked list by plug-in

U-> next = head- > next

Head- > next = u

}

Printf ("\ nMutual-> score read successfully!\ n")

Fclose (fp)

}

/ / exit the program

Void Exi ()

{

Char c

Printf ("\ nconfirm exit? (y _ N):")

Scanf ("% * c% c" & c)

If (c = ='N')

Return

/ / print the concluding remarks

System ("cls")

Printf ("\ n\ n")

Printf ("\ t\ t\ t% c% c\ n")

Printf ("\ t\ t\ t% c Thank you for using% c\ n", 4jin4)

Printf ("\ t\ t\ t% c% c\ n")

Printf ("\ t\ t\ t Thank you!\ n\ n\ n")

Exit (0)

}

Int main ()

{

/ / variables that store instructions

Int orz

/ / set the system text color

System ("color 0B")

/ / create a new student information header node

Head = new_node (head)

While (1)

{

/ / display menu,

Welcome ()

/ / receive user commands,

Scanf ("d", & orz)

/ / call the system function to clear the screen

System ("cls")

Switch (orz)

{

/ / enter the corresponding menu options according to the instructions

Case 1:Open (); break

Case 2:Save (); break

Case 3:Add (); break

Case 4:Mod (); break

Case 5:Del (); break

Case 6:Que_One (); break

Case 7:Que_Cla (); break

Case 8:Que_All (); break

Case 9:Exi (); break

Default: printf ("\ nMutual-> invalid instruction!\ n")

}

Printf ("\ n")

/ / execute system functions

System ("pause")

System ("cls")

}

Return 0

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report