linked-list

Thread-safe sorted linked list

半城伤御伤魂 提交于 2019-12-09 13:21:30
问题 I'm trying to write a thread-safe sorted single linked list . I wrote two versions: coarse grained synchronization and fine grained synchronization. Here are the two implementations: Fine grained: 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

Linked List Concatenation In O(1) Time

我怕爱的太早我们不能终老 提交于 2019-12-09 13:13:25
问题 I came upon an interesting question and I am puzzled with the answer provided to me. The question is as follows: The concatenation of 2 lists can be performed O(1) time. Which of the following implementation of list should be used? - Singly Linked List - Doubly Linked List - Circular Linked List - Array Implementation Of Linked List I initially thought that a DLL would be the correct choice as concatenation can happen from both side, however the answer seems to CLL. I am confused. Any

Linked list vs dynamic array for implementing a stack using vector class

旧城冷巷雨未停 提交于 2019-12-09 12:48:00
问题 I was reading up on the two different ways of implementing a stack: linked list and dynamic arrays. The main advantage of a linked list over a dynamic array was that the linked list did not have to be resized while a dynamic array had to be resized if too many elements were inserted hence wasting alot of time and memory. That got me wondering if this is true for C++ (as there is a vector class which automatically resizes whenever new elements are inserted)? 回答1: It's difficult to compare the

Why no immutable double linked list in Scala collections?

安稳与你 提交于 2019-12-09 09:25:45
问题 Looking at this question, where the questioner is interested in the first and last instances of some element in a List , it seems a more efficient solution would be to use a DoubleLinkedList that could search backwards from the end of the list. However there is only one implementation in the collections API and it's mutable. Why is there no immutable version? 回答1: Because you would have to copy the whole list each time you want to make a change. With a normal linked list, you can at least

Creating a node class in Java

a 夏天 提交于 2019-12-09 09:06:48
问题 So I'm fairly new to Java and programming and I was wondering how to create a node class? So far I have: public class ItemInfoNode{ private ItemInfoNode next; private ItemInfoNode prev; private ItemInfo info; public ItemInfoNode(ItemInfo info, ItemInfoNode next, ItemInfoNode prev){ info = info; next = next; prev = prev; } public void setInfo(ItemInfo info){ info = info; } public void setNext(ItemInfoNode node){ next = node; } public void setPrev(ItemInfoNode node){ prev = node; } public

Scala 2.11 LinkedList is deprecated, what should I use?

天涯浪子 提交于 2019-12-09 08:08:51
问题 According to the docs, scala.collection.mutable.LinkedList is deprecated as of the 2.11 version. Unfortunately I have found nothing to replace it with. I need an ordered collection that can remove an item from any index in constant time. What should I use? 回答1: Use MutableList and its iterator's remove method. They provide O(1) removal. http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html#linked_lists 回答2: From what I understand of your problem you want to

C++ “Object” class

牧云@^-^@ 提交于 2019-12-09 05:08:27
问题 In Java, there is a generic class called "Object", in which all classes are a subclass of. I am trying to make a linked list library (for a school project), and I have managed it to make it work for only one type, but not multiple, so is there anything similar to that? EDIT: I would post the code, but I don't have it on me at this time. 回答1: There's no generic base class in C++, no. You can implement your own and derive your classes from it, but you have to keep collections of pointers (or

Dynamically allocated linked list in c++.What to do after exception to prevent memory leak?

房东的猫 提交于 2019-12-09 01:58:31
问题 I like to implement linked list in c++ ,while adding new node I dynamically allocate it, if some allocation fails I would like my program to stop the execution. After the "new Node" fails the exception is thrown so do I have to call the destructor explicitly in the exception handler. how can I deal with this situation in order to prevent memory leak? Here is my code that I have written LinkedList.h #pragma once #include <iostream> #include <string.h> using namespace std; class LinkedList {

Linked-list in C++ using references instead of pointers

僤鯓⒐⒋嵵緔 提交于 2019-12-09 00:39:04
问题 Suppose I want to create an unmodifiable linked-list (i.e. it can only be traversed, no nodes can be added or removed once it was initially created). This could be easily implemented by: struct ListNode { int value; ListNode* nextNode; } My question is .... Would it be possible to use references instead of pointers? struct ListNodeWithRefs { int value; ListNodeWithRefs &nextNode; } I am not sure it would provide any performance gain at all but ... this question popped up while coding and my

Perfect Balanced Binary Search Tree

最后都变了- 提交于 2019-12-08 19:19:11
问题 I have an theoretical question about Balanced BST . I would like to build Perfect Balanced Tree that has 2^k - 1 nodes, from a regular unbalanced BST . The easiest solution I can think of is to use a sorted Array/Linked list and recursively divide the array to sub-arrays, and build Perfect Balanced BST from it. However, in a case of extremely large Tree sizes, I will need to create an Array/List in the same size so this method will consume a large amount of memory. Another option is to use