2024.8.3

单向循环链表

#include <stdio.h>
#include <stdlib.h>
#include "./02循环.h"



//创建
looplist* cr(void)
{
    looplist* head=(looplist*)malloc(sizeof(looplist));
    if(head==NULL)
    {
        printf("创建失败\n");
        return NULL;
    }
    head->text.len=0;
    head->next=head;
    return head;
}
//头插法
void toucha(looplist *head,hihi num)
{
    looplist* temp=(looplist*)malloc(sizeof(looplist));
    if(temp==NULL)
    {
        printf("创建失败\n");
        return;
    }
    temp->text.data=num;
    temp->next=NULL;

    temp->next=head->next;
    head->next=temp;                                              
    head->text.len++;
    return;

}
//头删
void touchan(looplist* head)
{
    if(head->next==NULL)
    {
        printf("失败\n");
        return;
    }
    looplist* temp;
    temp=head->next;
    head->next=temp->next;                                  
    free(temp);
    head->text.len--;
    return;
}
//尾插法
void inwei(looplist* head,hihi num)
{
    looplist* temp=(looplist*)malloc(sizeof(looplist));
    if(temp==NULL)
    {
        printf("创建失败\n");
        return;
    }
    temp->text.data=num;
    temp->next=NULL;

    looplist* p=head;
    while(p->next!=head)
    {
        p=p->next;
    }
    temp->next=p->next;
    p->next=temp;
    head->text.len++;
    return;
}
//尾删
void weishan(looplist* head)
{
    if(head->next==head)
    {
        printf("失败\n");
        return;
    }
    looplist* p=head;
    while(p->next->next!=head)
    {
        p=p->next;
    }
    looplist* temp=p->next;
    p->next=head;
    free(temp);
    head->text.len--;
    return;
}
 //从位置插入
 void locationin(looplist* head,hihi n,hihi num)
 {
     if(n<1)
     {
         printf("位置非法\n");
         return;
     }
     looplist* p=head;
     for(int i=0;i<n-1;i++)
     {
         p=p->next;
         if(p==head)
         {
             printf("位置非法\n");
             return;
         }
     }
     looplist* temp=(looplist*)malloc(sizeof(looplist));
     if(temp==NULL)
     {
         printf("创建失败\n");
         return;
     }
     temp->text.data=num;
     temp->next=NULL;
 
     temp->next=p->next;
     p->next=temp;
     head->text.len++;
     return;
 }
//从位置删除
void locationinde(looplist* head,hihi n)
{
    if(head->next==head)
    {
        printf("链表为空,无法删除\n");
        return;
    }

        if(n<1)
    {
        printf("位置非法\n");
        return;
    }
    looplist* p=head;
    for(int i=0;i<n-1;i++)
    {
        p=p->next;
        if(p->next==head)
        {
            printf("位置非法\n");
            return;
        }
    }
    looplist* temp=p->next;
    p->next=temp->next;
    free(temp);
    head->text.len--;
    return;
}
//遍历
void show(looplist* head)
{
    looplist* p=head;
    while(p->next!=head)
    {
        p=p->next;
        printf("%d ",p->text.data);
    }
    putchar(10);
    return;
}
                                      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值