复习下C 链表操作(单向循环链表、查找循环节点)

循环链表 稍复杂点。

 肯能会有0 或 6 字型的单向循环链表。  接下来创建 单向循环链表 并 查找单向循环链表中的循环节点。

这里已6字型单向循环链表为例。

 

//创建 循环链表
Student * CreateCircleLink_Table(void){
    int i = 0;
    Student *head = NULL;
    head = (Student *)malloc(sizeof(Student));
    head->name[0]='\0';
    head->point = 0;
    head->stu = NULL;
    //循环节点
    Student *circleStu = NULL;
    Student *temp = NULL;
    Student *currentNode = head;
    while (i<=9) {

        temp = (Student *)malloc(sizeof(Student));
        strncpy(temp->name,"Node",sizeof(temp->name));
        temp->point = i;
        temp->stu = NULL;
        
        currentNode->stu = temp;
        currentNode = temp;
        
        //循环节点
        if (i==3) {
            circleStu = currentNode;
        }
        
        i++;
    }
    
    //最后 合并循环节点
    currentNode->stu = circleStu;
    
    return head;
}

//已知循环节点 查询 主要为了验证循环链表是否可用
void SelectCircleLinkTable(Student *student){
    Student *next = student->stu;
    int i = 0;
    Student *circleStu = NULL;

    while (next) {
        if (circleStu!=NULL&&next->point == circleStu->point) {
            printf("循环节点%d,结束循环\n",next->point);
            break;
        }
        
        if (i==3) {
            circleStu = next;
        }
        printf("index %d; studentName is %s;  point is %d\n",i,next->name,next->point);
        i++;
        next = next->stu;
    }
}

//查找循环链表中循环节点
Student * SelectCircleNodeInLinkTable(Student *student) {
    Student *fast = student;
    Student *slow = student;
    
    Student *circleStu = NULL;
    while (fast->stu) {
        fast = fast->stu->stu;//快指针节点 为慢指针节点的2倍
        slow = slow->stu;
        
        if (fast==NULL) { //不存在循环节点
            return NULL;
        }
        if (fast == slow) {//快慢指针相遇。找到循环节点
            circleStu = fast;
            break;
        }
        
    }
    
    if (fast==NULL) { //不存在循环节点
        return NULL;
    }
    printf("相遇节点为==%d\n",circleStu->point);
    fast=student;
    while (fast!=slow) {
        slow=slow->stu;
        fast=fast->stu;
    }
    return fast;
}



int main(void){
    char sf[15];
    
//    /**
//     *  创建单向链表
//     */
//    int num;
//    printf ("请输入学生人数\n");
//    scanf("%d",&num);
//    Student *link_stu = CreateLink_Table(num);
//
//    
//    /**
//     *  单向链表插入节点
//     */
//    printf ("请插入节点内容 在 已存在节点名字的后面,如 已存在节点名字|待插入名字|待插入分数 \n");
//    scanf("%s",sf);
//
//    link_stu = insertStudentLinkTable(link_stu,sf);
//    
//    /**
//     *  反转单向链表
//     */
//    printf("反转链表Y|N \n");
//    scanf("%s",sf);
//    if (strcmp(sf,"Y")==0) {
//       Student *newLt= ReversionStudentLinkTable(link_stu);
//        //查询
//        selectStudent(newLt);
//    }
    
    /**
     *  创建循环链表
     */
    Student *student = NULL;
    printf("开始创建循环链表Y|N \n");
    scanf("%s",sf);
    if (strcmp(sf,"Y")==0) {
      student =  CreateCircleLink_Table();
    }
    
    printf("已知情况查询循环链表Y|N \n");
    scanf("%s",sf);
    if (strcmp(sf,"Y")==0) {
        SelectCircleLinkTable(student);
    }
    
    
    printf("未知情况查询循环链表Y|N \n");
    scanf("%s",sf);
    if (strcmp(sf,"Y")==0) {
      Student *circleStu = SelectCircleNodeInLinkTable(student);
        printf("=====循环节点==%d\n",circleStu->point);
    }
    
    
    return 0;
}

参考这便:http://blog.csdn.net/wenqian1991/article/details/17452715

posted @ 2014-11-25 15:59  hhhker  阅读(768)  评论(0编辑  收藏  举报