In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to implement the C language linked list of data structure. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Preface
The function library that needs to be used
# include#include
The malloc function is used to allocate space dynamically, which is equivalent to the role of new in Java.
First, you need to create a node structure.
Typedef struct {int data; struct linkNode* next;} linkNode; function 1. List initialization void iniLinkList (linkNode * fp) {/ / initialize the list pass in a header node linkNode * point= fp;// to define a pointer to the header node for (int item0 and node- > next=NULL; point- > next= node; point=point- > next;} 2. Calculate the list length int countListLength (linkNode * fp) {/ / fp: the chain header node linkNode * point= fp;// defines a pointer to the header node int sum = 0; / / it is used to calculate the number of linked list nodes while (point- > next) {sum++; point=point- > next;} return sum;} 3. Print linked list void printLinkList (linkNode * fp) {/ / incoming chain header node linkNode * point= fp;// defines a pointer to the header node printf ("list contents are as follows:\ n"); while (point- > next) {linkNode * node = point- > next; printf ("% d\ t", node- > data); point=point- > next;} printf ("\ n");} 4. Calculate the list length int countListLength (linkNode * fp) {/ / fp: the chain header node linkNode * point= fp;// defines a pointer to the header node int sum = 0; / / it is used to calculate the number of linked list nodes while (point- > next) {sum++; point=point- > next;} return sum;} 5. Delete the specified location node int deleteNode (int index,linkNode * fp) {/ / index: the subscript of the node in the linked list. Fp: the link header node linkNode * point = fp;// defines a pointer to the header node if (index (countListLength (fp)-1)) {/ / determines whether index is valid (within the length of the linked list) printf ("subscript is not in the linked list!") ; return 1; / / returns 1 to indicate deletion failed} else {for (int iDeposit 0 into extents;} linkNode * node = point- > next; point- > next=node- > next; / / Delete subscript location node return 0 / / return 0 to indicate that the deletion was successful}} 6. Insert node void insertLinkNode (linkNode * fp,int index,int data) {/ / index: subscript of node in linked list, fp: linked header node, data: inserted new data linkNode * point = fp;// definition pointer to header node if (index (countListLength (fp)-1)) {/ / determine whether index is valid (within the length of the linked list) printf ("subscript is not in the linked list!") ; return;} else {for (int item0 into text; / / find the node before the subscript location linkNode * node = malloc (sizeof (linkNode)); node- > data=data; / / create a new node node- > next=point- > next / / assign the next of the node before the subscript to the next point- of node > next = node; / / give the address of the newly created node to the node before the subscript}} 7. Full code + running effect # include#includetypedef struct {int data; struct linkNode* next;} linkNode;// initialization list void iniLinkList (linkNode* fp) {/ / pass in a header node linkNode* point= fp;// to define a pointer to the header node for (node- > next=NULL; point- > next= node; point=point- > next }} / / insert node void insertLinkNode (linkNode * fp,int index,int data) {/ / index: subscript of node in linked list, fp: linked header node, data: inserted new data linkNode * point = fp;// definition pointer to header node if (index (countListLength (fp)-1)) {/ / determine whether index is valid (within the length of the linked list) printf ("subscript is not in the linked list!") ; return;} else {for (int item0 into text; / / find the node before the subscript location linkNode * node = malloc (sizeof (linkNode)); node- > data=data; / / create a new node node- > next=point- > next / / assign the next of the previous node to the next point- of node > next = node; / / assign the address of the newly created node to the node before the subscript location}} / / delete the node at the specified location in the linked list int deleteNode (int index,linkNode * fp) {/ / index: subscript of the node in the linked list, fp: chain header node linkNode * point = fp / / define the pointer to the header node if (index (countListLength (fp)-1)) {/ / determine whether the index is valid (within the length of the linked list) printf ("subscript is not in the linked list!") ; return 1; / / returns 1 to indicate deletion failed} else {for (int iDeposit 0 into extents;} linkNode * node = point- > next; point- > next=node- > next; / / Delete subscript location node return 0 / / return 0 indicates successful deletion} / / calculate the linked list length int countListLength (linkNode * fp) {/ / fp: chain header node linkNode * point= fp;// defines pointer to header node int sum = 0; / / it is used to calculate the number of linked list nodes while (point- > next) {sum++; point=point- > next;} return sum } / / print linked list void printLinkList (linkNode * fp) {/ / incoming chain header node linkNode * point= fp;// defines pointer to header node printf ("list contents are as follows:\ n"); while (point- > next) {linkNode * node = point- > next; printf ("% d\ t", node- > data); point=point- > next;} printf ("\ n") } int main () {linkNode * linkList = malloc (sizeof (linkNode)); / / create a header node iniLinkList (linkList); printLinkList (linkList); deleteNode (3 herolinkList); printf (after deletion); printLinkList (linkList); insertLinkNode (linkList,3,7); printf (after insertion); printLinkList (linkList); return 0 } / / the contents of the linked list are as follows: / / 0 1 2 3 4 move / delete the contents of the linked list are as follows: / / 0 12 4 2 4 the contents of the linked list after insertion are as follows: / / 0 1 2 7 4 on how to implement the data structure C language linked list is shared here. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.