LOCATE操作(严2.38)

Description

设有一个双向循环链表,每个结点中除有pre,data和next三个域外,还增设了一个访问频度域freq。在链表被起用之前,频度域freq的值均初始化为零,而每当对链表进行一次LOCATE(L,x)的操作后,被访问的结点(即元素值等于x的结点)中的频度域freq的值便增1,同时调整链表中结点之间的次序,使其按访问频度非递增的次序排列,以便始终保持被频繁访问的结点总是靠近表头结点。试编写符合上述要求的LOCATE操作的程序。

Input

第一行输入双向循环链表的节点数m和被访问的节点数n,
第二行输入双向循环链表各节点的值,

第三行依次输入被访问的节点。

Output

输出符合上述要求的双向循环链表。

输出经过n次LOCATE后的链表。

Sample Input

7 1
a b c d e f g
d

Sample Output

d a b c e f g

Code:

#include <stdio.h>  
#include <stdlib.h>  
#include <cassert>  
#define TRUE 1  
#define OK 1  
#define FALSE 0  
#define ERROR 0  
#define OVERFLOW -1  
  
typedef struct node {  
    char data;  
    struct node * next;  
    struct node * prior;  
    int freq;  
}DuNode,* DuList;  
  
int Print(DuList head)  
{  
    DuNode * p=head->next;  
    while (p != NULL)  
    {  
        printf("%c ", p->data);  
        p = p->next;  
    }  
    printf("\n");  
    return OK;  
}  
DuList Sort(DuList head,int m)  
{  
    int i,j;  
    DuNode *p=head->next;  
    DuNode *q;  
    for(i=1;i<m;i++)  
    {  
        p=head->next;  
        for(j=1;j<=m-i && p->next;j++)  
        {  
            if(p->freq < p->next->freq)  
            {  
                if(p->next->next==0)  
                {  
                    q=p->next;  
                    p->prior->next=q;  
                    q->prior=p->prior;  
                    p->prior=q;  
                    p->next=NULL;  
                    q->next=p;  
                    continue;  
                }  
                q=p->next;  
                p->prior->next=q;  
                q->prior=p->prior;  
                p->prior=q;  
                p->next=q->next;  
                q->next->prior=p;  
                q->next=p;  
            }  
            p=p->next;  
        }  
    }  
}  
DuList Locate(DuList head,char e)  
{  
    DuNode *p=head->next;  
    while(p!=NULL)  
    {  
        if(p->data==e)  
        {  
            return p;  
        }  
        p=p->next;  
    }  
    return NULL;  
}  
DuList Insert(DuList head,char n)  
{  
    int i;  
    DuNode *s= (DuNode *)malloc(sizeof(DuNode));  
    if(head==NULL)  
    {  
        head=(DuList)malloc(sizeof(DuNode));  
        assert(head!=NULL);  
        head->data=0;  
        head->next=NULL;  
        head->prior=NULL;  
        head->freq=0;  
    }  
    DuNode *newnode=(DuList)malloc(sizeof(DuNode));  
    assert(newnode!=NULL);  
    newnode->data=n;  
    newnode->next=NULL;  
    newnode->prior=NULL;  
    newnode->freq=0;  
  
    DuNode *p=head;  
    while(p->next!=NULL)  
    {  
        p=p->next;  
    }  
    p->next=newnode;  
    newnode->prior=p;  
    return head;  
}  
int main()  
{  
    DuNode *q,*head=NULL;  
    char e,p;  
    int m,n,i;  
    scanf("%d %d",&m,&n);  
    getchar();  
    for(i=0;i<m;i++)  
    {  
        scanf("%c",&p);  
        getchar();  
        head=Insert(head,p);  
    }  
    for(i=n;i>0;i--)  
    {  
        scanf("%c",&e);  
        getchar();  
        q=Locate(head,e);  
        q->freq++;  
    }  
    Sort(head,m);  
    Print(head);  
    return 0;  
} 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值