帶頭結點與不帶頭結點的單鏈表實現

不帶頭結點:spa

typedef struct LNode {
    int data;
    struct LNode *next;
}LNode,*LinkList;

//初始化一個空的單鏈表
bool InitList(LinkList &L) {
    L = NULL;
    return true;
}

void test() {
    LinkList L;
    InitList(L);
}

帶頭結點:code

typedef struct LNode {
    int data;
    struct LNode *next;
}LNode, *LinkList;

bool InitList(LinkList &L) {
    L = (LNode *)malloc(sizeof(LNode));        //分配一個頭結點
    if (L == NULL)        //內存不足,分配失敗
        return false;
    L->next = NULL;  //頭結點以後暫時尚未結點
    return true;
}

void test() {
    LinkList L;
    InitList(L);
}
相關文章
相關標籤/搜索