Tag: 链表

线程安全的已排序链表

我正在尝试编写一个线程安全的已排序单链表 。 我写了两个版本:粗粒度同步和细粒度同步。 以下是两个实现: 细粒度: public void add(T t) { Node curr = head; curr.lock.lock(); while (curr.next != null) { // Invariant: curr is locked // Invariant: curr.data < t curr.next.lock.lock(); if (t.compareTo(curr.next.data) <= 0) { break; } Node tmp = curr.next; curr.lock.unlock(); curr = tmp; } // curr is acquired curr.next = new Node(curr.next, […]

什么是LinkedHashMap ?

好的,所以我是这些HashMaps的新手,但对LinkedLists和HashMaps有一些了解。 如果你能给我一些关于LinkedHashMap的简单解释并且在titile中这意味着我们明确地将它定义为某种类型会很棒吗?

在LinkedList中反转对象的问题

我正在为一个赋值编写代码,该赋值需要一个方法来反转LinkedList中的元素,给定列表的一部分要反转。 例如,如果用户输入3,则该方法将反转数组中的前3个元素。 我已经为它编写了代码,但它没有反转代码,而只是将第二个元素替换为第一个索引中存在的元素。 我唯一的问题似乎是reverseFirstSome方法。 我不是要求你为我编写代码,但任何正确方向的指针都会受到赞赏。 这是我的全部代码: import java.util.NoSuchElementException; public class LinkedList { //nested class to represent a node private class Node { public Object data; public Node next; } //only instance variable that points to the first node. private Node first; // Constructs an empty linked list. public LinkedList() { first = null; } // […]

把链表的头部移到尾部

我需要在Java中编写一个方法,将链表中的第一个元素移动到最后一个位置。 为了实现这一点,我相信我必须设置一个节点来引用头部后面的第一个元素,然后将下一个节点设置为null。 我尝试使用我的方法执行此操作,但在运行该方法时,输出不正确。 我所拥有的其他课程很可能无法在此发布,但我认为我只需要帮助概念化如何将第一个元素移动到列表的末尾。 我写的方法是: public void moveFirstToEnd() { if (head.next == null) { throw new NoSuchElementException(); } Node node = head.next; node.next = null; head.next = node; tail.next = node; tail = node; }

计算链接列表中的所有节点

我正在尝试编写一个简单的方法来计算链表中的所有节点。 我知道链表中有7个项目,但它只返回6个。 这是我的方法 public int count() { int count = 0; for (ListNode n = head; n.next != null; n = n.next) { count++; } return count; } 这是我的ListNode.java public class ListNode { String name; // a name in the list ListNode next; // the next node in the list ListNode prev; // the previous […]

理解节点和链表概念的问题

我是Data Structures和Algorithim的新手,我在解决链表的整个主题及其与节点的连接方面遇到了一些麻烦。 令我困惑的问题是指针和他们指向的东西 这是教科书中的示例问题 (i) “What happens to the pointer head when the object obj is added to an empty linked list?” head1 = new listNode(obj, head); (ii) Write A Constructor to represent (i) 我已经看到堆栈溢出是获得一些帮助的地方,我需要一些直接的帮助,所有的想法都将深受赞赏。 提前致谢

如何在Java中自定义generics类型链表?

我在java中编写自己的链表,它是generics类型,而不是使用java集合链表。 链表的add方法由以下代码组成: public void add(T item, int position) { Node addThis = new Node(item); Node prev = head; int i; if(position <= 0) { System.out.println("Error: Cannot add element before position 1."); } else if(position == 1) { addThis.setNext(head); head = addThis; } else { for(i = 1; i < position-1; i++) { prev = prev.getNext(); […]

Java:有没有办法有效地插入或删除LinkedList中间的许多元素?

我期待在Java的LinkedList中找到它,因为链表的要点是能够有效地插入(和删除)任何地方(假设你有一些指向你想要插入或删除的位置的指针)。 我在API中找不到任何东西。 我忽略了什么吗? 我能找到的最接近的是ListIterator中的add和remove方法。 但这有一些局限性。 特别是,根据API,只要通过remove修改底层LinkedList,其他迭代器就会变为无效。 这也是在我的测试中诞生的; 以下程序导致IllegalStateException: import java.util.*; public class RemoveFromLinkedList { public static void main(String[] args) { LinkedList myList= new LinkedList(); for (int i = 0; i < 10; ++i) { myList.add(i); } ListIterator i1 = myList.listIterator(); ListIterator i2 = myList.listIterator(); for (int i = 0; i < 3; ++i) { i1.next(); […]

java – 如何从linkedlist中删除节点?

此代码是一个表,其中包含Inert name,delete,show和quit选项。 这段代码运行良好,但我唯一的问题是如何删除节点中的选定名称 class Node{ Node in; String name; public Node(){ in = null; } public Node(String n){ in = null; name = n; } public void setIn(Node n){ in = n; } public Node getIn(){ return in; } public void setName(String n){ name = n; } public String getName(){ return name; } public class […]

如何在Java中实现链接列表?

我试图在Java中实现一个简单的HashTable,它使用链接列表进行冲突解决,这在C中很容易做到,但我不知道如何用Java做,因为你不能使用指针.. 。 首先,我知道这些结构已经用Java实现了,我不打算使用它,只是在这里训练…… 所以我创建了一个元素,它是一个字符串和指向下一个Element的指针: public class Element{ private String s; private Element next; public Element(String s){ this.s = s; this.next = null; } public void setNext(Element e){ this.next = e; } public String getString(){ return this.s; } public Element getNext(){ return this.next; } @Override public String toString() { return “[” + s + “] => […]