链表的创建,遍历,清除

本文介绍了一种使用C++语言创建动态链表的方法,并详细解释了如何在链表中插入节点以及释放内存的过程。通过实例演示了链表的创建、数据填充、遍历和内存释放,确保程序资源的有效管理。

node *creatline(int n)

{

    node *head=(node *)malloc(sizeof(node));

    head->data=rand()%100;

    head->next=NULL;

    node *p=head;

    int i=0;

    while (i<n-1) {

        p->next=(node *)malloc(sizeof(node));

        p->next->data=rand()%100;

        p->next->next=NULL;

        p=p->next;

        i++;

    }

    return head;

}

void printline(node *p)

{

    while (p!=NULL) {

        printf("%d\n",p->data);

        p=p->next;

    }

}

void releaseline(node *p)

{

    node *q=p->next;

    while (p!=NULL)

    {

        printf("%d\n",p->data);

        free(p);

        p=q;

        if (q!=NULL)

        {

            q=p->next;

        }

    }

    

}

void releaseline2(node *p)

{

    node *q=p->next;

    printf("%d\n",p->data);

    free(p);

    if (q!=NULL) {

        releaseline2(q);

    }

}

转载于:https://www.cnblogs.com/a514875560/p/3993713.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值