【唉呀哎呀】今天考試這個題

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

typedef struct _ListHead
{
    struct _ListHead* pre;
    struct _ListHead* next;
} ListHead;

typedef struct _Data
{
    ListHead list_head;
    int id;
} Data;

int main()
{
    int i =0;
    const int maxData = 32;
    Data* head = NULL;
    Data* tail = NULL;
    Data* newData= NULL;
    Data* pData= NULL;


    for(i = 0; i < maxData; i++)
    {
        newData = (Data*)malloc(sizeof(Data));
        if(!newData)
        {
            printf("no mem");
            return -1;
        }
        memset(newData, 0, sizeof(Data));

        newData->id = i;
        if(!head)
        {
            head = newData;
            tail = head;
        }
        else
        {
            tail->list_head.next = newData;
            newData->list_head.pre = tail;
            tail = newData;
        }
    }

    pData = head;
    if(!pData)
    {
        printf("null \n");
    }
    while(pData)
    {
        printf("id:%d\n",pData->id);
        pData = pData->list_head.next;
    }
    return 0;
}

 在辦公室一個小時都沒寫完,回來15分鐘測完啥狀況啊啊啊啊。blog

相關文章
相關標籤/搜索