链表——头标法插入

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

struct Test
{
    int data;
    struct Test* next;
};
void printList(struct Test* head)
{
    struct Test* point;
    point = head;
    while(point != NULL)
    {
        printf("%d ",point->data);
        point = point->next;
    }
}
struct Test* InsertfromHead(struct Test* head,struct Test* new)
{//头标法插入
    if(head == NULL)
    {
        head = new;
       
    }else
    {
        new->next = head;
        head = new;
    }

    return head;

}
struct Test* CreadList(struct Test* head)
{//创建结点
    struct Test *new  = NULL;
    while(1)
    {
        new = (struct Test*)malloc(sizeof(struct Test));
        printf("Please you input data to list\n");
        scanf("%d",&(new->data));
        if(new->data == 0)
        {
            printf("you input 0 quit!\n");
            free(new);
            return head;
        }

           head = InsertfromHead(head,new);//将链表头,和要插入的数据传到头标法中
    }

}
int main()
{
    struct Test* head = NULL;

    head = CreadList(head);

    printList(head);

    return 0;
}
posted @ 2022-09-14 17:19  信号编程好难  阅读(25)  评论(0)    收藏  举报