我有两个命令"添加"和"撤消"。使用"添加"操作,多个,包括一对,可以发送到程序。另一方面,用"撤消"操作,最后发送的数据应该给出。为了实现这个功能,我想;
我应该拥有这些数据结构元素;
| | HashMap<String, String> : It is used for storing data
| |
| |
| |
--------
Stack : It seems best to "undo" and "add" operations.
在某些时候,我应该在实践中低于堆叠;
| |
| |
| ________________________________ |
| | (filename5, date1) | | third HashMap type container
| | (filename6, date5) | | holding three items
| | (filename7, date9) | |
| |______________________________| |
| |
| ________________________________ | second HashMap type container
| | (filename3, date2) | | holding one item
| |______________________________| |
| |
| ________________________________ | first HashMap type container
| | (filename1, date1) | | holding two items
| | (filename2, date2) | |
| |______________________________| |
|----------------------------------|
我的问题是&#34;如何确保整个堆栈中所有容器中的第一个键唯一?&#34;
如果不能使用上述数据结构,我应该使用什么作为数据结构来实现上述愿望呢?
答案 0 :(得分:0)
为什么不保留两个结构:Stack<String>
并检查它是否包含filename_x
,第二个结构 - 您的哈希地图HashMap<String, String>
?
UPD:换句话说,您不需要将堆栈中的theese映射作为其元素。
答案 1 :(得分:0)
它通过三个Java Stack
和自己的堆栈实现来解决,以使push
和pop
操作同步。实施是;
class OwnStack{
private final Stack<String> file = new Stack<String>()
private final Stack<String> date = new Stack<String>()
private final Stack<String> numberOfElementsPushedAtTheSamTime
= new Stack<String>()
public void push(String []files, String []date{
/* Algorithm
* Iterate over files
* if file is in stack
* donot push anything to stack
* else
* push file to stack
* push category to stack
* push number of entries pushed at the same time to stack (files.length())
*/
}
public void pop(){
/*
* Algorithm
* If size is zero in file stack
* do nothing
* else
* get top number from numberOfElementsPushedAtTheSamTime stack
* iterate over stacks returned number times
* pop files stack
* pop date stack
*/
// I donot need return values
}
}