In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is a detailed introduction to "C language how to achieve two-way linked list and two-way circular linked list", detailed content, clear steps, details properly handled, I hope this article "C language how to achieve two-way linked list and two-way circular linked list" article can help you solve doubts, the following follow the small series of ideas slowly in-depth, together to learn new knowledge bar.
Double-linked lists and double-linked circular lists
Compared with the one-way linked list, there is one more precursor node. If it is empty, then next and prior both point to themselves. For double-loop lists, you only need the next of the last element to point to head->next, and the prior of head->next to point to the last node.
insert operation
The new node s is inserted into the list, s->next to node p, s->prior to p->prior, then p->prior->next points to s, p->prior points to s. Order needs attention
s->next = p;s->prior = p->prior;p->prior->next = s;p->prior = s;
delete operation
Remove node p. p->next->prior points to p->prior, p->prior->next points to p->next. Finally, delete the p node.
p->prior->next = p->next;p->next->prior = p->prior;delete p;
example operation
(Screenshot attached)
Note: Because the function does not return a Node* type, the pointer is referenced here, otherwise the change is not saved when exiting the function. If you need to delete all linked lists, you need to save the head address after InitList, otherwise you will miss a Node node that has not been deleted.
Code implementation:
#include#include #includeusing namespace std;const int OK = 1;const int ERROR = 0;const int LETTERNUM = 26;typedef char ElemType;struct Node{ ElemType data; Node * prior;//front drive node Node * next;//rear drive node};int InitList(Node *&L){ Node *p,*q; int i; L = new Node; //head node L->next = L->prior = NULL; p = L; //p is the current pointer for(int i=0;idata = 'A' + i; q->prior = p; q->next = p->next; p->next = q; p = q;//pointer movement } p->next = L->next; //tail node points to head->next(first letter address) L->next->prior = p; return OK;}void Change(Node *&L,int i){ //Move head pointer if (i>0){ while(i--){ L = L->next; } } else if (inext ; while(i++){ L = L->prior; } } else{ L = L->next; }}int main(){ Node *head = NULL; int i,n; InitList(head); //Node *s_head = head; cout
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.