双向循环链表

双向循环链表

 一 定义: 在双向循环链表中头节点的next指向第一个,头节点的prior指向最后一个,形成一个闭环

typedef struct List
{
    ElemType data;
    struct List *next;
    struct List *prior;
}Dulode,*DuLinkList;

 二 初始化链表

void InitList(DuLinkList &L)
{
    L = (DuLinkList)malloc(sizeof(Dulode));
    // use the L to point to the store place we malloc
    L->next = L;
    L->prior = L;
}

 三 从头部插入元素

void push(DuLinkList L,ElemType e)
{
    DuLinkList p = L;
    DuLinkList q = (DuLinkList)malloc(sizeof(Dulode));
    q->data = e;
    q->next = L->next;
    q->prior = p;
    L->next->prior = q;
    L->next = q;
    //insert the elem to the head
}

 四 从尾部插入元素

void push_back(DuLinkList L,ElemType e)
{
    DuLinkList p = L;
    DuLinkList q =(DuLinkList)malloc(sizeof(Dulode));
    q->data = e;
    q->next = p;
    q->prior = L->prior;
    L->prior->next = q;
    L->prior = q;
}
//insert to the tail of the LinkList

  五 删除元素

1根据指针删除

void Delete(DuLinkList q)
{
    q->next->prior = q->prior;
    q->prior->next = q->next;
    free(q);
}
// free the space that pointed by the q, and Link its next and prior

2 根据值删除

status Delete_by_data(DuLinkList L,ElemType data)
{
    DuLinkList  p = L;
    DuLinkList  q = L;
    while (q->next!=p)
    // because we use the two pointers, so we can judge whether we have look the all elements
    {
        if(q->next->data == data)
        {
            free(q);
            break;
        }
        q = q->next;
    }
    if(q->next == p)
        return 0;
    else
        return 1;
}

3升序排序

void sort_up(DuLinkList L)
{
    DuLinkList p,q;
    DuLinkList NewList = (DuLinkList)malloc(sizeof(Dulode));
    // we use a NewList to store the right order of elements
    NewList->next = NewList;
    NewList->prior = NewList;
    //now we have initilize it
    p = L;
    q = L;
    while (p->next!=q)
    {
        // while we haven't traverse the list for a whole round,keep point to the next
        DuLinkList temp = (DuLinkList)malloc(sizeof(Dulode));
        temp->data = p->next->data;
        // malloc a new node with the same data
        if(NewList->next==NewList&&NewList->prior==NewList)
        {
            // judge whether it is empty
            NewList->next = temp;
            NewList->prior = temp;
            temp->next = NewList;
            temp->prior = NewList;
        }
        else
        {
            // now we divide it into three situations
            // first the data is small than the head node
            if(temp->data<NewList->next->data)
            {
                NewList->next->prior = temp;
                temp->next = NewList->next;
                temp->prior = NewList;
                NewList->next = temp;
            }
            // second the data is bigger than the tail node
            else if(temp->data>NewList->prior->data)
            {
                NewList->prior->next = temp;
                temp->prior = NewList->prior;
                temp->next = NewList;
                NewList->prior = temp;
            }
            // third the data is in the middle
            else
            {
                DuLinkList p1 = NewList;
                while (p1->next->data<temp->data)
                {
                    p1 = p1->next;
                }
                temp->next = p1->next;
                temp->prior = p1;
                p1->next =temp;
            }
        }
        p = p->next;
    }
    p = L->next;
    q = L;
    // now we free the storage place
    while (q!=p)
    {
        p = p->next;
        free(p->prior);
    }
    L->next = NewList->next;
    L->prior = NewList->prior;
    NewList->next->prior = L;
    NewList->prior->next = L;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值