链表初识(静态链表的增删改查)

定义静态链表


        struct link t1={1,NULL};
        struct link t2={5,NULL};
        struct link t3={2,NULL};
        struct link t4={8,NULL};

        struct link *head;
        head=&t1;
        t1.next=&t2;
        t2.next=&t3;
        t3.next=&t3;

      

1、增(尾插法和头插法)

尾插法:无需定义指针返回链表头地址

int insertNumberEnd(struct link *head,int data,struct link *new)   //无需返回指针地址
{
    struct link *p;
    p=head;
    while(p!=NULL)
    {
        if(p->data == data)
        {
            new->next=p->next;
            p->next=new;
        }
        p=p->next;
    }
    return 0;
}

头插法:需要定义指针接收head地址

struct link *insertNumberBefore(struct link *head,int data,struct link *new)    //头插法
{
    struct link *p=head;
    if(p->data == data)                //插入位置为链表头
    {
        new->next = p;
        return new;
    }
    while(p->next !=NULL)             //插入位置为链表内
    {
        if(p->next->data == data)
        {
            new->next=p->next;
            p->next=new;
            return head;
        }
        p=p->next;
    }
    printf("No Find\n");
    return head;
}

2、删

struct link *deleteNode(struct link *head,int data)
{
    struct link *p=head;
    if(p->data == data)
    {
        head=head->next;
        return head;
    }
    while(p->next !=NULL)
    {
        if(p->next->data == data)
        {
            p->next = p->next->next;
            return head;
        }
        p=p->next;
    }
    printf("It's not there. It can't be deleted");
}

3、查

int searchLink(struct link *head,int data)
{
   struct link *p;
   p=head;
   
   while(p!=NULL) 
   {
       if(p->data == data)
       {
           return 1;    //返回值为1时表示找到搜索到该数据
       }
       p=p->next;
   }
   return 0;          //返回值为0时表示找到搜索到该数据
}

4、改

int setNumber(struct link *head,int data,int newdata)
{
    struct link *p;
    p=head;
    
    while(p!=NULL)
    {
        if(p->data == data)
        {
            p->data = newdata;
            return 1;
        }
        p=p->next;
    }
    return 0;
}

例子:

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

struct link
{
    int data;
    struct link *next;
    
};

/*
函数说明:打印
参数:该链表的头结构体地址
*/
void dayin(struct link *head)
{
    while(head!=NULL){
        printf("%d ",head->data);
        head=head->next;
    }
}

/*
函数说明:头插法插入节点
参数:struct link *head:原本链表的头结构体地址
     data:需要进行头插法的数据位置
     struct link *head:插入节点结构体的地址
返回值:新链表的头结点地址
*/
struct link *insertBefore(struct link *head,int data,struct link *new)
{
    struct link *p=head;
    if(p->data == data){
        new->next=p;
        return new;
    }
    
    while(p->next!=NULL){
        if(p->next->data == data){
            new->next=p->next;
            p->next=new;
            return head;
        }
        p=p->next;
    }
    return head;
    
}


/*
函数说明:尾插法插入节点
参数:struct link *head:原本链表的头结构体地址
     data:需要进行尾插法的数据位置
     struct link *head:插入节点结构体的地址
返回值:新链表的头结点地址
*/
struct link *insertAfter(struct link *head,int data,struct link *new)
{
    struct link *p=head;
    
    if(p->data==data){
        p->next=new;
        return head;
    }
    
    while(p!=NULL){
        if(p->data == data){
           new->next=p->next;
           p->next=new;
           return head;
        }
        p=p->next;
    }
    return head;
    
}

/*
函数说明:删除节点
参数:struct link *head:原本链表的头结构体地址
     data:需要进行删除的数据
返回值:新链表的头结点地址
*/
struct link *delete(struct link *head,int data)
{
    struct link *p=head;
    if(p->data == data){
        head=p->next;
        return head;
    }
    
    while(p->next!=NULL){
        if(p->next->data == data){
            p->next=p->next->next;
            return head;
        }
        p=p->next;
    }
    return head;
    
}

/*
函数说明:修改数据
参数:struct link *head:原本链表的头结构体地址
     data:要改的数据
     new_data:新的数据
返回值:新链表的头结点地址
*/
struct link *update(struct link *head,int data,int new_data)
{
    struct link *p=head;
    
    while(p!=NULL)
    {
        if(p->data==data)
        {
            p->data = new_data;
            return head;
        }
        p=p->next;
    }
    
    return head;
}

/*
函数说明:查找数据
参数:struct link *head:原本链表的头结构体地址
     data:要查找的数据
返回值:1为查到了;0为没查到
*/
int search(struct link *head,int data)
{
    struct link *p=head;
    
    while(p!=NULL){
        if(p->data==data){
            return 1;
        }
        p=p->next;
    }
    return 0;
}

int main()
{
    int ret;
    struct link t1={1,NULL};
    struct link t2={5,NULL};
    struct link t3={2,NULL};
    struct link t4={10,NULL};
    struct link t_head={666,NULL};
    struct link t_tail={888,NULL};
  
    struct link *head;
    head=&t1;
    t1.next=&t2;
    t2.next=&t3;
    t3.next=&t4;
    
    printf("原始数据:");
    dayin(head);
    printf("\n");
    
    //头插法插入节点
    printf("头插法:");
    insertBefore(head,5,&t_head);
    dayin(head);
    printf("\n");
    
    //尾插法插入节点
    printf("尾插法:");
    insertAfter(head,2,&t_tail);
    dayin(head);
    printf("\n");
    
    //删除数据
    printf("删除数据(5):");
    delete(head,5);
    dayin(head);
    printf("\n");    
    
    //修改数据
    printf("修改数据(666):");
    update(head,666,777);
    dayin(head);
    printf("\n"); 
    
    //查找数据
    printf("查找数据7(显示为查到)\n");
    ret=search(head,7);
    if(ret==1){
        printf("查到了\n");
    }else{
        printf("没查到\n");
    }
    
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值