[Algorithm world] about single-linked list operation has ring-free judgment

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h>//a variety of function tests for the ring list typedef struct NODE{INT data;struct Node *next;} node;typedef struct node* linklist;/* list initialization */int initlist (linklist *l) {*l = (linklist) malloc (sizeof (Node)); *l)) return-1; (*l)->next = Null;return 0;} /* The length of the list */int listlength (linklist L) {int i = 0; Linklist p = l->next;while (p) {i++;p = P->next;} return i;} int Createlisthead (linklist *l, int n) {linklist p;int i;for (i=0; i<n; i++) {p = (linklist) malloc (sizeof (Node));p data = I+1;p->next = (*l)->next; (*l)->next = p;}} int Createlisttail (linklist *l, int n) {linklist p, r;int i;r = *l;for (i=0; i<n; i++) {p = (linklist) malloc (sizeof (Node)) ;p->data = I+1;r->next = P;r = P;} R->next = NULL;} /* Print List */void listtraverse (linklist L) {linklist p = l->next;while (p) {printf ("%d", p->data);p = P->next;} printf ("\ n");} /* Single linked list reversal */linklist Listreverse (linklist l) {linklist Current, Pnext, prev;if (L = = NULL | | L->next = = NULL) return l;current = L->next;pNext = Current->next;current->next = Null;while (pnext) {prev = Pnext->next;pnext->next = Current;current = PN Ext;pnext = prev;} L->next = Current;return L;} Linklist ListReverse2 (linklist l) {linklist Current, p;if (L = = NULL) return null;current = l->next;printf ("current =%d\       n ", Current->data); while (current->next! = NULL) {/** for example * p = current->next; 9 number words need eight times, the first P equals 8* currnet->next = p->next;       The next value of P is 7, and the next * P->next = l->next; of the current is paid             Assign the first node of L 9 to the next value of P * l->next = p;          The next value of L is 8 because the next of the previous p is already 9*/p = current->next; Current->next = P->next;p->next = l->next; L->next = P;} return L;} int Listinsert (linklist *l, int pos, int num) {int J; Linklist p, s;p = *l;j = 1;while (P && j<pos) {p = p->next;++j;} if (!p | | j>pos) return-1;s = (linklist) malloc (sizeof (Node)); s->data = Num;s->next = P->next;p->next = S;r Eturn 0;} int IsEmpty (linklist L) {if (L->next) return-1;elsereturn 1;} int Buildlistloop (linklist *l, int num) {int i = 0; Linklist cur = (*l)->next; linklist tail= null;if (num <= 0 | | L = = null) return-1;for (i=1; i<num; ++i) {if (cur = = null) {return-1;} cur = cur->next;} Tail = Cur;while (tail->next) {tail = Tail->next;} Tail->next = Cur;return 0;} int Listtraveselimit (linklist L, int n) {int i = 0; Linklist p = l->next;while (P && i<n) {printf ("%d", p->data);p = p->next;i++;} printf ("\ n only shows%d \ n", "Nth"); return 0;} int Hasloop (linklist l) {linklist fast = l; linklist slow = l;/** * here if there is no ring, then fast arrives at the end point * When the list length is odd, fast->next is empty * When the list length is even, fast is empty */while (fast! = NULL && F Ast->next! = NULL) {fast = Fast->next->next;slow = slow->next;if (fast = = slow) break;} if (fast = = NULL | | fast->next = NULL) Return-1;elsereturn 1;} int Looplength (linklist L) {if ( -1 = = Hasloop (l)) return 0; Linklist fast = L; linklist slow = l;int length = 0;bool begin = False;bool again = false;while (fast! = NULL && Fast->neXT = NULL) {fast = Fast->next->next;slow = slow->next;//stops counting after two laps, jumps out of the loop if (fast = = Slow && again = = True ) {break;} After a lap start counting if (fast = = Slow && again = = false) {begin = True;again = true;} Count if (begin = = True) ++length;} return length;}  Solve the problem of entry point ideas//8 O-o 7/////9 o O 6//\///O-o-O-o- o//1 2 3 4 5//The distance from the meeting point to the connection point is equal to the distance from the head pointer to the connection point//First let the speed pointer meet, and then the slow pointer re-points to the head node//from the meeting point, the head pointer to start walking, meet the shop is the connection point linklist Findloopentera CE (linklist l) {linklist fast = L; Linklist slow = l;while (fast! = NULL && Fast->next! = null) {fast = Fast->next->next;slow = Slow->nex t;//If there is a ring, then fast will exceed the slow lap if (fast = = slow) break;} if (fast = = NULL | | fast->next = = NULL) return Null;slow = L;while (slow! = fast) {slow = Slow->next;fast = Fast->nex t;} return slow;} int Getmidnode (linklist l, int *e) {linklist fast = l; Linklist slow = L;while (fast->next! = null) {if (Fast->next->next! = null) {fast = FAST-&GT;next->next;slow = Slow->next;} Else{fast = Fast->next;}} *e = Slow->data;return 0;} int clearlist (linklist *l) {linklist p, q;p = (*l)->next;while (p) {q = P->next;free (p);p = q;} (*l)->next = Null;return 0;} int main () {linklist l;int i;int n;int e;i = initlist (&l); if (i! =-1) printf ("Initlist () initialization succeeded. \ n");p rintf ("After the list L is initialized, Li Stlength (L) =%d\n ", Listlength (L)); for (i=0; i<10; i++) {Listinsert (&l, 1, i);} printf ("IsEmpty =%d (1: null,-1: Non-empty) \ n", IsEmpty (L));p rintf ("Listlength (L) =%d\n", Listlength (L));p rintf ("Print List:"); Listtraverse (L);//reverse reversal of LISTREVERSE2 (l);p rintf ("Reverse Printing chain list:"); Listtraverse (L);//The middle node of the list Getmidnode (L, &e);p rintf ("Intermediate node of the list e=%d\n", E);p rintf ("Ring the list, enter position:"); scanf ("%d", &AMP;N); Buildlistloop (&l, N); Listtraveselimit (L, 20);//Determine if the list has a ring if (1 = = Hasloop (L)) printf ("Chain list has a ring \ n"), elseprintf ("Chain list without ring \ n"),//Calculate ring length printf ("Calculate the ring length, Looplength (L) =%d\n ", Looplength (L));//Find the entry point of the ring linklist enterance = Findloopenterace (l);p rintf (" The value of the entry point is:%d\n ", Enterance->data);//printf ("AdoptedHead interpolation method: \ n ", Createlisthead (&l));//listtraverse (L);//printf (" Using the tail interpolation method \ n ", Createlisttail (&l, 10));// Listtraverse (L);//i = Clearlist (&l);//if (i = = 0)//printf ("Empty linklist list. \ n");//printf ("IsEmpty =%d (1: null,-1: Non-empty) \ n" , IsEmpty (L));//printf ("Listlength (L) =%d\n", Listlength (L)); return 0;}

  

[Algorithm world] about single-linked list operation has ring-free judgment

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.