你会如何实现,你用了多长时间?

aaassd 2005-06-07 10:57:58
Problem statement:
Design and implement a CIRCULAR buffer for integers. A circular buffer
contains a FIXED number of slots for storing integers and acts like a
FIFO
queue. New entrants to the queue are added at the END of the buffer.
Delete
removes from the HEAD of the queue. Implement operations to add,
delete,
compare (comparing two such queue objects), and find. Make the code
robust
and consider edge cases. Assume that CONCURRENT access to the buffer is
possible.

Please implement it in Java.
...全文
126 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Preamble 2005-06-14
  • 打赏
  • 举报
回复
private int first;
private int end;
private int size;
public void addInteger(Integer e)
{
size ++;
if(size>FIXED_SLOT)
{
throw new OutOfCircularQueueSizeException();
}
end = (end+1)%FIXED_SLOT;
array[end] = e;
}
public Integer delete()
{
size --;
if(size<0) throw new IllegalOperationException();
Integer iReturn = array[(first)%FIXED_SLOT];
first = (first+1)%FIXED_SLOT;
return iReturn;
}

写了一片段,如果要同步的华,加上synchronized
也没有测试
aaassd 2005-06-14
  • 打赏
  • 举报
回复
不错,还有吗?
treeroot 2005-06-07
  • 打赏
  • 举报
回复
import java.util.*;
class FIFOCache
{
private Map map;
public static int DEFAULT_SIZE=100;
public FIFOCache(){
this(DEFAULT_SIZE);
}
public FIFOCache(final int cacheSize){
map=new LinkedHashMap(){
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > cacheSize;
}
};
}

public void queue(Object o){
map.put(o,null);
}
public void remove(){
if(map.isEmpty()) return;
map.remove(map.keySet().iterator().next());
}
public boolean find(Object o){
return map.containsKey(o);
}
public boolean equals(Object o){
if(o instanceof FIFOCache){
return map.equals(((FIFOCache)o).map);
}
return false;
}

public int hashCode(){
return map.hashCode();
}

}
nwpulipeng 2005-06-07
  • 打赏
  • 举报
回复
I can't understand so complicated English
aaassd 2005-06-07
  • 打赏
  • 举报
回复
我也知道有。
treeroot 2005-06-07
  • 打赏
  • 举报
回复
不用实现,有现成的,LinkedHashMap

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