非循环双向链表增加访问频度域freq,每进行一次Locate(L,x)运算,freq加一。使链表中结点保持按访问频度非增的顺序排列,同时最近访问的结点排在频度相同的结点前面

typedef struct dnode20
{
    int data, fre;
    dnode20 *lnext, *rnext;
}Dnode20;
void printDnode20(Dnode20 *h)
{
    Dnode20 *p = h->rnext;
    while (p)
    {
        printf("%d  fre:%d    ", p->data, p->fre);
        p = p->rnext;
    }
}
Dnode20 *createDnode20link()//创建20题目要求的链表
{
    type x;
    Dnode20 *h, *t;
    t = h = (Dnode20 *)malloc(sizeof(Dnode20));
    h->rnext=h->lnext = NULL;

    scanf_s("%d", &x);
    while (x != -1)
    {
        Dnode20 *s = (Dnode20 *)malloc(sizeof(Dnode20));
        s->data = x;
        s->fre = 0;
        s->rnext = NULL;
        t->rnext = s;
        s->lnext = t;
        t = s;
        scanf_s("%d", &x);
    }
    t->rnext = NULL;
    printDnode20(h);
    return h;
}
Dnode20 *Locate20(Dnode20 *h,type x)
{
    Dnode20 *p=h;
    Dnode20 *q,*m;
    while (p->rnext&&p->rnext->data != x)
        p = p->rnext;
    if (!p->rnext)
        return NULL;
    ++(p->rnext->fre);
    q = p;
    m = p->rnext;
    p->rnext = m->rnext;
    if (m->rnext!=NULL)
    m->rnext->lnext = p;
    while (q!= h&&q->fre <= m->fre)
        q = q->lnext;
    q->rnext->lnext = m;
    m->rnext = q->rnext;
    m->lnext = q;
    q->rnext = m;
        /*q->rnext->lnext = m;
        p->rnext = m->rnext;
        m->lnext = q;
        if (m->rnext)
            m->rnext->lnext = p;
        m->rnext = q->rnext;
        q->rnext = m;*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值