linklist----reverse 约定不等于承诺〃 2022-08-13 18:48 142阅读 0赞 #include <stdio.h> #include <stdlib.h> #define N 10 #define M 3 typedef struct node{ int n; struct node *next; }node_t; node_t *reverse(node_t *head) { node_t *pre, *cur, *next; if(!head || !(head->next)) return head; pre = head; cur = head->next; while(cur){ next = cur->next; cur->next = pre; pre = cur; cur = next; } head->next = NULL; return pre; } void disp(node_t *head) { node_t *cur; for(cur = head; cur; cur = cur->next){ printf("%d\n", cur->n); } } int main(void) { int i, remain = N, step = 1; node_t *range1, *range2, *newp, *pre, *cur, *head, *tail; for(i = 0; i < N; i++){ newp = malloc(sizeof(node_t)); if(!newp){ perror("malloc fail!\n"); return 0; } newp->n = i + 1; newp->next = NULL; if(i == 0){ head = newp; }else{ tail->next = newp; } tail = newp; } //tail->next = head; range1 = range2 = head; while(1){ range1 = range1->next; range2 = range2->next->next; if(!range1 || !range2){ printf("no ring!\n"); return 0; } if(range1 == range2){ printf("ring!\n"); return 0; } } return 0; }
还没有评论,来说两句吧...