linked-list

Array/Linked list: performance depends on the *direction* of traversal? [closed]

痴心易碎 提交于 2019-12-03 03:13:58
This post is divided to two major sections. The first section introduces the original test cases & results, and my thoughts about it. The second section details the modified test case, and its results. The original title of this topic was "Full iteration over an array significantly faster than with a linked list". The title was changed due to the newer test results (presented in Section Two). SECTION ONE: The Original Test Case For a full one-directional sequential traversal, it's known that the linked list and the array have similar performance, but due to the cache-friendliness (reference

Finding the “Nth node from the end” of a linked list

∥☆過路亽.° 提交于 2019-12-03 02:29:54
This seems to be returning the correct answer, but I'm not sure if this is really the best way to go about things. It seems like I'm visiting the first n nodes too many times. Any suggestions? Note that I have to do this with a singly linked list. Node *findNodeFromLast( Node *head, int n ) { Node *currentNode; Node *behindCurrent; currentNode = head; for( int i = 0; i < n; i++ ) { if( currentNode->next ) { currentNode = currentNode->next; } else { return NULL; } } behindCurrent = head; while( currentNode->next ) { currentNode = currentNode->next; behindCurrent = behindCurrent->next; } return

Algorithm for Shuffling a Linked List in n log n time

爷,独闯天下 提交于 2019-12-03 02:26:50
问题 I'm trying to shuffle a linked list using a divide-and-conquer algorithm that randomly shuffles a linked list in linearithmic (n log n) time and logarithmic (log n) extra space. I'm aware that I can do a Knuth shuffle similar to that could be used in a simple array of values, but I'm not sure how I would do this with divide-and-conquer. What I mean is, what am I actually dividing? Do I just divide to each individual node in the list and then randomly assemble the list back together using some

Why does std::list::reverse have O(n) complexity?

爱⌒轻易说出口 提交于 2019-12-03 02:06:51
问题 Why does the reverse function for the std::list class in the C++ standard library have linear runtime? I would think that for doubly-linked lists the reverse function should have been O(1). Reversing a doubly-linked list should just involve switching the head and the tail pointers. 回答1: Hypothetically, reverse could have been O(1) . There (again hypothetically) could have been a boolean list member indicating whether the direction of the linked list is currently the same or opposite as the

Is the LinkedList in .NET a circular linked list?

自古美人都是妖i 提交于 2019-12-03 01:41:53
I need a circular linked list, so I am wondering if LinkedList is a circular linked list? No. It is a doubly linked list, but not a circular linked list. See MSDN for details on this . LinkedList<T> makes a good foundation for your own circular linked list, however. But it does have a definite First and Last property, and will not enumerate around these, which a proper circular linked list will. Ady Kemp A quick solution to using it in a circular fashion, whenever you want to move the "next" piece in the list: current = current.Next ?? current.List.First; Where current is LinkedListNode<T> .

Difference between LIST_HEAD_INIT and INIT_LIST_HEAD

百般思念 提交于 2019-12-03 01:20:31
I'm trying to understand the Linux kernel linked list API. According to Linux Kernel Linked List I should initialize the list head by INIT_LIST_HEAD but here (Linux Kernel Program) it's suggested to use LIST_HEAD_INIT instead. Here's a working code I wrote, but I'm not sure if I did it in proper way. Could someone verify that it's OK? #include <stdio.h> #include <stdlib.h> #include "list.h" typedef struct edge_attr { int d; struct list_head list; } edge_attributes_t; typedef struct edge { int id; edge_attributes_t *attributes; } edge_t; int main () { int i; struct list_head *pos; edge

Creating and Understanding linked lists of structs in C

别等时光非礼了梦想. 提交于 2019-12-03 00:42:21
I am having trouble grasping the concepts of struct and the linked list data structure together. For example lets say we have have this code: a struct that has a worker's content and a linked list of these structs that contains nodes of each worker and a pointer to the next node(?). typedef struct Schedule { char name[10]; char description[10]; int hours; int workordernum; } Work; typedef struct linkedlist { struct Schedule work; struct linkedlist *next; } Node; The problem is how do you make a method that always adds a node in the beginning of the list, a method that adds it anywhere(middle)

Arrays vs Linked Lists in terms of locality

我们两清 提交于 2019-12-02 23:37:20
Say we have an unsorted array and linked list. The worst case when searching for an element for both data structures would be O( n ), but my question is: Would the array still be way faster because of the use of spatial locality within the cache, or will the cache make use of branch locality allowing linked lists to be just as fast as any array ? My understanding for an array is that if an element is accessed, that block of memory and many of the surrounding blocks are then brought into the cache allowing for much faster memory accesses. My understanding for a linked list is that since the

Are vector a special case of linked lists?

半腔热情 提交于 2019-12-02 23:29:33
When talking about the STL, I have several schoolmates telling me that "vectors are linked lists". I have another one arguing that if you call the erase() method with an iterator, it breaks the vector, since it's a linked list. They also tend to don't understand why I'm always arguing that vector are contiguous, just like any other array, and don't seem to understand what random access means. Are vector stricly contiguous just like regular arrays, or just at most contiguous ? (for example it will allocate several contiguous segments if the whole array doesn't fit). In silico I'm sorry to say

Return two arrays in a method in Java

白昼怎懂夜的黑 提交于 2019-12-02 22:43:04
Considering I have two arrays for example: String[] array1 = new String[10]; int[] array2= new int[10]; So that inside a method I've computed two arrays, namely array1 & array2 and now I want to return both of these arrays. How should I go about it? I read here that I can make another class and define certain object types and encapsulate these arrays in that class constructor, but I'm still confused and did not understand completely. If you could show me a working example which does that, or may be any similar idea, it would be good. anubhava You can actually return something like this also: