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

What is the function of linked list in C language

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

Share

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

This article mainly introduces the relevant knowledge of "what is the function of linked lists in C language". The editor shows you the operation process through practical cases. The operation method is simple, fast and practical. I hope this article "what is the role of linked lists in C language" can help you solve the problem.

First of all, everything in the pointer computer is a number. When you create a variable in the C language, the compiler processes it only by address, but in your code, you can access it in two ways: by value and by address.

In this code, I use three variables, and the compiler will access all of them by address.

Time-global variables-stored in the heap

Skip- parameters-stored in the stack

I-Local variables-stored in the stack

"Hello world-%d\ n"-stored in the heap

A heap is a block of memory allocated by the startup code linked by the compiler to your code. How it is created is not important, but not all compilers are the same. That's the technology we want.

For simplicity, assume that the variable Times ends at memory location 256 (0x100). The position of the variable I on the stack is 64 (0x40). The word "Hello..." The location is 200 (0xc8). Finally, skip and you get 68 (0x44).

Now suppose you call function () like this:

This is what the for loop starts with:

Time-stored in 0x100-value is 10

Skip-stored in 0x44-value 2

I-stored in 0x40-value 0

Unnamed text is stored in 0xc8-value "Hello world-%d\ n"

Addressing is very simple now. In the C world, when you use a variable name that is not a pointer, the compiler treats it as a "value stored in an address." Therefore, when you use the variable skip, the compiler uses the value stored at 0x44. All variables in this code snippet are by value, indicating that only values are used. The pointer is the address of the variable. Suppose we do this:

Now you have a variable called TimesPtr. Like all other variables, it has a memory address associated with it. But by defining it as a pointer, you can specify its value as the memory address.

So, using all the above numbers, we will also add

TimesPtr-stored in the address 260 (ox104)-stack

Number-stored at address 263 (quantity 0x108)-on heap

We have assigned TimePtr as the address of Times, that is, 0x100. At the beginning of the loop, we specify I = 0, so 0 is stored in 0x40. We also assign Num, which is also a pointer to the integer 0. The compiler will create the code, and 0 will be put into the amount of 0x108, the number of addresses.

When you use the'* 'operator, you are telling the compiler to use the variable "value stored in the address" that you specify. When only variable names are used, you will tell the compiler "stored value".

By the way, in the printf () call, you pass the address of the text to the function printf (), because the text has only one address in the compiler, not a name (and the array is passed by address, and the text is an array of characters).

The easiest way to create a linked list is to simply add a pointer to the structure itself as a member of the structure. It is accomplished as follows:

If you want to use typedef, you cannot use the typedef name because it has not been defined. Suppose we want to reference the _ student structure as studentT. The code is as follows:

Next will now be the address of the next node in the list, or NULL if not.

This is the easiest way to create a list of links. Simply create a node, assign its next pointer to the list address, and then assign the column header to the address of that node. In the class, you'd better deal with strdup () and remember to release the node when you delete it.

Now you need to insert and delete operations in a single linked list. I will provide you with a generic linked list to hold any data type:

This is the end of the content about "what is the function of linked lists in C language". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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