单向链表的节点插入与删除

理解了链表的创建后,其他链表操作也变得比较简单,对上次的代码做了些修改,今天来记录一下自己对链表节点的插入与删除操作的学习。

在某个节点后插入节点

struct node *insert(struct node *head, int datanum, struct node *new)
{
    struct node *p1;

    if(head == NULL)//是空链表时
    {
        head = new;//让头结点指向新建的节点
        new->next = NULL;//新节点的下一位为空,只有头结点
        n = n+1;
        return head;
    }
    p1 = head;

    while(p1->datanum != datanum && p1->next != NULL)
    {
        p1 = p1->next;
    }

    if(p1->datanum == datanum)//在p1指向的节点的数据号后面插入新链表
    {
        new->next = p1->next;//p1的next保存的下一个节点位置给新节点的next
        p1->next = new;//新节点成为p1的下一个节点
        n = n+1;//节点数+1
    }
    else
    {
        printf("not found datanum%d\n",datanum);

    }
    return head;
 make5.c                                                                          

};

删除某个节点

struct node *delete(struct node *head, int datanum)
{
        struct node *p1;
        struct node *p2;
        if(head == NULL)//头指针为空则打印信息并返回
        {
            printf("空链表\n");
            return 0;

        }
        p1 = head;//p1保存头结点
        {
            p2 = p1;
            p1 = p1->next;
        }
        if(p1->datanum == datanum)//当找到需要删除的数据号时
        {
            if(p1 == head)//如果要删除头结点
            {
                head = p1->next;//让头结点后移一位

            }
            else
            {
                p2->next = p1->next;//把p1的next指向的节点让p2的next保存
            }
            free(p1);//释放掉p1
            p1 = NULL;//避免p1成为野指针
            printf("successed delete datanum%d\n", datanum);
            n = n-1;//节点少一个

        }
        else
        {

            printf("not found datanum%d\n",datanum);
        }
    return head;//返回头指针

};

代码示例,删除与插入

/*********************************************************************************
 *      Copyright:  (C) 2017 fanmaolin<fanmaolinn@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  make5.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(08/05/2017)
 *         Author:  fanmaolin <fanmaolinn@gmail.com>
 *      ChangeLog:  1, Release initial version on "08/05/2017 11:18:55 AM"
 *                 
 ********************************************************************************/

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

#define LEN sizeof(struct node)
struct node
{
    int datanum;//数据号
    int data;//存储数据
    struct node *next;//指向下一个节点
};

int n;//统计是第几个节点

struct node *creat()
{
    struct node *head;//头指针,指向第一个节点
    struct node *p1 = NULL;//p1保存创建的新节点的地址
    struct node *p2 = NULL;//p2保存原链表最后一个节点的地址

    n = 0;
    p1 = (struct node *)malloc(LEN);//新建一个节点
    p2 = p1;
    if(p1==NULL)//创建节点失败
    {
        printf("creat fail\n");
        return 0;
    }
    else
    {
        head = NULL;//头指针为空
        printf("please input %d node\n",n+1);
        scanf("%d %d",&(p1->datanum), &(p1->data));//输入数据
    }
    while((p1->datanum)!=0)    //循环进行的条件
    {
        n = n + 1;//计算创建的节点数
        if(n==1)
        {
            head = p1;//head保存头结点的位置
            p2->next = NULL;//因为前面p1 = p2,而且现在只有一个节点,把next设置为空
        }


            p2->next = p1;//当不是第一个节点时,p2的next指向p1新创建的节点
        }

        p2 = p1;//p2保存p1的指向的节点,为p1创建新的节点做准备
        printf("Please input %d node\n", n+1);
        scanf("%d %d", &(p1->datanum), &(p1->data));//向新的节点输入数据
    }

    p2->next = NULL;//最后一个节点指向为空

    free(p1);//释放结束时p1申请的内存空间
    p1 = NULL;//避免p1成为野指针
    return head;//返回头指针
};
void print(struct node *head)
{
    struct node *p;
    p = head;

    {
        //打印节点的地址,数据,下一个节点的地址
        p = p->next;//p指向下一个节点

    }while(p != NULL);
};

