Returns the reverse order of a single-chain table, determines whether a single-chain table has a ring, and whether two linked lists without a ring have an intersection.

Source: Internet
Author: User

This class is a collection class of several functions. Note that after the ring is created, it is determined that there is a ring, and then the ring should be removed, otherwise, the destructor of the final linked list will fail to exit due to the existence of loops, which will lead to four cycles of the program.

 

# Include <iostream> <br/> using namespace STD; <br/> struct listnode <br/> {<br/> int data; <br/> listnode * next; <br/>}; <br/> class linklist <br/>{< br/> listnode * head; // point to the head pointer of the linked list <br/> int count; // number of nodes <br/> listnode * interp; // The intermediate node for storing the manufacturing ring <br/> Public: <br/> linklist (); <br/> linklist (int n); // manually create a linked list containing n elements <br/> ~ Linklist (); <br/> void reverselist (); // reverse linked list <br/> void printlist (); // print the current linked list <br/> bool iscircle (); // determine whether there is a ring in the linked list <br/> // manufacturing ring -- the method is to point the end of the chain to the end of the N node in the chain (condition: Count> N) <br/> void genecircle (int n); <br/> void degenecircle (int n); // remove the ring to prevent non-destructing <br/> }; <br/> linklist: linklist () <br/>{< br/> head = NULL; <br/> COUNT = 0; <br/>}< br/> linklist: linklist (int n) <br/>{< br/> COUNT = N; <br/> head = new listnode; <br/> listnode * P = head; <br/> for (INT I = 0; I <n; I ++) <br/> {<br/> listnode * newptr = new listnode; // create a new node <br/> cout <"input node" <I + 1 <":"; <br/> CIN> newptr-> data; // assign a value to the new node <br/> P-> next = newptr; // the current node points to newptr <br/> P = p-> next; // P changes to the next location of the current location <br/>}< br/> P-> next = NULL; <br/> interp = NULL; <br/>}< br/> linklist ::~ Linklist () <br/>{< br/> listnode * P = head-> next; <br/> listnode * tmpptr; <br/> while (P! = NULL) <br/>{< br/> tmpptr = p-> next; <br/> Delete P; <br/> P = tmpptr; <br/>}< br/> Delete tmpptr, P, Head, interp; <br/> P = tmpptr = head = interp = NULL; <br/> cout <"deconstructor! "<Endl; <br/>}< br/> void linklist: reverselist () <br/>{< br/> listnode * pcurr = head-> next; // pcurr points to the header node of the list to be reversed (that is, P points to the first node) <br/> If (pcurr = NULL | pcurr-> next = NULL) // only the header pointer or only one node <br/> return; <br/> head-> next = NULL; // clear the original linked list (the head is still the header node after the reverse order) <br/> // link the nodes in the original linked list in reverse order <br/> while (pcurr! = NULL) <br/>{< br/> listnode * pTMP = pcurr-> next; // pcurr points to the node to be processed, pTMP stores the next node of pcurr <br/> // inserts pcurr between the head and the first node (pcurr is used as the new first node) <br/> pcurr-> next = head-> next; // Insert the new node pcurr to the first node (Head-> next) <br/> head-> next = pcurr; // the header node points to the new node pcurr <br/> pcurr = pTMP; // The next node to be processed <br/>}< br/> void linklist: printlist () <br/> {<br/> If (COUNT = 0) <br/> {<br/> cout <"list is empty! "<Endl; <br/> return; <br/>}< br/> listnode * P = head-> next; <br/> cout <"data of the List is:" <Endl; <br/> while (P! = NULL) <br/>{< br/> If (p-> next) <br/> cout <p-> data <"--> "; <br/> else <br/> cout <p-> data; <br/> P = p-> next; <br/>}< br/> cout <Endl <"length of the list is:" <count <Endl; <br/>}< br/> bool linklist: iscircle () <br/>{< br/> listnode * P = head-> next; <br/> listnode * q = head-> next; <br/> while (p-> next & Q-> next) <br/>{< br/> P = p-> next; <br/> If (null = (q = Q-> next )) <br/> return false; <br/> If (P = = Q) <br/> return true; <br/>}< br/> return false; <br/>}< br/> void linklist: genecircle (int n) <br/> {<br/> If (n> = count) <br/> {<br/> cout <"can not generate a circle! "<Endl; <br/> return; <br/>}< br/> listnode * temp, * P = head-> next; // P will point to the nth (team end) element <br/> for (INT I = 1; p-> next; I ++) <br/> {<br/> if (I = N) // locate the nth element <br/> {<br/> temp = P; // use temp to store the pointer P for the nth element <br/> break; <br/>}< br/> P = p-> next; // P constantly points to the following elements <br/>}< br/> interp = p-> next; <br/> P-> next = temp; // The next element of P points to temp (saving the element pointed to by P), forming a ring <br/>}< br/> void linklist :: degenecircle (int n) <br/>{< br/> listnode * P = H EAD-> next; <br/> for (INT I = 1; p-> next; I ++) <br/>{< br/> if (I = N) <br/> break; <br/> P = p-> next; <br/>}< br/> P-> next = interp; <br/>}< br/> int main () <br/>{< br/> int N, Pos; <br/> cout <"input length of the list:"; <br/> CIN> N; <br/> linklist ls (n); <br/> ls. printlist (); <br/> ls. reverselist (); <br/> ls. printlist (); <br/> ls. reverselist (); <br/> ls. printlist (); <br/> cout <"input the position where to Genera Tor circle: "; <br/> CIN> Pos; <br/> ls. genecircle (POS); <br/> If (LS. iscircle () <br/>{< br/> cout <"circle linklist! "<Endl; <br/> // you must use this function to restore the link to the stateless state. Otherwise, because of the existence of the ring, <br/> // The destructor of the linked list cannot exit normally, resulting in an endless Program <br/> ls. degenecircle (POS); <br/>}< br/> else <br/> cout <"non-circle linklist! "<Endl; <br/> return 0; <br/>}
 

 

 

