用Java语言:
使用3个队列,分别保留手机上最近10个,未接来电、已接来电、已拨电话
public class Main {
public static void main(String[] args) {
Main main = new Main();
Op op = new Op();
Scanner sc = new Scanner(System.in);
int type;
while((type=sc.nextInt())!=Op.OP_FINISH){
String mobile = "";
if (type < 4) {
mobile = sc.next();
}
op.operator(type,mobile);
}
}
static class Op{
Memory tel;
Memory notAccept;
Memory accept ;
public static final int OP_TEL = 1;
public static final int OP_REFUSE = 2;
public static final int OP_ACCEPT = 3;
public static final int OP_TEL_PRINT = 4;
public static final int OP_REFUSE_PRINT = 5;
public static final int OP_ACCEPT_PRINT = 6;
public static final int OP_FINISH = 7;
public Op(){
tel = new Memory();
notAccept = new Memory();
accept = new Memory();
}
public void operator(int type,String mobile){
switch (type){
case OP_TEL:
addTel(mobile);
break;
case OP_REFUSE:
addNotAccept(mobile);
break;
case OP_ACCEPT:
addAccept(mobile);
break;
case OP_TEL_PRINT:
System.out.println(getTelList());
break;
case OP_REFUSE_PRINT:
System.out.println(getNotAcceptList());
break;
case OP_ACCEPT_PRINT:
System.out.println(getAcceptList());
break;
}
}
public void addTel(String mobile){
tel.add(mobile);
}
public Queue<String> getTelList(){
return tel.getList();
}
public void addNotAccept(String mobile){
notAccept.add(mobile);
}
public Queue<String> getNotAcceptList(){
return notAccept.getList();
}
public void addAccept(String mobile){
accept.add(mobile);
}
public Queue<String> getAcceptList(){
return accept.getList();
}
}
static class Memory{
Queue<String> list = null;//拨出
public Memory(){
list = new LinkedList<>();
}
public void add(String mobile){
list.offer(mobile);
if(list.size()>10)
list.poll();
}
public Queue<String> getList(){
return list;
}
}
}
public static void main(String[] args) {
//add()和remove()方法在失败的时候会抛出异常(不推荐)
Queue<String> wjld = new LinkedList<String>();
//添加元素
wjld.offer("a");
wjld.offer("b");
wjld.offer("c");
wjld.offer("d");
wjld.offer("e");
wjld.offer("a");
wjld.offer("b");
wjld.offer("c");
wjld.offer("d");
wjld.offer("e");
Queue<String> yjld = new LinkedList<String>();
//添加元素
yjld.offer("a");
yjld.offer("b");
yjld.offer("c");
yjld.offer("d");
yjld.offer("e");
yjld.offer("a");
yjld.offer("b");
yjld.offer("c");
yjld.offer("d");
yjld.offer("e");
Queue<String> ybdh = new LinkedList<String>();
//添加元素
ybdh.offer("a");
ybdh.offer("b");
ybdh.offer("c");
ybdh.offer("d");
ybdh.offer("e");
ybdh.offer("a");
ybdh.offer("b");
ybdh.offer("c");
ybdh.offer("d");
ybdh.offer("e");
}