Determine whether a single-chain table has a ring and a single-chain table

Source: Internet
Author: User

Determine whether a single-chain table has a ring and a single-chain table

1 # include <stdio. h> 2 # include <stdlib. h> 3 4 typedef struct Node {5 int data; 6 struct Node * next; 7} Node; 8 typedef struct Node * LinkList; 9 10 void InitialLinkList (LinkList * L) {11 (* L) = (LinkList) malloc (sizeof (struct Node); 12 if (! (* L) {13 printf ("Error: InitialLinkList: malloc \ n"); 14 exit (1); 15} 16 (* L)-> next = NULL; 17} 18 void createsimplereceivelist_withoutloop (LinkList * L, int number) {19 int count; 20 LinkList new, tail = (* L); 21 printf ("Create simplereceivelist_withoutloop \ n "); 22 for (count = 1; count <= number; count ++) {23 new = (LinkList) malloc (sizeof (struct Node); 24 if (! (New) {25 printf ("Error: createsimplereceivelist_withoutloop \ n"); 26 exit (1); 27} 28 printf ("please enter % d element:", count ); 29 scanf ("% d", & (new-> data); 30 new-> next = tail-> next; 31 tail-> next = new; 32 tail = new; 33} 34} 35 void createsimplereceivelist_withloop (LinkList * L, int number, int loopInIndex) {36/* The first parameter LinkList is the linked list to be created 37 * The second parameter number is the length of the linked list 38 * The third parameter loopInIndex indicates that the link points to the end of the linked list elements, 0 indicates that it is directed to the header node. 39 **/40 int count; 41 LinkList new, temp = * L, temp1 = * L; 42 if (loopInIndex> number) {43 printf ("Error: createsimplereceivelist_withloop: loopInIndex \ n "); 44} 45 else {46 printf (" Create simplecancelist_withloop \ n "); 47 for (count = 1; count <= number; count ++) {48 new = (LinkList) malloc (sizeof (struct Node); 49 if (! New) {50 printf ("Error: createsimplereceivelist_withloop \ n"); 51 exit (1); 52} 53 printf ("please enter % d element", count ); 54 scanf ("% d", & (new-> data); 55 temp-> next = new; 56 new-> next = * L; 57 temp = new; 58} 59 for (count = 0; count <loopInIndex; count ++) {60 temp1 = temp1-> next; 61} 62 temp-> next = temp1; 63} 64} 65 void JodgeLoop (LinkList L) {66 LinkList p = L, q = L; 67 int step_o = 0, step_ I = 0; 68 int isLoop = 0; 69 while (p) {70 step_ I = 0; 71 q = L; 72 while (q! = P) {73 q = q-> next; 74 step_ I ++; 75} 76 if (step_o! = Step_ I) {77 isLoop = 1; 78 break; 79} 80 p = p-> next; 81 step_o ++; 82} 83 if (isLoop = 1) 84 printf ("Exist loop in this List. appear in position % d \ n ", step_ I); 85 else 86 printf (" Dose no Exist loop in this List! \ N "); 87} 88 void JodgeLoop_2 (LinkList L) {89 LinkList quick = L, slow = L; 90 int isLoop = 0; 91 while (quick! = NULL & quick-> next! = NULL) {92 quick = quick-> next; 93 slow = slow-> next; 94 if (quick = slow) {95 isLoop = 1; 96 break; 97} 98} 99 if (isLoop) 100 printf ("Exist loop in this List. \ n "); 101 else102 printf (" Dose no Exist loop in this List! \ N "); 103} 104 void Display_withoutLoop (LinkList L) {105 while (L-> next! = NULL) {106 L = L-> next; 107 printf ("% d", L-> data); 108} 109 printf ("\ n "); 110} 111 112 int main () {113 LinkList L1; 114 LinkList L2; 115 InitialLinkList (& L1); 116 InitialLinkList (& L2); 117 createsimplecancelist_withloop (& L1, 5, 1 ); 118 createsimplecancelist_withoutloop (& L2, 4); 119 JodgeLoop (L1); 120 JodgeLoop (L2); 121 JodgeLoop_2 (L1); 122 JodgeLoop_2 (L2); 123 return 0; 124}

 

Related Article

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.