#include
using namespace std;
class LRUStack
{
public:
LRUStack(int len)
{
p=new int[len];
length=len;
//初始化栈,都为-1
for(int i=0;i
p[i]=-1;
}
~LRUStack()
{
delete[]p;
}
int push(int a)
{
int pos=search(a);
int index=lastpos();
if(pos!=-1)
{
//已经存在
for(int i=pos;i
{
p[i]=p[i+1];
}
p[index]=a;
return -1;
}
else
{
//不存在
if(isfull())
{
//已经满了
int last=p[0];
for(int i=pos;i
{
p[i]=p[i+1];
}
p[index]=a;
return last;
}
else
{
p[lastpos()+1]=a;
return -1;
}
}
}
private:
int *p;
int length;
int search(int a)
{
for(int i=0;i
{
if(p[i]==a)
return i;
}
//没有找到
return -1;
}
bool isfull()
{
if(p[length-1]==-1)return false;
return true;
}
int lastpos()
{
for(int i=0;i
{
if(p[i]==-1)
return i-1;
}
return length-1;
}
};
int main()
{
int length;
cout
cin>>length;
if(length
{
cout
return 0;
}
LRUStack LruStack(length);
int num;
int pushnum;
while(true)
{
cout
cin>>num;
pushnum=LruStack.push(num);
if(pushnum==-1)
cout
else
cout
}
return 0;
}
#include
using namespace std;
class LRUStack
{
public:
LRUStack(int len)
{
p=new int[len];
length=len;
//初始化栈,都为-1
for(int i=0;i
p[i]=-1;
}
~LRUStack()
{
delete[]p;
}
int push(int a)
{
int pos=search(a);
int index=lastpos();
if(pos!=-1)
{
//已经存在
for(int i=pos;i
{
p[i]=p[i+1];
}
p[index]=a;
return -1;
}
else
{
//不存在
if(isfull())
{
//已经满了
int last=p[0];
for(int i=pos;i
{
p[i]=p[i+1];
}
p[index]=a;
return last;
}
else
{
p[lastpos()+1]=a;
return -1;
}
}
}
private:
int *p;
int length;
int search(int a)
{
for(int i=0;i
{
if(p[i]==a)
return i;
}
//没有找到
return -1;
}
bool isfull()
{
if(p[length-1]==-1)return false;
return true;
}
int lastpos()
{
for(int i=0;i
{
if(p[i]==-1)
return i-1;
}
return length-1;
}
};
int main()
{
int length;
cout
cin>>length;
if(length
{
cout
return 0;
}
LRUStack LruStack(length);
int num;
int pushnum;
while(true)
{
cout
cin>>num;
pushnum=LruStack.push(num);
if(pushnum==-1)
cout
else
cout
}
return 0;
}