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)05/31 Report--
Most people do not understand the knowledge points of this article "C language takes the lead in how to achieve two-way circular linked list", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C language to take the lead in two-way circular list how to achieve" article.
The structure of leading two-way cyclic linked list
In fact, single linked lists also have a big drawback:
1. You can't traverse from back to front.
two。 Unable to find the precursor
In addition to the single linked list, we naturally have a two-way linked list, what we want to say is to take the lead in the two-way circular linked list, simply understood as: the lead node, there are two directions. Cyclical. The structure diagram is as follows:
Although the structure is more complex, it is very convenient for us to find the node, for example, we can find the tail node directly, and then enter the related operation. The operation of the actual code will be simpler and more convenient than the single linked list. Without too much explanation here, we will start with the code directly.
Code operation
Let's go straight to the point and get into the operation of the code implementation, and if you understand the previous operation, I'm sure it won't be difficult for you. The source code is given directly below:
List.h
# pragma once#include # include typedef int LTDataType;// takes the lead in bidirectional loop-the optimal linked list structure. Inserting and deleting data anywhere is O (1) typedef struct listNode {struct ListNode* next; struct ListNode* prev; LTDataType data;} ListNode;// initializing ListNode*ListInit (); / / destroying void ListDestory (ListNode* phead); / / printing void ListPrint (ListNode* phead) / / insert void ListPushBack (ListNode* phead, LTDataType x); / / insert void ListPushFront (ListNode* phead, LTDataType x); / / insert void ListPopFront (ListNode* phead); / / delete void ListPopBack (ListNode* phead); ListNode* ListFind (ListNode* phead, LTDataType x); / / insert xvoid ListInsert (ListNode* pos, LTDataType x) before pos position; / / delete the value void ListErase (ListNode* pos) of pos position
List.c
# include "List.h" / / Open up a new node ListNode* BuyListNode (LTDataType x) {ListNode* newnode = (ListNode*) malloc (sizeof (ListNode)); newnode- > data = x; newnode- > next = NULL; newnode- > prev = NULL; return newnode;} / / initialize ListNode* ListInit () {ListNode*phead = BuyListNode (0); phead- > next = phead; phead- > prev = phead Return phead;} / / destroy void ListDestory (ListNode* phead) {assert (phead); ListNode* cur = phead- > next; while (cur! = phead) {ListNode* next = cur- > next; free (cur); cur = next;} free (phead); phead = NULL } / / print void ListPrint (ListNode* phead) {ListNode* cur = phead- > next; while (cur! = phead) {printf ("% d", cur- > data); cur = cur- > next;} printf ("\ n");} / / void ListPushBack (ListNode* phead, LTDataType x) {assert (phead); ListNode* tail = phead- > prev ListNode* newnode = BuyListNode (x); tail- > next = newnode; newnode- > prev = tail; newnode- > next = phead; phead- > prev = newnode;} / / void ListPushFront (ListNode* phead, LTDataType x) {assert (phead); ListNode* first = phead- > next; ListNode* newnode = BuyListNode (x); newnode- > next = first; first- > prev = newnode Phead- > next = newnode; newnode- > prev = phead;} / / head deletion void ListPopFront (ListNode* phead) {assert (phead); assert (phead- > next! = phead); ListNode* first = phead- > next; ListNode* second = first- > next; phead- > next = second; second- > prev = phead; free (first); first = NULL } / / delete void ListPopBack (ListNode* phead) {assert (phead); assert (phead- > next! = phead); ListNode* tail = phead- > prev; ListNode* prev = tail- > prev; prev- > next = phead; phead- > prev = prev; free (tail); tail = NULL;} ListNode* ListFind (ListNode* phead, LTDataType x) {assert (phead); ListNode* cur = phead- > next While (cur! = phead) {if (cur- > data = = x) {return cur;} cur = cur- > next;} return NULL;} / / insert xvoid ListInsert (ListNode* pos, LTDataType x) {assert (pos) before the pos location ListNode* prev = pos- > prev; ListNode* newnode = BuyListNode (x); prev- > next = newnode; newnode- > prev = prev; newnode- > next = pos; pos- > prev = newnode;} / / remove the value of the pos location void ListErase (ListNode* pos) {assert (pos); ListNode* prev = pos- > prev; ListNode* next = pos- > next; prev- > next = next Next- > prev = prev; free (pos);}
Test.c
# include "List.h" void TestList1 () {ListNode* plist = ListInit (); ListPushBack (plist, 1); ListPushBack (plist, 2); ListPushBack (plist, 3); ListPushBack (plist, 4); ListPrint (plist); ListPushFront (plist, 0); ListPushFront (plist,-1); ListPrint (plist); ListPopFront (plist); ListPopFront (plist) ListPopFront (plist); ListPrint (plist); ListPopBack (plist); ListPrint (plist);} void TestList2 () {ListNode* plist = ListInit (); ListPushBack (plist, 1); ListPushBack (plist, 2); ListPushBack (plist, 3); ListPushBack (plist, 4); ListPrint (plist); ListNode* pos = ListFind (plist, 3) If (pos) {pos- > data * = 10; printf ("found and * 10\ n");} else {printf ("not found\ n");} ListPrint (plist); ListInsert (pos, 300); ListPrint (plist); ListErase (pos) ListPrint (plist);} int main () {TestList2 (); return 0;} the above is about the content of this article "how to realize the two-way circular linked list led by C language". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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.
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.