In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to realize circular linked list and Joseph ring in C++". Many people will encounter this dilemma in the operation of actual cases. then 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!
Cyclic linked list and Joseph ring
Realization of cyclic linked list
A single linked list has only backward nodes. When the tail list of a single linked list does not point to NULL, but to the head node, it forms a ring and becomes a single cyclic linked list, which is referred to as a cyclic linked list. When it is an empty table, the backward node only thinks about itself, which is the main difference between it and a single linked list, judging whether node- > next equals head.
The code implementation is divided into four parts:
Initialization
insert
Delete
Location search
Code implementation:
Void ListInit (Node * pNode) {int item; Node * temp,*target; coutnext = pNode;} else {/ / for (target = pNode;target- > target- > next); temp = new Node; if (! (temp)) exit (0); temp- > data = item; temp- > next = pNode;target- > next = temp } void ListInsert (Node * pNode,int I) {/ / Parameter is the first node and insertion position Node * temp; Node * target; int item; coutnext! = pNode;target = target- > next); temp- > next = pNode;target-> next = temp; pNode = temp;} else {target = pNode; for (int jaunt 1; temp = new Node; if (! temp) exit (0); temp- > data = item Temp- > next = target- > next; target- > next = temp;}} void ListDelete (Node * pNode,int I) {Node * target,*temp; if (itemized targets 1) {for (target=pNode;target- > next extents target target-> next); temp = pNode;// save the first node to be deleted, and it is easy to release pNode = pNode- > next; target- > next = pNode; delete temp;} else {target=pNode; for (int jig1) Jnext; temp = target- > next;// to release node target- > next = target- > next- > next; delete temp;}} int ListSearch (Node * pNode,int elem) {/ / query and return the location of the node Node * target; int item1; for (target = pNode;target- > datastatelem & & target- > nextstone = pNode;++i) target = target- > next; if (target- > next = = pNode & & target- > dataroomelem) return 0; else return i;}
Joseph problem
Joseph's ring (Joseph problem) is an applied problem in mathematics: it is known that n people (denoted by the serial number 1Magne2Magne3. N, respectively) sit around a round table. The person numbered k begins to count, and the person who counts to m comes out; his next person starts counting from 1, and the person who counts to m comes out again; repeat according to this rule until all the people around the round table are out of line. This kind of problem can be solved with the idea of circular list.
Note: when writing code, pay attention to the special case when the number of reports is m = 1
# include#includeusing namespace std;typedef struct Node {int data; Node * next;}; Node * Create (int n) {Node * p = NULL, * head; head = new Node; if (! head) exit (0); p = head; / / p is the current pointer int item=1; if (n) {int item1; Node * temp; while (idata = iTunes; p-> next = temp; p = temp) } p-> next = head- > next;} delete head; return p-> next;} void Joseph (int npenint m) {/ / n is the total number of people, m is the number of exits m = n% m; Node * start = Create (n); if (m) {/ / if the remainder is zero, it means that the while (start- > nextint start) {Node * temp = new Node If (! temp) exit (0); for (int iTunes 0 into text; temp = start- > next; start- > next = start- > next- > next; start = start- > next; 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.