当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++进阶与实例

C - Create and Free List

    #include <stdlib.h>

    #define MAX 10

    /*struct define*/
    struct List {
        /*strurct node date*/
        int             number;
        char            name[MAX];

        /*struct node node , to next node point*/
        struct List     *Next;
    };
    /*struct type define*/
    typedef struct List Node;
    /*struct point define*/
    typedef Node        *Link;

    int main() {
        /*struct-point type date*/
        Link            New;

        /*node date , to save node*/
        int             newNumber;
        char            newName[MAX];

        int             i;

        printf("assign memory , to save node ! ");
        /*assign memory , to save node*/
        New = (Link)malloc(sizeof(Node));

        if(New == NULL) {
            printf("memory assign error");
        }
        else {
            printf("input newNumber , to node :");
            scanf("%d",&newNumber);
            printf("input newName , to node :");
            scanf("%s",newName);

            New->number = newNumber;
            for(i=0;i<=MAX;i++) {
                New->name[i] = newName[i];
            }
            New->Next = NULL;

            printf("output , from you input : ");
            printf("%d ",New->number);
            printf("%s ",New->name);
        }

        printf("free memory , to lost node ! ");
        free(New);

        return 0;
    }

    -----------------------------------------------------
     [root@localhost CTest]# ./CreateAndFreeList
    assign memory , to save node !
    input newNumber , to node :10001
    input newName , to node :1234567890
    output , from you input : 10001 1234567890
    free memory , to lost node !
    [root@localhost CTest]# vi CreateAndFreeList.c

相关内容
赞助商链接