问题描述:一双向循环链表,每个结点除了prior,datanext三个域外,还增设了一个访问频度域freq。链表启用前,freq0,每对链表进行一次LOCATE(Lx)的操作后,该结点的freq1,同时调整链表中结点之间的次序,使得被频繁访问的结点总是靠近表头结点

。编写符合上述要求的LOCATE算法。

问题分析:

重新把问题打在上面,更好地理解了一遍,也提高了自己的概括能力,挺好!

 

看吧,当自己写时,总感觉有那么些别扭,可是看着别人的就豁然开朗,自己还没想到豁然开朗,思维的锻炼还不够。。。

好吧,既然你已经看懂了答案了,很清晰的逻辑。但是我不想让自己现在就根据刚看的,然后写出来,看看明天还记不记得,今天暂且到这里吧。【2013-04-22

 实现代码:(暂存)

 

  1 #include<stdio.h>
  2  #include<stdlib.h>
  3  typedef  struct DuLNode{
  4   int  data;
  5   int freq;
  6   struct  DuLNode  *prior;//前驱结点
  7   struct  DuLNode  *next;//后继结点
  8  }DuLNode,*DuLinkList;
  9  void add(DuLinkList &L,int value)
 10  {
 11      if(L==NULL)
 12      {
 13         L=(DuLinkList)malloc(sizeof(DuLNode));
 14         L->data=value;
 15         L->next=L;
 16         L->prior=L;
 17         L->freq=0;
 18      }
 19      else
 20      {
 21         DuLinkList temp=(DuLinkList)malloc(sizeof(DuLNode));
 22         temp->data=value;
 23         temp->freq=0;
 24         L->prior->next=temp;
 25         temp->prior=L->prior;
 26         temp->next=L;
 27         L->prior=temp;//这下应该明白为什么是在这里插入。      
 28      }
 29  }
 30  void print(DuLinkList L)
 31  {
 32      if(L==NULL)
 33          printf("链表为空!");
 34      else
 35          if(L->prior==L)//只有一个结点的情况
 36              printf("%d",L->data);
 37          else//循环链表中多个结点的情况
 38          {
 39              DuLinkList cur=L;
 40              printf("%d",cur->data);
 41              while((cur=cur->next)!=L)
 42                  printf("%d",cur->data);
 43          }
 44  }
 45  void ListLocate_Dul(DuLinkList  &L, int e)
 46  {
 47       DuLinkList p,q;
 48       p=L;
 49       while(p->data!=e)
 50       {
 51         p=p->next;
 52         if((p==L)&&(p->data!=e)) //有待改进
 53         {
 54            printf("没有找到你要找的数:%d\n",e);
 55            return;
 56           //exit(0);//这个用的挺危险的。。。会结束程序,而不是此函数的返回值
 57         }
 58       }
 59       p->freq++;//p指向找到的数据
 60       p->prior->next=p->next;//将此结点抽离出来
 61       p->next->prior=p->prior;
 62       //插入到合适的位置
 63       q =L;
 64       while(true) 
 65       {
 66           if(p->freq>=q->freq)
 67                 break;
 68           else
 69              q=q->next; 
 70       }
 71        // if(q==L)
 72        //{
 73        //     p->next=q->next;
 74        //       q->next=p;
 75        //       p->prior=q->prior;
 76        //        q->prior=p;  
 77        // }
 78        // else
 79        //{
 80               q->prior->next=p;
 81               p->prior=q->prior;
 82               p->next=q;
 83               q->prior=p;//这还是插入之前呢。
 84        //}
 85  }
 86  int main()
 87  {
 88     DuLinkList L=NULL,pt;
 89     int temp;
 90     printf("创建循环链表:\n");
 91     while(scanf("%d",&temp)&&(temp!=-1))
 92     {
 93       add(L,temp);
 94     }
 95     printf("输入你要访问的数:\n");
 96     while(true){
 97     scanf("%d",&temp);
 98     if(temp==-1)
 99         break;
100     else
101        ListLocate_Dul(L,temp);
102     }
103     printf("访问后,链表数据调整为:\n");
104     pt=L;
105     int i=0;
106    // while(i<3)
107     //{
108        printf("数据元素为:%d,频率为:%d\n",pt->data,pt->freq);
109        pt=pt->next;
110     
111     //}
112  }
View Code

相关文章:

  • 2021-07-06
  • 2021-09-02
  • 2021-12-05
  • 2021-08-25
  • 2021-10-26
  • 2021-09-09
  • 2022-12-23
猜你喜欢
  • 2021-10-21
  • 2022-03-04
  • 2021-07-30
相关资源
相似解决方案