链表的创建,遍历,清除

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);

    }

}

posted on 2014-09-25 21:08  陈丰波  阅读(135)  评论(0编辑  收藏  举报