双向链表Locate( L , x )运算

#include <stdio.h>
#include <stdlib.h>

typedef struct DNode{
    int data ;
    int freq;
    struct DNode *next, *prior ;
}DNode , *DinkList;

DinkList Createlist( DinkList L){
    int num ;
    DNode *p , *r = L ;
    printf("请输入数据:\n");
    scanf("%d",&num) ;
    while( num!=-1 ){
        p = (DNode *)malloc(sizeof(DNode));
        p->data = num ;
        p->freq =0;
        r->next = p ;
        p ->prior = r ;
        r = p ;
        scanf("%d",&num) ;
    }
    r->next = L ;                     //将尾指针指向头结点,头指针指向尾结点
    L->prior = r;
    return L ;
}

void ViewLinklist( DinkList L ){
    DNode *p = (DNode *)malloc(sizeof(DNode));
    p = L->next ;
    printf("循环双链表为:");
    while( p->next!=L ){
        printf("%d ->",p->data);
        p = p->next ;
    }
    printf("%d",p->data);
}

DinkList Locate( DinkList L , int x){
    DNode *p = L->next ,*q ;
    while(p!=NULL&&p->data!=x)
        p=p->next;
    if(!p) printf("该节点不存在");
    else{
        p->freq++;
        p->next->prior = p->prior ;       //将节点p摘下
        p->prior->next = p->next ;
        q=p->prior ;
        while(q!=L&&q->freq<=p->freq)     //插入到同频度的第一个
            q = q->prior ;
        p->next = q->next ;
        q->next->prior = p ;
        p->prior = q ;
        q->next = p ;
    }
    return p;
}

int main()
{
    int data ;
    DinkList L = (DinkList )malloc(sizeof(DNode));
    Createlist( L ) ;
    ViewLinklist(L );
    printf("\n");
    printf("请输入查找数据:");
    scanf("%d",&data);
    Locate(L,data);
    ViewLinklist(L );
}

猜你喜欢

转载自blog.csdn.net/RRWJ__/article/details/82941537