2018-09-13

用c语言创建单链表,参数采用二级指针。

源码如下:

//单链表头插法创建

#include

#include

#define flag -1

//创建节点

typedef struct Node{

  int data;

  struct Node *next;

} LNode,*listNode;

//头插法创建单链表

void creatListNodeOfHead(listNode * L){ //注意有时指针不一定是传地址

    LNode *head;//head只是暂时指向申请的空间

    int input;

    (*L) = NULL;

    printf("传递的指针参数%p\n",L);

    printf("指针内容%p\n",(*L));

    printf("请输入:");

    scanf("%d",&input);

    while(input != flag){

        printf("继续输入...\n");

        head = (listNode)malloc(sizeof(LNode));

        head->data = input;

        head->next = (*L);

        *L = head;

        //printf("head创建完成\n");

        scanf("%d",&input);

        //printf("%d",(*L)->data);

    }

}

int main(){

    //使用头插法

    LNode *L,*temp = NULL;

    printf("原指针地址%p\n",&L);

    creatListNodeOfHead(&L);

    printf("输入结束,开始测试\n");

    temp = L;

    while(temp!=NULL){

        printf("%d ",temp->data);

        temp = temp->next;

    }

    return 0;

}

你可能感兴趣的:(2018-09-13)