In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to realize the basic function of single linked list in C language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "C language how to achieve the basic functions of a single linked list"!
1. First, take a brief look at the concept of linked lists:
Note that a linked list is a linear list implemented by a structure, which can only go from back to back, not from back to front (because next only holds the address of the next node). When implementing the operation of a single linked list, you need to use a pointer to operate. Very simple, the notes are very detailed, you are welcome to correct ha ~ the previous writing is too bad to rewrite it.
two。 Code display:
# include # include # include typedef struct linklist {int data; struct linklist* next;} node;// directory / / 1. Dynamically apply for node node* Creatnode (int x); / / 2. Insert void PushBack (node** plist, int x) of single linked list; / / 3. Print void Printlist (node** plist) of single linked list; / / 4. Delete void Popback (node** plist) at the end of the single chain list; / / 5. Void PushFront (node** plist, int x) of single linked list; / / 6. Delete void PopFrount (node** plist) from the header of the single linked list; / / 7. Single linked list search node* Findpos (node* plist, int x); / / 8. The single linked list inserts xvoid Insertlinstafter (node* pos, int x) after the pos position; / / 9. The element void PopPosAfter (node* pos) after the single linked list deletes the pos position; / / 10. Destroy void Destorylist (node** plist) of single linked list; / / 1. Dynamic application node node* Creatnode (int x) {node* t = (node*) malloc (sizeof (node)); if (t = = NULL) {assert (0); return NULL;} else {t-> next = NULL; t-> data = x; return t;}} / / 2. Insert void PushBack (node** plist, int x) {assert (plist) of single linked list; if (* plist = = NULL) {* plist = Creatnode (x);} else {node* p = * plist; while (p-> next) {p = p-> next } p-> next = Creatnode (x);}} / / 3. Print single linked list void Printlist (node** plist) {assert (plist); node* p = * plist; while (p) {printf ("% d", p-> data); p = p-> next;}} / / 4. Delete void Popback (node** plist) {assert (plist); if (* plist = = NULL) {return NULL;} node* p = * plist; node* Q = NULL; while (p-> next) {Q = p; p = p-> next;} Q-> next = NULL Free (p);} / 5. Headline void PushFront (node** plist, int x) {assert (plist); node* t = Creatnode (x); if (NULL = = * plist) {* plist = t;} else {t-> next = * plist; * plist = t;}} / / 6. Delete void PopFrount (node** plist) {assert (plist); if (plist = = NULL) {return NULL;} else {node* p = * plist; * plist = p-> next; free (p);} / / 7. Lookup of single linked list node* Findpos (node* plist, int x) {node* cur = plist; while (cur) {if (cur- > data = = x) {return cur;} cur = cur- > next;} return NULL;} / / 8. The single linked list inserts xvoid Insertlinstafter (node* pos, int x) {assert (pos) after the pos position; if (NULL = = pos) {return;} node* t = Creatnode (x); t-> next = pos- > next; pos- > next = t;} / / 9. The single linked list deletes the element void PopPosAfter (node* pos) {assert (pos); if (pos- > next = = NULL) {return;} else {node* p = pos- > next; pos- > next = p-> next; free (p);} / / 10. Destruction of single linked list void Destorylist (node** plist) {assert (plist); node* p = * plist; while (p) {* plist= p-> next; free (p); p = * plist;} * plist=NULL;} void test1 () {node* plist=NULL / / create header pointer PushBack (& plist, 1); / / insert element PushBack (& plist, 2); PushBack (& plist, 3); PushBack (& plist, 4); PushBack (& plist, 5); Printlist (& plist); / / print linked list element 1 2 3 4 5 printf ("\ n"); Popback (& plist) / / tail delete element PushFront (& plist, 0); / / first insert element 0 Printlist (& plist); / / print linked list 0 1 2 3 4 printf ("\ n"); PopFrount (& plist); / / first deleted element 0 Printlist (& plist); / / print linked list 1 2 3 4 printf ("\ n"); Findpos (plist,1) / / find the address of 1 in the linked list, which is not convenient to demonstrate. The following will demonstrate Insertlinstafter (Findpos (plist, 4), 5); / / insert 5 after 4 to use the above Findpos function Printlist (& plist); / / print the linked list 1 2 3 4 5 printf ("\ n"); PopPosAfter (Findpos (plist, 4)) / delete the element after the specified location (delete the 5 after 4) Printlist (& plist); / / print the linked list 1 2 3 4 printf ("\ n"); Destorylist (& plist); / / destroy the linked list Printlist (& plist); / / print the linked list} void test () {test1 ();} int main () {test (); return 0;}
3. Test results:
a. The header pointer plist is created first
b. Tail insert 1 2 3 4 5
c. Delete element 5
d. First insert element 0
e. First delete element 0
f. Insert 5 after element 4
g. Delete 5 after element 4
h. Destroy linked list
At this point, I believe you have a deeper understanding of "how C language realizes the basic function of single linked list". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.