LOCATE operation (Yan 2.38)

Description

There is a doubly circular linked list. In addition to the three domains of pre, data and next, each node also adds a frequency domain freq. Before the linked list is activated, the value of the frequency field freq is initialized to zero, and each time a LOCATE(L, x) operation is performed on the linked list, the visited nodes (that is, the nodes whose element value is equal to x) are in the The value of the frequency field freq is increased by 1, and the order between the nodes in the linked list is adjusted so that they are arranged in the order of non-increasing access frequency, so that the frequently visited nodes are always close to the header node. . Try writing a program for the LOCATE operation that meets the above requirements.

Input

The first line inputs the number of nodes m of the doubly circular linked list and the number of visited nodes n, and
the second line inputs the value of each node of the doubly circular linked list,

The third line enters the visited nodes in turn.

Output

Output a doubly linked list that meets the above requirements.

Output the linked list after n times of 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 a b c e f gd->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;  
}  
intmain()  
{  
    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;  
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324440595&siteId=291194637