数据结构day2

#include <stdio.h>
#include <stdlib.h>
#include "./func.h"
//创建单链表
struct node* link()
{
    struct node* head=(struct node*)malloc(sizeof(struct node));
    if(NULL==head)
    {
        printf("单链表创建失败\n");
        return NULL;
    }   
    head->txt.len=0;
    head->next=NULL;
    
    return head;
}   

//头插
void headmeg(struct node* head,datatype data)
{
    struct node*temp=(struct node*)malloc(sizeof(struct node));
    if(NULL==temp)                                                                      
    {
        printf("插入数据失败\n");
        return;
    }   
    //初始化
    temp->txt.data=data;
    temp->next=NULL;
    
    temp->next=head->next;
    head->next=temp;
    head->txt.len++;
}   

//尾插
void winser(struct node *head,int data)
{
    struct node *temp=(struct node*)malloc(sizeof(struct node));
    struct node *p=head;
    //初始化
    temp->txt.data=data;
    temp->next=NULL;
    if(NULL==temp)
    {
        printf("创建失败\n");
    }
    while(p->next!=NULL)
    {                                                            
        p=p->next;
    }
    
    p->next=temp;
    head->txt.len++;
    printf("插入成功\n");
    
}
//遍历链表
void showpoint(struct node *head)
{
    if(NULL==head)
    {
        printf("链表出错\n");
        return;
}
//按位置插入
void wzinsert(struct node* head,int index,datatype data)
{                                                                       
    if(index<1 || index>head->txt.len+1)
    {
        printf("插入位置非法");
        return;
    }
    if(NULL == head)
    {
        printf("传参失败");
        return;
    }
    struct node* p=head;
    //创建temp结点并初始化
    struct node* temp=(struct node*)malloc(sizeof(struct node));
    if(NULL == temp)
    {
        printf("创建失败");
        return;
    }
    temp->txt.data=data;
    temp->next=NULL;

    //移动指针
    for(int i=0;i<index-1;i++)
    {
        p=p->next;
    }
    temp->next=p->next;
    p->next=temp;
    head->txt.len++;
    return;
}
//按位置删除
void del(struct node *head,int index)
{
    if(index<=0 || index>head->txt.len)
    {
        printf("位置不合法\n");
    }
    struct node *p=head;
    for(int i=0;i<index-1;i++)
    {
        p=p->next;
    }
    //备份删除节点
    struct node *temp=p->next;
    p->next=temp->next;
    free(temp);
    head->txt.len--;
    printf("删除成功\n");

//快慢指针
struct node *kM_list(struct node *head)
{                                                    
 
    //判断链表是否为空
    if(head->next==NULL)
    {
        printf("链表为空\n");
    }
    //定义指针
    struct node *low,*K;
    low=K=head->next;
    while(K->next!=NULL && K->next->next!=NULL)
    {   
        low=low->next;
        K=K->next->next->next;
    }
    return low;
}

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值