美文网首页
线性表-循环单链表

线性表-循环单链表

作者: 掖莯圷 | 来源:发表于2018-09-02 00:02 被阅读0次

循环链表是另一种形式的链式存储结构。它的特点是表中最后一个结点的指针域指向头结点,整个链表形成一个环,从表中任何一结点出发均可找到表中其他结点
循环链表的操作和线性链表基本一致,差别仅在于算法中的循环条件不是p或p->next是否为空,而是在于它们是否等于头指针。


image.png

0、定义

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <malloc.h>

typedef int ElemType ;

struct Node{
    ElemType data;//数据域
    struct Node *next;//节点
};
typedef Node CircleList;

1、初始化链表

/**
 * 功能:初始化链表
 */
void Init_List(CircleList* head){
    head->next=head;
}

2、判断是否为空链表

/**
 * 功能:判断链表是否是空表
 * 参数:链表头指针
 * 返回值:true 是  false 否
 */
bool IsEmpty(CircleList *head){
    if(head->next==head){
        printf("CircleList为空表\n");
        return true;
    }
    return false;
}

3、获取链表长度

/**
 * 功能:获取链表长度
 * 参数:链表地址
 * 返回值:链表长度
 */
int Length_List(CircleList *head){
    CircleList* p=head->next;
    int length=0;
    while(p!=head){
        p=p->next;
        length++;
    }
    return length;
}

4、展示链表

/**
 * 功能:遍历链表
 * 参数:链表地址
 */
void Show_List(CircleList *head){
    if(head==NULL){
        printf("链表未初始化\n");
        return ;
    }
    if(head->next==head){
        printf("[]\n");
        return;
    }
    bool flag=true;
    printf("[");
    CircleList *p=head->next;//第一个节点
    while(p!=head){
      if(flag){
         printf("%d",p->data);
         flag=false;
      }else{
         printf(",%d",p->data);
      }
        p=p->next;
    }
    printf("]\n");
}

5、插入操作

  /**
 * 功能:向链表插入数据元素
 * 参数:链表地址 下标  插入的元素
 * 返回值:链表长度
 */
bool Insert_List(CircleList *head,int index, ElemType e){
    if(head==NULL){
        printf("链表未初始化\n");
        return false;
    }
    if(index<1||index>Length_List(head)+1){
        printf("下标越界\n");
        return false;
    }
    //开始插入
    int i;
    CircleList* pre=head;
    //遍历获取到插入位置的前一个元素
    for(i=1;i<index&&(pre->next!=head);i++){
        pre=pre->next;
    }
    //校验
    if(!pre||i>index){
        printf("搜索index位置元素失败\n");//已判断下标越界,不会存在这个问题
        return false;
    }
   CircleList* newNode=(CircleList*)malloc(sizeof(CircleList));
   newNode->data=e;
   newNode->next=pre->next;
   pre->next=newNode;
   return true;
}

6、删除元素

  /**
 * 功能:删除某个下标的数据元素
 * 参数:链表地址 下标  删除的元素
 * 返回值:链表长度
 */
bool Delete_List(CircleList *head,int index, ElemType *e){
    if(head==NULL){
        printf("链表未初始化\n");
        return false;
    }
    if(index<1||index>Length_List(head)){
        printf("下标越界\n");
        return false;
    }
    CircleList *pre,*temp;
    pre=head;
    int i=1;
     //遍历获取到删除的前一个元素
    while(pre->next!=head && i<index){
        pre=pre->next;
        i++;
    }
    temp=pre->next;
    *e=temp->data;
    pre->next=temp->next;
    free(temp);
    return true;
}

7、查找元素

/**
 * 功能:查找元素,返回下标
 * 参数:链表地址 元素
 * 返回值: 下标 -1 为查找不到
 */
int Search_List(CircleList *head,ElemType e){
    if(IsEmpty(head)){
        return -1;
    }
    CircleList *p=head->next;//第一个节点
    int i=1;
    while(p!=head){
        if(p->data==e){
            return i;
        }
        p=p->next;
        i++;
    }
    return -1;
}

8、获取下标的元素


/**
 * 功能:获取某个下标的元素
 * 参数:链表地址 下标
 * 返回值: true ,false
 */
bool GetElem(CircleList *head,int index,ElemType *e){
    if(IsEmpty(head)){
        return false;
    }
    if(index<1||index>Length_List(head)){
         printf("数组下标越界\n");
         return false;
    }
    CircleList *p=head->next;//第一个节点
    int j=1;
    while((p!=head)&&j<index){
       p=p->next;
       j++;
    }
    if((p==head)||j>index)return false;
    *e=p->data;
    return false;
}

9、清空链表

/**
 * 功能:清空线性表
 * 参数:pList:链表地址
 */
bool Clear_List(CircleList* head){
    CircleList *temp,*p;
    temp=head->next;//第一个节点
    while(temp != head){
        p=temp->next;
        free(temp);
        temp=p;
    }
    head->next=head;
    return true;
}

相关文章

  • 数据结构与算法之数组与链表

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

  • 数据结构与算法之栈与队列

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

  • 线性表的链式存储结构2.0

    这篇文章开启线性表的大版本更新2.0----循环链表 单循环链表 由前面关于单链表的介绍我们知道,在单链表中每个结...

  • 数据结构——线性表

    线性表分为顺序存储结构和链式存储结构(单链表,静态链表,循环链表,双向链表)。 单链表(**一种动态结构,所占空间...

  • 线性表(三)——双向链表的表示和实现

    在上篇文章中我们分析讨论了线性表的链式存储结构。链式存储结构表示的线性表主要分为单链表、单循环链表和双向循环链表三...

  • 数据结构-系列文章

    线性表 单链表 单链表-OC实现 双链表 循环链表 栈 栈 队列 待完善 数组 待完善 树 待完善 图 待完善 哈...

  • 线性表之顺序表和链表(单/双链表,单/双循环链表)

    线性表按存储方式分为顺序表和链表两种,其中链表又分为单链表,循环链表,双链表,双向循环链表。 顺序表 顺序表采用顺...

  • 学习数据结构第一弹 线性表(5)

    循环链表与双向链表 循环链表: 循环链表也是一种线性表的链式存储结构,其实他和单链表很像,其特点是它是一个环,也就...

  • 数据结构-线性表

    归纳 线性关系、线性表的定义,线性表的基本操作。 线性表的顺序存储结构与链式存储结构(包括单(向)链表、循环链表和...

  • 数据与算法结构

    线性表 顺序表 链表(物理上离散,逻辑上连续) 链表的类别 单链表 循环链表 双链表 链表的操作 顺序表与链表的比...

网友评论

      本文标题:线性表-循环单链表

      本文链接:https://www.haomeiwen.com/subject/qphpwftx.html