struct node *delete(struct node *head, int datanum)
{
        struct node *p1;
        struct node *p2;
        if(head == NULL)//头指针为空则打印信息并返回
        {
            printf("空链表\n");
            return 0;

        }
        p1 = head;//p1保存头结点
        {
            p2 = p1;
            p1 = p1->next;
        }
        if(p1->datanum == datanum)//当找到需要删除的数据号时
        {
            if(p1 == head)//如果要删除头结点
            {
                head = p1->next;//让头结点后移一位

            }
            else
            {
                p2->next = p1->next;//把p1的next指向的节点让p2的next保存
            }
            free(p1);//释放掉p1
            p1 = NULL;//避免p1成为野指针
            printf("successed delete datanum%d\n", datanum);
            n = n-1;//节点少一个

        }
        else
        {

            printf("not found datanum%d\n",datanum);
        }
    return head;//返回头指针

};


struct node *insert(struct node *head, int datanum, struct node *new)
{
    struct node *p1;

    if(head == NULL)//是空链表时
    {
        head = new;//让头结点指向新建的节点
        new->next = NULL;//新节点的下一位为空,只有头结点
        n = n+1;
        return head;
    }
    p1 = head;

    while(p1->datanum != datanum && p1->next != NULL)
    {
        p1 = p1->next;
    }

    if(p1->datanum == datanum)//在p1指向的节点的数据号后面插入新链表
    {
        new->next = p1->next;//p1的next保存的下一个节点位置给新节点的next
        p1->next = new;//新节点成为p1的下一个节点
        n = n+1;//节点数+1
    }
    else
    {
        printf("not found datanum%d\n",datanum);

    }
    return head;
 make5.c                                                                          

};

int main()
{
    struct node *head;
    struct node *new;

    int datanum;

    head = creat();
    print(head);
    /*插入一个节点*/
    new = (struct node *)malloc(LEN);//创建新节点
    printf("Please input insert node datanum and data:");
    scanf("%d %d", &(new->datanum), &(new->data));
    printf("insert behind datanum:");//选择在哪一个节点的数据号后面插入新节点
    scanf("%d", &datanum);
    head = insert(head, datanum, new);
    print(head);

    /*删除一个节点*/
    printf("you want delete :");//输入要删除的节点的数据号
    scanf("%d",&datanum);
    head = delete(head, datanum);
    print(head);

}

检查运行结果

[fanmaolin@Centeros lianbiao]$ gcc make5.c    
[fanmaolin@Centeros lianbiao]$ ./a.out 
please input 1 node
1
1
Please input 2 node
2
2
Please input 3 node
3
3
Please input 4 node
5
5
Please input 5 node
0
0
head is 226550020
p is 226550020 ,datanum is 1, data is  1, pnext is 226550060
p is 226550060 ,datanum is 2, data is  2, pnext is 226550120
p is 226550120 ,datanum is 3, data is  3, pnext is 226550160
p is 226550160 ,datanum is 5, data is  5, pnext is 0
Please input insert node datanum and data:4 666
insert behind datanum:3
head is 226550020
p is 226550020 ,datanum is 1, data is  1, pnext is 226550060
p is 226550060 ,datanum is 2, data is  2, pnext is 226550120
p is 226550120 ,datanum is 3, data is  3, pnext is 226550220
p is 226550220 ,datanum is 4, data is  666, pnext is 226550160
p is 226550160 ,datanum is 5, data is  5, pnext is 0
you want delete :5
successed delete datanum5
head is 226550020
p is 226550020 ,datanum is 1, data is  1, pnext is 226550060
p is 226550060 ,datanum is 2, data is  2, pnext is 226550120
p is 226550120 ,datanum is 3, data is  3, pnext is 226550220
p is 226550220 ,datanum is 4, data is  666, pnext is 0

//我在数据号为3的节点后面插入数据号为4的几点,插入数据为666;删除了数据号为5的节点
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值