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

How to use linked list in C language

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use linked list in C language, which is very detailed and has certain reference value. Friends who are interested must read it!

I. the concept of structure

For example, the student's information, including the student's name, student number, gender, age and other information, some of these parameters may be array type, character type, integer type, or even structure type data. Although these are different types of data, they are all used to express student information.

Second, the usage of the structure

1. Struct structure name access method: structure variable name. Members

{undefined member 1; member 2;}

2 、 typedef struct

{undefined member 1; member 2;} structure name

The second is generally used in medium and large products, because when there are more structures, defining structure variables by aliases can greatly improve the readability of the code.

Structure arrays and pointers

1. Declare a structure directly with struct, and then define the structure array, struct structure name array name [array size]

2. Declare a structure with typedef struct, name the structure weight, and define the structure array by renaming. Structure weight named array name [array size]

IV. Structural pointer

As long as the variable or array or function compiler stored in memory will assign them an address, we can access the number in the address by pointing to this address. as long as the pointer variable is defined as the same data type, for example, to point to a character-type variable, we can also define a structure-type pointer to point to it.

1. Declare a structure directly with struct, and then define the structure pointer, struct structure name * structure pointer variable name

2. Declare a structure with typedef struct, name the structure weight, and define the structure pointer by alias. Structure alias * structure pointer variable name

Structure pointer access member method structure pointer variable name-> member name

5. The structure containing the structure

The student information contains data such as name, student number, gender, date of entry and exit, and the date of birth includes the three members of the year, month and day, so declare the date of birth as a separate structure, then the structure of the student contains the structure of the date of birth. this is the structure that contains the structure.

VI. Linked list

In the data structure, there is a kind of data structure called linked list, which, in popular terms, connects multiple structural variables like chains. We take the linked list composed of four student data as an example to analyze the specific working principle of the linked list.

# include/* can be constructed with structures as long as it has multiple attributes For example, the serial port has a variety of information, clock source, baud rate, stop bit, check bit * / typedef struct {unsigned short Year; unsigned char Mon; unsigned char Day;} today;/* this structure is just constructed, this program does not use * / struct student {unsigned char Name [20]; unsigned char number; unsigned char sex; student * pLast; / * two-way linked list * / student * pNext;} Int main () {unsigned char i; struct student * pStu; struct student stu1 [4] = {{"stu1", 1pNext = & stu1 [1]; stu1 [0] .pNext = & stu1 [1] Stu1 [1] .pNext = & stu1 [2]; stu1 [2] .pNext = & stu1 [3]; stu1 [3] .pLast = & stu1 [2]; stu1 [2] .pLast = & stu1 [1]; stu1 [1] .pLast = & stu1 [0]; pStu = & stu1 [0]; for } pStu = & stu1 [3]; printf ("\ r\ n"); for (iname return pStu- > number,pStu- > sex); pStu = pStu- > pLast;} name 0;}

Linked list is a linear data structure based on structure.

Structures are widely used, and here we remember a golden rule: anything with multiple attributes can be constructed with structures, which we call objects. For example, is the serial port of the single-chip microcomputer sometimes clock source, baud rate, stop bit, parity bit and so on? Then the serial port is an object that can be constructed with a structure.

Static linked list

The above linked list is four structural variables of student information linked together, this is a static linked list, which means that the number of structural variables connected together is fixed, the method of static creation of linked list is suitable for some fixed number of data structures. For example, the product needs to display a multi-level menu structure on the LCD, and generally use keys or touch to enter the submenu or return to the main menu, then these main menus and submenus can be made into a linked list, then with the structure pointer, you can easily find the menu that needs to jump. This kind of menu structure often needs to plan how many menus are needed when the function is defined. Each menu has those common properties.

8. Dynamic linked list

Dynamic linked list means to connect structural variables by means of dynamic allocation. The number of structural variables is unknown and will increase or decrease with certain conditions. For example, I want to receive data from the serial port. If the serial port receives 10 data at a time, then it is easy to deal with. Let's directly define an array and set the size to 10. Well, if we don't know how many data will come from the serial port, for example, it is possible to have 100 data in one frame and 1000 in the next frame, then it is very inconvenient to use an array to store it. We do not know how big an array should be allocated. If we allocate an array with a size of 1000, it will cause a waste of memory, for example, we can use dynamic linked lists to achieve this.

Generally, dynamic linked lists are often used in the underlying operating system, and are usually used for message queuing or task creation.

The above is all the contents of the article "how to use linked lists in C language". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report