阅读背景:

链表创建与释放_weixin_33857230的博客

来源:互联网 
#include <stdio.h>
#include <stdlib.h>

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

void printList(struct node *node)
{
    while(node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
    printf("\n");
}

void freeList(struct node *node)
{
    struct node *tmp = NULL;
    while(node != NULL) {
        tmp = node;
        node = node->next;
        free(tmp);
        tmp = NULL;
    }
}

void push(struct node **head_ref, int new_data)
{
    struct node *new_node = (struct node *)malloc(sizeof(struct node));
    new_node->data = new_data;
    new_node->next = *head_ref;
    (*head_ref) = new_node;
}

int main(int argc, char *argv[])
{
    struct node *res = NULL;
    push(&res, 15);
    push(&res, 34);
    push(&res, 16);
    push(&res, 11);
    push(&res, 10);
   
    printList(res);
    freeList(res);
    return 0;
}#includ



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: