链表的增删改查常用方法

链表的定义

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

链表的遍历

void printLink(struct Test *head){
        struct Test *point = head;//定义point链表指针作为索引指向链表头
        while(point != NULL){
                printf("%d ",point -> data);//打印链表头数据
                point = point -> next;//point指针指向链表下一节
        }
        putchar('\n');
}

链表从指定节点后方插入新节点

void inseartback(struct Test *head,int data,struct Test *new){
        struct Test *p = head;
        while(p != NULL){
                if(p -> data == data){
                        new -> next  = p -> next;
                        p -> next = new;

                }
                p = p -> next;
        }
}

链表从指定节点前方插入新节点

两种情况

  1. 指定节点为头节点
  2. 指定节点不是头节点
struct Test* inseartfront(struct Test *head,int data,struct Test *new){
        struct Test *p = head;
        if(head -> data == data){
                new -> next = head;//新的节点指向头节点成为新的头节点
                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 Test *delete(struct Test *head,int data){
        struct Test *p = head;
        if(head -> data == data){//删除节点为头节点
                head = head-> next;
                return head;
        }
        while(p != NULL){
                if(p -> next -> data == data){
                        p -> next = p -> next -> next;
                        return head;
                }
                p = p -> next;
        }
        return head;
}

链表修改指定节点

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

头插法的动态创建

struct Test *inserthead(struct Test *head,struct Test *new){
        if(head == NULL){
                head = new;
        }else{
                new -> next = head;
                head = new;
        }
        return head;
}
struct Test *creathead(struct Test *head){
        while(1){
            struct Test *new;
            new = (struct Test*)malloc(sizeof(struct Test));
            scanf("%d",&(new -> data));
            if(new -> data == 0){
                    printf("0 quit\n");
                    free(new);
                    return head;
            }
            head = inserthead(head,new);
        }
}

尾插法创建链表

struct Test *insertbehing(struct Test *head,struct Test *new){
        struct Test *p = head;
        if(head == NULL){
                head = new;
                return head;
        }
        while(p -> next != NULL){
                p = p -> next;
        }
        p -> next = new;
        return head;
}
struct Test *creathead2(struct Test *head){
        while(1){
        struct Test *new;
                new = (struct Test*)malloc(sizeof(struct Test));
                scanf("%d",&(new -> data));
        if(new -> data == 0){
                printf("0 quit\n");
                free(new);
                return head;
        }
                head = insertbehing(head,new);
        }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从入门到捕蛇者说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值