In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use C language to realize static linked list". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Note:
1. Here we use k to apply for space and I to traverse the space.
2. Static linked list uses cursors to simulate pointers, divides the fixed allocated memory into spare list and linked list, and realizes discrete storage when using self-made malloc and free functions to apply for free spare space.
3. The basic operation is actually similar to the dynamic linked list, but one is to use p = p-> next and the other is to use I = L [I] .cur to realize the backward movement of the pointer.
4. When initializing the linked list, only the cur of the last space in the linked list is 0, which means that it is the head pointer and there is no allocated space. The head pointer of the alternate linked list is the first position in the space, and the cur of the last pointer is also 0. 0 also means NULL in a static linked list
5, using dynamic linked list thinking, cur members can be regarded as pointers, in which the storage of the subscript of the unit is equivalent to pointing to which unit.
# include#include typedef struct {int data; int cur;} component, SLinkList [100]; int Malloc (SLinkList space) {int I = space [0] .cur; if (I) space [0] .cur = space.cur; return I;} void Free (SLinkList space, int k) {space.cur = space [0] .cur; space [0] .cur = k;} void Creat (SLinkList L) {int i; L [99] .cur = 0; for (I = 0; I < 98) Int ListLength +) L [I] .cur = I + 1; L [98] .cur = 0;} int ListLength (SLinkList L) {int I = 0, k = L [99] .cur; while (k) {k = L [k] .cur;} return I;} void Insert (SLinkList L, int val, int index) {int I = 99, k, n; k = Malloc (L); if (k) {L [k] .data = val; for (n = 1; n < index) While +) I = L [I] .cur; L [k] .cur = L [I] .cur; L [I] .cur = k;}} void Traverse (SLinkList L) {int I = L [99] .cur; while (I) {printf ("% d", L [I] .data); I = L [I] .cur;}} int main () {SLinkList L; Creat (L); Insert (L, 1,1); Traverse (L); printf ("Please enter the number:"); return 0 } "how to use C language to achieve static linked list" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.