- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 Java 中的节点实现了双向链表。我的 Node 和链表类的代码如下。对于改进操作的设计和/或运行时间有什么建议吗?
public class Node implements Comparable<Node>{
Node next;
Node previous;
Object element;
int index;
public Node(Object element){
this.element = element;
this.next = null;
this.previous = null;
}
public Node(Object element, Node next, Node previous){
this.element = element;
this.next = next;
this.previous = previous;
}
public Node(Object element, Node next, Node previous, int index){
this.element = element;
this.next = next;
this.previous = previous;
this.index = index;
}
//Getter Method
public Node get_next(){
return this.next;
}
//For comparable class
public int compareTo(Node x){
if (this.element.equals(x.element)){
return 0;
}
else
return 1;
}
public boolean compare_index(int index){
if (this.index == index){
return true;
}
else
return false;
}
}
public class Mylinkedlists{
Node first_node;
Node last_node;
int size = 0;
//Constructors
public Mylinkedlists(Object element){
Node temp = new Node(element, last_node,first_node,0);
first_node = new Node (null,temp,null);
last_node = new Node(null,null,temp);
size+=1;
}
public Mylinkedlists(){
first_node = new Node (null,last_node,null);
last_node = new Node(null,null,first_node);
}
//Returns size of Node
public int size(){
return size;
}
//Adds element to end of linked list
public void add(Object element){
Node to_add = new Node(element);
Node temp = last_node.previous;
size+=1;
//Pointer changes
last_node.previous.next = to_add;
last_node.previous = to_add;
to_add.previous = temp;
to_add.next = last_node;
to_add.index = size-1;
}
//Inserts Element after node
public void insert(Object add_after, Object element_to_add) throws NotFoundException{
Node current = first_node.next;
Node insert_after = new Node(add_after);
Node to_add = new Node(element_to_add);
//find the node
while (current.compareTo(insert_after) != 0){
current = current.next;
}
//Inserts element after node
if (current.compareTo(insert_after) == 0){
Node temp = current.next;
current.next.previous = to_add;
to_add.next = temp;
current.next = to_add;
to_add.previous = current;
size+=1;
}
else
throw new NotFoundException("Element not in list");
}
//Removes element from linked list
public void remove(Object element) throws NotFoundException{
boolean stop = false;
Node search_for = new Node(element);
Node current = first_node.next;
for (int i = 0; i < size && !stop; i ++){
//Compares nodes
if (current.compareTo(search_for) == 1){
current.next.previous = current.previous;
current.previous.next = current.next;
current.next = null;
current.previous = null;
size-=1;
stop = true;
}
else
current = current.next;
}
//If element not in list
if (stop == false && current.next == null)
throw new NotFoundException("Element not in list");
}
//Removes element from end of linked list
public void remove_From_End(){
last_node.previous.next = null;
Node temp = last_node.previous.previous;
last_node.previous.previous = null;
last_node.previous = temp;
last_node.previous.next = last_node;
size -= 1;
}
//Returns true if list contains element, else false
public boolean contains(Object element){
boolean contains = false;
Node current = first_node.next;
Node with_element = new Node(element);
while (current.next != null){
if (current.compareTo(with_element) == 0)
return true;
else
current = current.next;
}
return contains;
}
public Object get(int index) throws NotFoundException{
if (index < 0 || index > (size-1))
throw new NotFoundException("Index out of range");
Node current = first_node;
for (int i = 0; i <= index; i++){
current = current.next;
}
return current.element;
}
public String toString(){
String temp = "";
Node temp2 = first_node;
for (int i = 0 ; i < size; i++){
temp2 = temp2.next;
temp += String.valueOf(temp2.element) + " ";
}
return temp;
}
}
例如,我使用点(.) 表示法直接访问 Node 类的元素。 getter 和 setter 方法是唯一的选择吗?此外,是否还有其他 get 方法的实现时间少于 O(n) 时间?
最佳答案
访问链表的时间不可能少于O(n)
。您将需要遍历数组才能访问各个元素。有更快的方法来访问各个元素,但这需要更改您的数据结构。我能想到的最简单的数据结构是数组。但是你的插入
和删除
将是O(n)
。
因此,在选择数据结构时,问题就变成了您将更多地进行插入
和删除
,还是获取
?
关于java - java中双向链表实现的效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26789009/
我有 2 个类:User 和 UserPicture,它们具有 1:1 关系。 public class User { @Id @GeneratedValue(strategy=G
使用ssh转发时,我无法针对远程服务器使用cvs和ftp进行提交。是否可以让服务器对我的机器发起请求-我希望服务器上的Web应用程序调用我的机器上的REST方法。 谢谢。 尼古拉·G。 最佳答案 是的
我正在 Python 2.7.12 中实现双向 A* 算法,并在 Russell 和 Norvig 第 3 章的罗马尼亚 map 上进行测试。边具有权重,目的是找到两个节点之间的最短路径。 这是测试图
您能否建议一种映射或类似的数据结构,让我们可以轻松地相互获取值和键。也就是说,每个都可以用来寻找另一个。 最佳答案 Java 在其标准库中没有双向映射。 例如使用 BiMap 来自Google Gua
我想同步两个数据库运行时 服务器 A:安装了公共(public) IP 和 mysql 的 Amazon ec2。服务器B:这是局域网中带有mysql的私有(private)机器。 (IP是私有(pr
保存双向@OneToOne 映射时,hibernate 是否应该在两个表上都记录? 我有一个包含 applicant_id 列的表 interview,它引用了包含字段 interview_id 的
我喜欢新的 SwipeRefreshLayout!它看起来很棒,而且非常容易使用。但我想在两个方向上使用它。我有一个消息屏幕,我想通过从上到下滑动来加载旧消息,我想通过从下到上滑动来加载新消息。 这个
使用 ICS 4.0.1(愿意升级到 4.0.3)(不会 root 和重写 android 操作系统) 在接收到 android beam 后,是否可以将 NDEF 消息发送回 android 手机
我想知道处理这种 git 场景的最佳方法: Git 仓库:CoreProduct Git repo b: SpecificCustomerProduct 是从 a fork 出来的 到目前为止,我们一
这个问题在这里已经有了答案: How to implement an efficient bidirectional hash table? (8 个回答) 关闭2年前。 我在 python 中做这个
您能否推荐一种 map 或类似的数据结构,我们可以在其中轻松地从彼此获取值和键。也就是说,每个都可以用来寻找另一个。 最佳答案 Java 在其标准库中没有双向映射。 例如使用 BiMap 来自 Goo
Java中是否有类似双面列表的东西?也许第三方实现? 这里有一个小例子来证明我的想法。 原始状态: 答:0-1-2-3 | | | | 乙:0-1-2-3 删除 B 中的元素 1 后: 空值 | 答:
我有两个实体通过这样的双向 OneToOne 关联连接: @Entity class Parent { @NotNull String businessKey; @OneToO
我已将 Vagrant 配置为使用 Rsync 共享文件夹而不是(非常慢)vboxsf VirtualBox 默认提供的文件系统: Vagrant.configure("2") do |config|
@keyframes mgm { from { max-height: 250px; } to { max-height: 0px; } } .mgm {
我想了解有关使用双向 LSTM 进行序列分类时合并模式的更多详细信息,尤其是对于我还不清楚的“Concat”合并模式。 根据我对这个方案的理解: 在将前向和后向层的合并结果传递到 sigmoid 函数
我有兴趣将本地 git 存储库设置为远程存储库的镜像。我已经阅读了一些可能相关的帖子,但主要区别在于我需要对两个存储库进行读写访问。 大多数时候,用户会针对 Repo A 工作,但是有时他们会针对 R
我已经仔细阅读了文档 https://firebase.google.com/docs/database/web/read-and-write以及网上很多例子。但这里有一个脱节:在将对象添加到数据库时
这个问题已经有答案了: Hibernate bidirectional @ManyToOne, updating the not owning side not working (3 个回答) 已关闭
我知道有很多关于它的问题,但我找不到针对我的问题的好的答案。 我使用 Jboss 作为 7,Spring 和 Hibernate (4) 作为 JPA 2.0 提供程序,因此我有简单的 @OneToM
我是一名优秀的程序员,十分优秀!