C++ 雙鏈表的 創建、鏈接、刪除


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

//定義一個雙鏈表節點
 struct Node
{
    int date; //數據
    Node* next; //下個數據
    Node* last; //上個數據
};

//定義list存放node指針
 struct List
{
    int size; //存放node節點的大小
    Node* head; //指針指向頭節點
    Node* tail; //指針指向尾節點

};

//初始化雙鏈表節點
 Node* init_node(int x)
{
    Node* temp= (Node*)malloc(sizeof(Node));
    temp->date = x;
    temp->next = temp->last = NULL;
    return temp;
}

//初始化雙鏈表
List* init_list()
{
    List* temp = (List*)malloc(sizeof(List));
    temp->head = NULL;
    temp->tail = NULL;
    temp->size = 0;
    return temp;
}

//節點的鏈接傳參,第一個參數數據在前,第二個參數數據在后
void  link_node(Node* n1, Node* n2)
{
    n1->next = n2;
    n2->last = n1;
}


//雙鏈表鏈接
List* push_head(List* list, Node* n1)
{
    if (list->size == 0)
    {
        list->head = n1;    //雙鏈表第一次添加節點時,此時雙鏈表中只有一個元素,list的頭指針和尾指針都指向一個鏈表,
        list->tail = list->head;
        list->size=1;
    }
    else
    {
        link_node(n1, list->head); 
        list->head = n1;
        list->size++;
    }
    return list;
}

//從頭開始雙鏈表
void list_head_printf(List* list)
{
    if (list->head == NULL)
    {
        printf("雙鏈表為空!\n");
        return;
    }
    else
    {
        printf("雙鏈表輸出從頭部開始輸出:");

        for (Node *temp = list->head; temp != NULL; temp = temp->next)
        {
            printf("%d", temp->date);
            if (temp == list->tail)
                printf("\n");
            else
                printf(" ");
        }
    }
}


void push_end(List* data,Node*temp1 ) //雙鏈表尾插
{
    List* temp = data;
    if (temp->head == NULL)
    {
        temp->head = temp1;
        temp->tail = temp->head;
        temp->size = 1;
        return ;
    }
    else
    {
        link_node(temp->tail, temp1);
        temp1->last = temp->tail;  //將temp1的前驅掛在尾節點上,
        temp->tail = temp1;        //尾節點的值現在指向temp1
        temp->size++;
        return ;
    }

}


void del_node(List* list, int num)  //雙鏈表刪除某個位置的節點 傳參:1.雙鏈表節點,2,需要刪除的位置
{
    Node* tt = (Node*)malloc(sizeof(Node)); //動態申請一個結構體指針用於free對象
    if (list->head == NULL) //若雙鏈表為空則直接返回
    {
        printf("雙鏈表為空!\n");
        return;
    }
    if (list->size < num) //判斷查找的位置是否和空間大小范圍內
    {
        printf("雙鏈表不存在該位置!\n");
        return ;
    }
    if (list->size == 1) //判斷刪除雙鏈表節點時,雙鏈表只有1個節點
    {
        tt = list->head;
        free(tt);
        list->head = list->tail = NULL;
        list->size = 0;
        return;
    }
    if (list->size == 2) //判斷刪除雙鏈表節點時,雙鏈表只有2個節點
    {
        Node* temp = (Node*)malloc(sizeof(Node));
        temp = list->tail;
        free(temp);
        list->head->next = NULL;
        list->tail = list->head;
        list->size--;
        return;
    }
    if (list->size > 2) //判斷刪除雙鏈表節點時,雙鏈表只有2個以上個節點
    {
        int n = 1;    
        for (Node* temp = list->head; temp->next != NULL; temp = temp->next) //循環遍歷雙鏈表節點
        {
            if (n == num)  //找到當前需要刪除的節點
            {
                if (temp->next == NULL) //判斷要刪除的節點位置是否是在最后,
                {
                    tt = temp;
                    temp = temp->last;
                    list->tail = temp;
                    free(tt);
                    list->size--;
                    return ;
                }
                else   
                {
                    tt = temp;
                    link_node(temp->last, temp->next);
                    free(tt);
                    list->size--;
                    return;
                }                
            }
            n++;
        }
    }
}


int main()
{
    List* list = init_list();
    Node* temp1 = init_node(1);
    Node* temp2 = init_node(2);
    Node* temp3 = init_node(3);
    push_head(list, temp1);
    push_head(list, temp2);
    push_head(list, temp3);
    printf("當前雙鏈表中的內容\n");
    list_head_printf(list);
    printf("即將刪除位置2的雙鏈表\n");
    del_node(list, 2);
    list_head_printf(list);
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM