3.线性表-单向循环链表

为了方便,本文介绍的单向循环链表不包含头节点

单向循环链表内容

  • 单向循环链表的的定义
  • 单向循环链表的创建
  • 单向循环链表插入
  • 单向循环链表的删除
  • 单向循环链表的查询
单向循环链表的的定义

在单链表中,将终端结点的指针域NULL改为指向表头结点或开始结点
它的有点是:从任一结点出发都可访问到表中所有结点。

单向循环链表的创建

单向循环链表有两种表现形式,如下图:


表现形式

在创建单向循环链表,分为两种情况:
第一次创建、已创建,新增元素,所以,思路如下
判断是否第一次创建
YES:创建节点,将指针域指向自己
NO:找到链表结尾的位置
创建新的节点
尾节点->next=新节点
新节点->next=头节点

//单向循环链表的创建
Status createSCycleList(Linklist *node){
    int item;
    
    Linklist temp;
    Linklist tail = NULL;//记录最后一个节点
    while (1) {
        scanf("%d", &item);
        
        if (item == 0)
            break;
        
        if (*node == NULL) {
            //节点为空时,第一次创建,将节点的next指向自己,同时记录尾节点
            *node = malloc(sizeof(lNode));
            if (*node == NULL)
                return ERROR;
            (*node)->data=item;
            (*node)->next = *node;
            tail = *node;
        }else{
            
            //创建节点
            temp = malloc(sizeof(lNode));
            if (temp == NULL)
                return ERROR;
            temp->data=item;
            //指向头节点
            temp->next = *node;
            //尾节点指向新建节点
            tail->next = temp;
            tail = temp;
        }
    }
    
    return OK;
}
//在其他地方调用
LinkList sCycleList;
createSCycleList(&sCycleList);

上面一直是把节点插在后面,下面这个是把节点插在前面

Status createSCycleList(Linklist *node){
    int item;
    
    Linklist temp;
    Linklist tail = NULL;//记录最后一个节点
    while (1) {
        scanf("%d", &item);
        
        if (item == 0)
            break;
        
        if (*node == NULL) {
            //节点为空时,第一次创建,将节点的next指向自己,同时记录尾节点
            *node = malloc(sizeof(lNode));
            if (*node == NULL)
                return ERROR;
            (*node)->data=item;
            (*node)->next = *node;
            tail = *node;
        }else{
            
            //创建节点
            temp = malloc(sizeof(lNode));
            if (temp == NULL)
                return ERROR;
            temp->data=item;
            //指向头节点
            temp->next = *node;
            //尾节点指向新建节点
            tail->next = temp;
            //将头指针指向新首元节点
            *node = temp;
        }
    }
    
    return OK;
}

单向循环链表插入

单向循环链表插入分为两种情况:
如果是在第一个位置:
1.首先创建新节点p,将值赋值给p节点的数据域;
2.将p->next指向首元节点node;
3.遍历找到尾节点tail,将tail->next指向新的首元节点p
4.将头指针指向新的首元节点:*node = p

如果不是在第一个位置:
1.找到要插入位置的前一个节点p;
2.将新建节点的next指向p->next;
3.将p->next指向新建节点

//单循环链表插入
Status insertCycleList(Linklist *node,int place,int num){
    
    //链表为空
    if (*node == NULL)
        return ERROR;
    
    //插入位置不对
    if (place < 1)
        return ERROR;
    
    Linklist target,temp;
    int i = 0;
    if (place == 1) {
        //创建temp
        temp = malloc(sizeof(lNode));
        if (temp == NULL)
            return ERROR;
        temp->data = num;
        //找尾节点
        //尾节点下一个节点等于头节点时,找到尾节点
        for (target = *node; target->next != *node; target = target->next);
        
        //新节点->next
        temp->next = *node;
        //尾节点->next = 新节点
        target->next = temp;
        //让头节点 = temp
        *node = temp;
    }else{
        //创建新节点 temp
        temp = malloc(sizeof(lNode));
        if (temp == NULL)
            return ERROR;
        temp->data = num;
        
        //找到插入前的一个位置target
        for (i = 1,target = *node; target->next != *node && i != place-1 ; target = target->next,i++);
        
        if (*node == target->next && (i+1) < place) {
            //第i-1位置的节点的下一个节点是头节点,且插入位置大于末尾位置,超出插入范围
            //比如,只有四个元素,你可以插在第五个的位置,你不能插入到第六个位置
            return ERROR;
        }
        
        //新节点next指向target->next
        temp->next = target->next;
        //target后继是新节点
        target->next = temp;
        
    }
    
    return OK;
}
单向循环链表的删除

单向循环链表的删除分为两种情况:
如果是在第一个位置:
1.遍历找到尾节点tail;
2.temp记录首元节点;
3.首元节点=首元节点->next
4.尾节点指向首元节点,同时释放temp

如果不是在第一个位置:
1.找到要删除位置的前一个节点p;
2.将p->next赋值要删除的节点q;
3.将p->next指向q->next,并释放q

//单循环链表的删除
Status deleteCycleList(Linklist *node,int place){
    
    if(*node == NULL || place < 1) return ERROR;
    
    lNode *target,*temp;
    int i = 0;
    
    //如果只有一个节点
    if ((*node)->next == *node) {
        if (place > 1) {
            return ERROR;
        } else {
            temp = *node;//记录该节点
            *node = NULL;//将头节点指向NULL
            free(temp);//释放该节点
            return OK;
        }
    }
    
    if (place == 1) {
        //找尾节点
        //尾节点下一个节点等于头节点时,找到尾节点
        for (target = *node; target->next != *node; target = target->next);
        temp = *node;
        *node = (*node)->next;
        target->next = *node;
        free(temp);
    } else {
        //找到删除前的一个位置target
        for (i = 1,target = *node; target->next != *node && i != place-1 ; target = target->next,i++);
        
        if (*node == target->next) {//第i-1位置的节点的下一个节点是头节点,超出删除范围
            return ERROR;
        }
        
        temp = target->next;
        target->next = temp->next;
        free(temp);
    }
    
    return OK;
}
单向循环链表的查询
//单循环链表的查询
int findNodeFromCycleList(Linklist node, int value){
    
    if (node == NULL) {
        return -1;
    }
    
    int i = 1;
    Linklist temp = node;
    while (temp && temp->data != value && temp->next != node) {
        i++;
        temp = temp->next;
    }
    
    //防止没有找到,返回了1
    if (temp->next == node && temp->data != value) {
        return -1;
    }
    return i;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转自:http://blog.csdn.net/oreo_go/article/details/52116214 ...
    YYT1992阅读 1,077评论 0 4
  • 有头链表(注意:头结点有的地方是全局变量) 初学者学自于大话数据结构,不足及错误的地方请读者提出来,谢谢。 可加 ...
    lxr_阅读 840评论 0 2
  • # 单项循环列表 ``` typedef int ElemType; typedef int Status; //...
    天地逍遥阅读 358评论 0 0
  • 古有吴三桂怒发冲冠为红颜,在那个时候为男人保护自己的女人,用行动传出了流芳千古佳话。当今社会,还有人为了红颜去拼命...
    为谁舞倾城阅读 384评论 1 7
  • 从早上六点半直忙到下午六点半,头晕目眩的感觉,回到家刚一进门,听到的第一个好消息是孩子放学回来作业已全部完成;第二...
    摇滚海豚阅读 239评论 0 0