zoukankan      html  css  js  c++  java
  • 双向链表

    typedef struct DListElement_
    {
    int data;
    struct DListElement_ *prev;
    struct DListElement_ *next;

    }DListElement;

    typedef struct DList_
    {
    int size;
    DListElement *head;
    DListElement *tail;

    }DList;

    int dlist_ins_prev(DList* list, DListElement *element, const int data)
    {
    DListElement * new_element = (DListElement*)malloc(sizeof(DListElement));
    if (NULL == list || NULL == new_element || NULL == element)
    {
    return -1;
    }

    new_element->data = data;
    if (list->size == 0)
    {
    list->head = new_element;
    list->tail = new_element;
    new_element->next = NULL;
    new_element->prev = NULL;
    }
    else
    {
    if (NULL == element->prev)
    {
    new_element->prev = NULL;
    new_element->next = element;
    element->prev = new_element;
    }
    else
    {
    new_element->prev = element->prev;
    new_element->next = element;
    element->prev->next = new_element;
    element->prev = new_element;
    }

    }

    list->size++;

    return 0;
    }


    int dlist_ins_next(DList* list, DListElement *element, const int data)
    {
    DListElement * new_element = (DListElement*)malloc(sizeof(DListElement));
    if (NULL == list || NULL == new_element || NULL == element)
    {
    return -1;
    }

    new_element->data = data;
    if (list->size == 0)
    {
    list->tail = new_element;
    list->head = new_element;
    new_element->next = NULL;
    new_element->prev = NULL;
    }
    else
    {
    if (NULL == element->next)
    {
    list->tail = new_element;
    element->next = new_element;
    new_element->prev = element;
    new_element->next = NULL;
    }
    else
    {
    new_element->next = element->next;
    new_element->prev = element;
    element->next->prev = new_element;
    element->next = new_element;
    }

    }

    list->size++;
    return 0;
    }


    int deleElement(DList* list, DListElement* element)
    {
    if (NULL == list || NULL == element)
    {
    return -1;
    }

    if (NULL == element->prev)
    {
    list->head = element->next;
    list->head->prev = NULL;
    }

    if (NULL == element->next)
    {
    list->tail = element->prev;
    element->prev->next = NULL;
    }
    else
    {
    element->next->prev = element->prev;
    element->prev->next = element->next;
    }

    if (NULL != element)
    {
    free(element);
    }

    list->size--;
    return 0;
    }

  • 相关阅读:
    软件工程概论总结第三章
    软件工程概论总结第二章
    软件工程概论总结
    软件工程概论10-软件测试
    软件工程概论9-软件实现
    软件工程概论-8面向对象设计
    软件工程概论-7面向对象分析
    软件工程概论-6面向对象基础
    软件工程概论-5软件工程中的形式化方法
    软件工程概论-4需求过程
  • 原文地址:https://www.cnblogs.com/weiyouqing/p/12718060.html
Copyright © 2011-2022 走看看