会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
1989huangkq
huangkq1989
博客园
|
首页
|
新随笔
|
新文章
|
联系
|
订阅
|
管理
交叉链表 有环链表 链表逆转
/////////////////////////////////////////////// // // Description: this file process the link // 1. add node in the head and the tail // 2. link has a loop or not // 3. two links will meet each other or not // Create by: kangquan // Blog:http://blog.csdn.net/kangquan2008 // /////////////////////////////////////////////// #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <limits.h> #define LOOP_LINK_SIZE 11 typedef struct NODE { int data; struct NODE * next; }Node, * Link; Link head = NULL; void add_node_in_head(Node * node) { if(head == NULL)// when head of link is NULL { head = node; node->next = NULL; }else{ node->next = head; head = node; } } // add the node in the tail void add_node_in_tail(Node * node) { Node * p = NULL; Node * pre =NULL; for(p=head; p!=NULL; p=p->next) { pre = p; // when p points to NULL, // p already has no relative with the link, // so we shoule record the pre node of p! } pre->next = node; node->next = NULL; } // display it void display_node() { Node * p = head; for(; p!=NULL; p = p->next) { printf("%d ",p->data); } printf("\n"); } // travel the link and free the memory void free_link() { Node * p = head; for(; p!=NULL; p=head) // remember it is p=p->next,not p++ { head = p->next; free(p);// it's error,wher you free p,then p=p->next! } } //////////////////////////////////////////////////////////////////////// // the head is a parameter /////////////////////////////////////////////////////////////////////// // add a node to link--head Link add_node(Link head,Node * node) { if(head == NULL) { head = node; node->next = NULL; }else{ node->next = head; head = node; } return head; } void make_loop(Link head) { Node * tmp; int cnt = 0; for(Node * p=head; p!=NULL; p=p->next) { cnt++; if(cnt == 7) tmp = p; if(cnt == 11) { p->next = tmp; break; } } } // one link travels by two steps at a time, // the other travels by one step at a time, // the first one(two steps) will get the loop first, // and it will loop there,waiting the slow one, // so if there is a loop there,they alway meet each other. bool loop_link(Link head,Node ** tail,Node ** meet) { Node * one_step = head; Node * two_step = head; if(one_step == NULL || one_step->next == NULL)// only two node,never has a loop return false; Node * record_tail = NULL; do{ one_step = one_step->next; record_tail = two_step->next; two_step = two_step->next->next; }while(two_step != NULL && two_step->next != NULL && one_step != two_step); if(one_step == two_step) { (*meet) = two_step; return true; }else if(two_step == NULL){ (*tail) = record_tail; return false; }else if(two_step->next == NULL){ (*tail) = two_step; // (*tail) = one_step->next; return false; } } void make_cross(Link link_one,Link link_two) { link_one->next = link_two->next->next; } bool cross_links(Link link_one,Link link_two) { Node * tail = NULL; Node * meet = NULL; Node * tail_second = NULL; Node * meet_second = NULL; bool is_loop = false; bool is_loop_second = false; is_loop = loop_link(link_one,&tail,&meet); is_loop_second = loop_link(link_two,&tail_second,&meet_second); if(!is_loop && !is_loop_second)// both have no loop { if(tail == tail_second)// when both tails are the same,they're cross return true; else return false; }else if(is_loop != is_loop_second){// one has loop,no cross return false; }else{// both have cross,mean if they meet each other,the meeting point is beginning of the loop,we can go through the loop by one link,if meet the other loop point(meet_point),they are cross if(meet == meet_second)//it's meeting point return true; Node * tmp = meet->next; while(tmp != meet)// ok,still in the loop { if(tmp == meet_second) return true; tmp = tmp->next; } return false; } } // reverse the link void reverse_link(Link *head) { if((*head) == NULL) return; Node *pre,*cur,*next; // pre used to make the head point to reversed link // cur used to reverse the current node // next used to move the current point pre = (*head); cur = (*head)->next; while(cur != NULL) { next = cur->next; cur->next = pre;//reverse pre = cur; cur = next; } (*head)->next = NULL; (*head) = pre; } int main(void) { Node * new_node = NULL; for(int i=0; i<LOOP_LINK_SIZE; i++) { new_node = (Node *)malloc(sizeof(Node)); new_node->data = i; add_node_in_head(new_node); } for(int i=0; i<LOOP_LINK_SIZE; i++) { new_node = (Node *)malloc(sizeof(Node)); new_node->data = i*10; add_node_in_tail(new_node); } display_node(); reverse_link(&head); display_node(); free_link(); //////////////////////////////////////////////////////////////////////// // the head is a parameter /////////////////////////////////////////////////////////////////////// Node * link = NULL; for(int i=0; i<LOOP_LINK_SIZE; i++) { Node * new_node = (Node *)malloc(sizeof(Node)); new_node->data = 10 * i; link = add_node(link,new_node); } Node * link2 = NULL; for(int i=0; i<LOOP_LINK_SIZE; i++) { Node * new_node = (Node *)malloc(sizeof(Node)); new_node->data = 10 * i; link2 = add_node(link2,new_node); } make_loop(link); make_loop(link2); make_cross(link,link2); Node * meet_node = (Node *)malloc(sizeof(Node)); Node * tail_node = (Node *)malloc(sizeof(Node)); meet_node->data = INT_MAX; tail_node->data = INT_MAX; printf("%s\n",loop_link(link,&tail_node,&meet_node)?"It's loop":"No loop"); printf("%s\n",loop_link(link2,&tail_node,&meet_node)?"It's loop":"No loop"); printf("%s\n",cross_links(link,link2)?"cross":"no cross"); return 0; }
发表于
2011-08-27 20:47
huangkq1989
阅读(
129
) 评论(
0
)
收藏
举报
刷新页面
返回顶部
公告