Determine whether two single-chain tables are Intersection
, If intersection, give the intersectionFirst

Point (neither of the two linked lists has a ring)

 

The intersection of two linked lists means that all nodes starting from the intersection are overlapped, similar to a "person.


There are two better methods:

(1) connect one of the linked lists to the beginning and end, and check whether the other linked list has a ring. If so, the two linked lists are intersecting, and the detected dependency ring entry is the first vertex of the intersection.

(2) If the two linked lists intersect and the two linked lists are the same nodes from the intersection to the end of the linked list, we can traverse one linked list first until the end of the chain list and then traverse another linked list, if you can also go to the same end point, the two linked lists will intersection.

Now let's write down the length of the two linked lists and traverse them again. The long chain table node starts to step forward (lengthmax-lengthmin), and then the two linked lists move forward simultaneously, each step, the first point of an encounter is the first point of the intersection of two linked lists.


The code for the 2nd method is as follows:


Node * findnode (node * phead1, node * phead2) <br/>{< br/> node * P1 = phead1; <br/> node * P2 = phead2; <br/> int I = 1, j = 1, K = 0, f = 0; <br/> If (phead1 = NULL | phead2 = NULL) <br/> return NULL; <br/> while (P1-> next! = NULL) <br/>{< br/> P1 = p1-> next; <br/> I ++; <br/>}< br/> while (P2-> next! = NULL) <br/>{< br/> P2 = P2-> next; <br/> J ++; <br/>}< br/> If (P1! = P2) <br/> return NULL; <br/> else <br/> {<br/> P1 = phead1; <br/> P2 = phead2; <br/> F = FABS (I-j) <br/> if (I> J) <br/> {<br/> for (k = 0; k <F; k ++) <br/> P1 = p1-> next; <br/> while (P1! = P2) <br/>{< br/> P1 = p1-> next; <br/> P2 = P2-> next; <br/>}< br/> return P1; <br/>}< br/> else <br/> {<br/> for (k = 0; k <F; k ++) <br/> P2 = P2-> next; <br/> while (P1! = P2) <br/>{< br/> P1 = p1-> next; <br/> P2 = P2-> next; <br/>}< br/> return P1; <br/>}< br/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.