#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *prior;
struct node *next;
}T;
T *creat1(int n){
T *head,*p,*L;
int i;
head=(T *)malloc(sizeof(T));
head->prior=NULL;
head->next=NULL;
L=head;
for(i=0;i<n;i++)
{
p=(T *)malloc(sizeof(T));
p->data=rand()%11;
p->next=L->next;
if(L->next!=NULL){
L->next->prior=p;
}
L->next=p;
p->prior=L;
}
return head;
}
T *creat2(){
T *head;
head=(T *)malloc(sizeof(T));
head->prior=NULL;
head->next=NULL;
return head;
}
void print(T *head){
T *h=head;
while(h->next!=NULL){
h=h->next;
printf("%d\n",h->data);
}
}
void LRU_List(T *head1,T *head2,int j,int n){
int i,b=0,k=0,flag=0;
T *e,*L,*h;
for(i=1;i<=n;i++){
e=head1->next;
b=0;
printf("第%d次\n",i);
if(head2->next==NULL){
head1->next=e->next;
e->next->prior=head1;
e->next=head2->next;
if(head2->next!=NULL){
head2->next->prior=e;
}
head2->next=e;
e->prior=head2;
k=k+1;
}
else{
L=head2;
while(L->next!=NULL){
L=L->next;
b=b+1;
if(e->data==L->data){
flag=1;
break;
}
else{
flag=0;
}
}
if(flag!=1){
head1->next=e->next;
e->next->prior=head1;
e->next=head2->next;
head2->next->prior=e;
head2->next=e;
e->prior=head2;
k=k+1;
}
else{
head1->next=e->next;
e->next->prior=head1;
if(b!=1){
L->next->prior=L->prior;
L->prior->next=L->next;
L->next=head2->next;
head2->next->prior=L;
L->prior=head2;
head2->next=L;
}
}
}
if(k==j+1){
h=head2;
while(h->next!=NULL){
h=h->next;
}
h->prior->next=NULL;
k=j;
}
printf("K=%d\n",k);
print(head2);
}
printf("最终结果:");
print(head2);
}
void main(){
T *head1,*head2;
int n,j;
printf("请输入有多少个节点:");
scanf("%d",&n);
head1=creat1(n);
print(head1);
printf("\n物理块数量:");
scanf("%d",&j);
head2=creat2();
LRU_List(head1,head2,j,n);
}