linked-list

Is it OK to use “delete this” to delete the current object?

随声附和 提交于 2019-12-17 16:49:31
问题 I'm writing a linked list and I want a struct's destructor (a Node struct) to simply delete itself, and not have any side effects. I want my list's destructor to iteratively call the Node destructor on itself (storing the next node temporarily), like this: //my list class has first and last pointers //and my nodes each have a pointer to the previous and next //node DoublyLinkedList::~DoublyLinkedList { Node *temp = first(); while (temp->next() != NULL) { delete temp; temp = temp->next(); } }

Linked List vs Vector

一世执手 提交于 2019-12-17 15:44:18
问题 Over the past few days I have been preparing for my very first phone interview for a software development job. In researching questions I have come up with this article. Every thing was great until I got to this passage, "When would you use a linked list vs. a vector? " Now from experience and research these are two very different data structures, a linked list being a dynamic array and a vector being a 2d point in space. The only correlation I can see between the two is if you use a vector

Why is inserting in the middle of a linked list O(1)?

拈花ヽ惹草 提交于 2019-12-17 15:19:43
问题 According to the Wikipedia article on linked lists, inserting in the middle of a linked list is considered O(1). I would think it would be O(n). Wouldn't you need to locate the node which could be near the end of the list? Does this analysis not account for the finding of the node operation (though it is required) and just the insertion itself? EDIT : Linked lists have several advantages over arrays. Insertion of an element at a specific point of a list is a constant-time operation, whereas

Binary Trees vs. Linked Lists vs. Hash Tables

泪湿孤枕 提交于 2019-12-17 15:04:13
问题 I'm building a symbol table for a project I'm working on. I was wondering what peoples opinions are on the advantages and disadvantages of the various methods available for storing and creating a symbol table. I've done a fair bit of searching and the most commonly recommended are binary trees or linked lists or hash tables. What are the advantages and or disadvantages of all of the above? (working in c++) 回答1: Your use case is presumably going to be "insert the data once (e.g., application

How to sort a linked list using bubble-sort?

半城伤御伤魂 提交于 2019-12-17 10:46:50
问题 I am trying to use bubble-sort in order to sort a linked list. I use curr and trail in order to traverse thru the list. curr is supposed to be one step ahead of trail always. This is my code so far: void linked_list::sort () { int i,j=0; int counter=0; node *curr=head; node *trail=head; node *temp=NULL; while (curr !=NULL) { curr=curr->next; //couting the number of items I have in my list. counter++; //this works fine. } curr=head->next; // reseting the curr value for the 2nd position. for (i

Lock-Free Concurrent Linked List in Java

﹥>﹥吖頭↗ 提交于 2019-12-17 10:25:44
问题 I would like to use a Linked List like the one described in this paper. However, I didn't find any Java implementation in the web. If no java implementation of the above mentioned Linked List exists, I think, I would use the java.util.concurrent.ConcurrentLinkedQueue<E> . Is this a good choice (it is not really a linked list)? If it's not a good choice, does anyone know of a reliable concurrent (thread-safe) wait-free(lock-free) Linked List implementation in Java? 回答1: ConcurrentLinkedQueue

sorting a doubly linked list with merge sort

做~自己de王妃 提交于 2019-12-17 10:01:15
问题 I have found this code in the internet and it was for arrays ,I want to change it for doubly linked list(instead of index we should use pointer) would you please help me that how can i change merge method(I have changed sort method by myself) also this is not my home work ,I love working with linked list!! public class MergeSort { private DoublyLinkedList LocalDoublyLinkedList; public MergeSort(DoublyLinkedList list) { LocalDoublyLinkedList = list; } public void sort() { if

Is a Linked-List implementation without using pointers possible or not?

南楼画角 提交于 2019-12-17 09:33:51
问题 My question is very simple, can one using C++, implement a link-list data structure without using pointers (next nodes)? To further qualify my question, I'm mean can one create a Linked-List data structure using only class instantiations. A common node definition might be like so: template<typename T> struct node { T t; node<T>* next; node<T>* prev; }; I'm aware of std::list etc, I'm just curious to know if its possible or not - and if so how? Code examples will be greatly appreciated. More

Why is ArrayDeque better than LinkedList

我与影子孤独终老i 提交于 2019-12-17 08:00:37
问题 I am trying to to understand why Java's ArrayDeque is better than Java's LinkedList as they both implement Deque interface. I hardly see someone using ArrayDeque in their code. If someone sheds more light into how ArrayDeque is implemented, it would be helpful. If I understand it, I will be more confident using it. I could not clearly understand the JDK implementation as to the way it manages head and tail references. 回答1: Linked structures are possibly the worst structure to iterate with a

Linked list recursive reverse

我与影子孤独终老i 提交于 2019-12-17 07:12:51
问题 I was looking at the code below from stanford library: void recursiveReverse(struct node** head_ref) { struct node* first; struct node* rest; /* empty list */ if (*head_ref == NULL) return; /* suppose first = {1, 2, 3}, rest = {2, 3} */ first = *head_ref; rest = first->next; /* List has only one node */ if (rest == NULL) return; /* put the first element on the end of the list */ recursiveReverse(&rest); first->next->next = first; /* tricky step -- see the diagram */ first->next = NULL; /* fix