雙向鏈表的操作

一、雙向鏈表的創建
雙向鏈表的結點結構體有兩個指針,一個指向前向結點,一個指向後繼節點,創建步驟和單向鏈表差不多

typedef struct DNode
{
    int data;
    struct DNode *front;
    struct DNode *next;
}

DNode *create_DNode_list(int n)
{
    int a, i;
    DNode *head, *pioneer, *previous;
    head = (DNode *)malloc(sizeof(DNode));
    head->data = 0;
    head->next = NULL;
    head->front = NULL;
    pioneer = head;
    previous = head;

    for(i = 0; i < n; i++){
        scanf("%d", &a);
        // 1:malloc space for the new Node and let the pioneer point to it
        pioneer = (Node *)malloc(sizeof(Node));
        // 2:initiate the new Node
        pioneer->data = a;
        pioneer->next = NULL;
        pioneer->front = previous;
        // 3:use the previous to link the previous to the new Node
        previous->next = pioneer;
        // 4:update the previous
        previous = pioneer;
    }
    return head;

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章