首页 > 其他分享 >设计单向循环链表的接口

设计单向循环链表的接口

时间:2024-04-23 21:23:20浏览次数:12  
标签:head 单向 Head CircLList next 链表 接口 New

/**********************************************************************************
*

  •      file name:  003_单向循环链表.c
    
  •      author   :  [email protected]
    
  •      date     :  2024/04/23
    
  •      function :  设计单向循环链表的接口
    
  •      note     :  None
    
  •      CopyRight (c) 2024-2024  [email protected]    All Right Reseverd
    
  • *******************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>


//指的是单向循环链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int  DataType_t;

//构造单向循环链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct CircularLinkedList
{
	DataType_t  		 data; //结点的数据域
	struct CircularLinkedList	*next; //结点的指针域

}CircLList_t;


//创建一个空单向循环链表,空链表应该有一个头结点,对链表进行初始化
CircLList_t * CircLList_Create(void)
{
	//1.创建一个头结点并对头结点申请内存
	CircLList_t *Head = (CircLList_t *)calloc(1,sizeof(CircLList_t));
	if (NULL == Head)
	{
		perror("Calloc memory for Head is Failed");
		exit(-1);
	}

	//2.对头结点进行初始化,头结点是不存储数据域,指针域指向自身,体现“循环”思想
	Head->next = Head;

	//3.把头结点的地址返回即可
	return Head;
}

//创建新的结点,并对新结点进行初始化(数据域 + 指针域)
CircLList_t * CircLList_NewNode(DataType_t data)
{
	//1.创建一个新结点并对新结点申请内存
	CircLList_t *New = (CircLList_t *)calloc(1,sizeof(CircLList_t));
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

	//2.对新结点的数据域和指针域进行初始化
	New->data = data;
	New->next = NULL;

	return New;
}

//头插
bool CircLList_HeadInsert(CircLList_t *head,DataType_t data)
{
    //将传进来的数据创建一个新的节点
    CircLList_t *New = CircLList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
		return false;
    }
    //定义一个变量存储尾节点
    CircLList_t *tmp = head->next;
    //判断是否为空链表
    
    if (head->next == head)
    {
        head->next = New;
        New->next = New;
        return true;
    }
    //遍历到尾节点
    while (tmp->next != head->next)
    {
        tmp = tmp->next;
    }
    //将新节点插入头部
    New->next = head->next;
    tmp->next = New;
    
    head->next = New;
    return true;
    
}

//尾插
bool CircLList_TailInsert(CircLList_t *head,DataType_t data)
{
    //将传进来的数据创建一个新的节点
	CircLList_t *New = CircLList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
		return false;
    }
    CircLList_t *tmp = head->next;
    //判断是否为空链表
    if (head->next == head)
    {
        head->next = New;
        New->next = New;
        return true;
    }
    //遍历到尾节点
    while (tmp->next != head->next)
    {
        tmp = tmp->next;
    }
    tmp->next = New;
    New->next = head->next;
    return true;
    
    
    
    

}


//指定位置插入(找链表中已存在的数据的后面插)
bool CircLList_DestInsert(CircLList_t *head,DataType_t destval,DataType_t data)
{
	//将传进来的数据创建一个新的节点
	CircLList_t *New = CircLList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
		return false;
    }
    //判断是否为空链表
    if (head->next == head)
    {
        head->next = New;
        New->next = New;
        return true;
    }
    //定义两个变量记录头节点和首节点地址
    CircLList_t *tmp1 = head;
    CircLList_t *tmp2 = head->next;
    while (tmp2->next != head->next)
    {
        tmp2 = tmp2->next;
        tmp1 = tmp1->next;
        if (tmp2->data == destval)
        {  
            //指定位置刚好在尾部(后插)
            if (tmp2->next == head->next)
            {
                New->next = tmp2->next;
                tmp2->next = New;
                New->next = head->next;
                return true;
            }
            New->next = tmp2->next;
            tmp2->next = New;
        }
        else if (tmp1->data == destval)
        {
            //指定位置刚好在头部(后插)
            if (tmp1 == head->next)
            {
                New->next = tmp1->next;
                tmp1->next = New;
                return true;
            }
        }
    }
    return true;
}


//遍历链表
bool CircLList_Print(CircLList_t *Head)
{
	//对单向循环链表的头结点的地址进行备份
	CircLList_t *Phead = Head;
	
	//判断当前链表是否为空,为空则直接退出
	if (Head->next == Head)
	{
		printf("current linkeflist is empty!\n");
		return false;
	}

	//从首结点开始遍历
	while(Phead->next)
	{
		//把头结点的直接后继作为新的头结点
		Phead = Phead->next;

		//输出头结点的直接后继的数据域
		printf("data = %d\n",Phead->data);

		//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
		if (Phead->next == Head->next)
		{
			break;
		}	
	}

	return true;
}
//头删
bool LList_HeadDel(CircLList_t *Head)
{
	//对单向循环链表的头结点的地址进行备份
	CircLList_t *Phead = Head->next;
    CircLList_t *tmp = Head->next;
    //链表为空的情况下
    if (Head->next == Head)
    {
        printf("此链表为空!没有元素可以删除!");
        return false;
    }
    //遍历尾节点
    while(tmp->next != Head->next)
    {
        tmp = tmp->next;
    }
    Head->next = Phead->next;
    tmp->next = Phead->next;
    Phead->next = NULL;
    free(Phead);
    return true;
}
//尾删
bool LList_TailDel(CircLList_t *Head)
{
    //对单向循环链表的头结点的地址进行备份
    CircLList_t *Phead = Head;
    CircLList_t *tmp = Head->next;
    //链表为空的情况下
    if (Head->next == Head)
    {
        printf("此链表为空!没有元素可以删除!");
        return false;
    }
    //遍历尾节点
    while(tmp->next != Head->next)
    {
        Phead = Phead->next;
        tmp = tmp->next;
    }
    Phead->next = tmp->next;
    tmp->next = NULL;
    free(tmp);
    return true;
}
//指定删
bool CircLList_DestDel(CircLList_t *Head,DataType_t destval)
{
    //链表为空的情况下
    if (Head->next == Head)
    {
        printf("此链表为空!没有元素可以删除!");
        return false;
    }
    CircLList_t *tmp1 = Head;
    CircLList_t *tmp2 = Head->next;
    while (tmp2->next != Head->next)
    {
        tmp2 = tmp2->next;
        tmp1 = tmp1->next;
        if (tmp2->data == destval)
        {  
            //指定位置刚好在尾部
            if (tmp2->next == Head->next)
            {
                tmp1->next = tmp2->next;
                tmp2->next = NULL;
                free(tmp2);
                return true;
            }
            tmp1->next = tmp2->next;
            tmp2->next = NULL;
            free(tmp2);
            
        }
        //指定位置刚好在头部
        else if (tmp1->data == destval)
        {
            
            if (tmp1 == Head->next)
            {
                CircLList_t *Phead = Head->next;
                // 遍历到尾节点
                while (Phead->next != Head->next)
                {
                    Phead = Phead->next;
                }
                Head->next = tmp1->next;
                Phead->next = tmp1->next;
                tmp1->next = NULL;
                free(tmp1);
                return true;
            }
        }
        
        
    }
    printf("该链表中没有这个数!\n");
}



int main(int argc, char const *argv[])
{
	CircLList_t *head = CircLList_Create();
    //头插入
    CircLList_HeadInsert(head,5);
    CircLList_HeadInsert(head,6);
    CircLList_HeadInsert(head,7);
    CircLList_HeadInsert(head,8);
    CircLList_HeadInsert(head,9);
    CircLList_Print(head);

    //尾插
    printf("尾插法:\n");
    CircLList_TailInsert(head,55);
    CircLList_Print(head);
    //指定位置插入(找链表中已存在的数据的后面插)
    printf("指定插:\n");
    CircLList_DestInsert(head,9,99);
    CircLList_DestInsert(head,6,909);
    CircLList_DestInsert(head,55,9900);
    CircLList_Print(head);
    // LList_HeadDel(head);
    // LList_TailDel(head);
    printf("\n");
    CircLList_DestDel(head,9900);

    CircLList_DestDel(head,91);
    
    CircLList_Print(head);
	return 0;
}

标签:head,单向,Head,CircLList,next,链表,接口,New
From: https://www.cnblogs.com/wwwwariana/p/18153763

相关文章

  • 单项循环链表的头插、尾插、中间插、头删、尾删、中间删
    单项循环链表的头插、尾插、中间插、头删、尾删、中间删/******************************************************************** author :[email protected]* date :2024/04/23* function:单项循环链表的头插、尾插、中间插、头删、尾删、中间删* note :Non......
  • 性能测试——压测工具jmeter接口测试
    柠檬班jmeter教程参考:https://www.bilibili.com/video/BV1st411Y7QW/?spm_id_from=333.337.search-card.all.click&vd_source=79bbd5b76bfd74c2ef1501653cee29d6 黑马jmeter教程参考:https://www.bilibili.com/video/BV1ty4y1q72g/?spm_id_from=333.337.search-card.all.click&v......
  • 数据结构:单循环链表的创建插入与删除
    数据结构:单循环链表的创建·插入·删除/***@filename: 单循环链表的创建·插入·删除*@brief实现单循环链表的创建删除插入的功能*@[email protected]*@date2024/04/23*@version1.0:版本*@notenoone*CopyRight(c)2023-2024liuliu@......
  • 单向循环链表的接口程序
    /**@filename: main.c@brief单向循环链表的接口程序@[email protected]@date2024/04/[email protected]:版本@property:属性介绍@note补充注意说明CopyRight(c)[email protected]*/include<stdio.h>include......
  • 链表的查找操作例题
    代码/********************************************name:find*function:查找链表倒数第k位置的结点*argument:@head:头指针@k:链表倒数第k位置的结点数*retval:None*date:2024/04/22*note:Note*********************......
  • 接口测试方法:Spring boot Test、python、postman
    一般的rest接口在pom.xml中加入org.springframework.bootspring-boot-starter-testtest新建测试类@RunWith(SpringRunner.class)@SpringBootTestpublicclassUserControllerTest{privateMockMvcmvc;//初始化执行@BeforepublicvoidsetUp()throwsException......
  • JMeter 做接口加密测试
    JMeter怎么做接口的加密?JMeter如果需要做加密测试,是需要加密类型对应的jar包的。本文以MD5,加密作为教程。 1、在TestPlan引用jar包; 2、添加BeanShellSampler取样器,并输入调用代码 importmd5.mymd5;//调用jar包StringpassAftermd5=mymd5.getMd5("1234");//......
  • SpringBoot整合OpenAPI 3生成在线接口文档
    SpringBoot整合springdoc-openapi,自动生成在线接口文档。1、引入pom依赖<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.3.0</version></dependenc......
  • springboot 接口限制访问频率
     1.自定义注解@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)public@interfaceRateLimit{//默认最大访问次数intvalue()default3;//默认时间窗口(秒)longduration()default60;} 2.创建拦截器处理频率逻辑@Slf4......
  • 用edge_tts和Flask写一个语音生成接口
    1、安装Flask和edge_ttspipinstalledge-ttspipinstallflask[async]2、接口调用用application/json,POST参数:例子{"text":"现在是11:30分=,小爱提醒您,现在要出发了,请注意时间","lang":"zh-CN-YunxiNeural"}3、完整代码fromflaskimportFlask,requestim......