linked-list

record voice in a Queue<byte[]> and send it to the server

强颜欢笑 提交于 2019-12-19 09:44:01
问题 I am developing voice application. I need a buffer queue of some sort so that i record continuosly in a thread , place the buffers full of bytes into the queue and to transmit to the server, and i take the next buffer from the queue. Here is the recording code: Queue<byte[]> qArray = new LinkedList<byte[]>(); recordingThread = new Thread(new Runnable() { @Override public void run() { bData = new byte[BufferElements]; while (isRecording) { recorder.read(bData, 0, BufferElements); qArray.add

Reversing a Doubly Linked List

瘦欲@ 提交于 2019-12-19 03:14:13
问题 This method right below reverses a doubly linked list with n elements. I dont understand how this really works. I have added comments, please correct me if I am wrong. I am not sure how the traversing process works. public void reverseDLL( ) { Node temp=head; //swap head and tail head=tail; // head now points to tail tail=temp; //tail points to head //traverse the list swapping prev and next fields of each node Node p=head; //create a node and point to head while(p!=null) //while p does not

How is LinkedList's add(int, E) of O(1) complexity?

点点圈 提交于 2019-12-18 18:39:59
问题 From the linked-list tag wiki excerpt: A linked list is a data structure in which the elements contain references to the next (and optionally the previous) element. Linked lists offer O(1) insert and removal at any position , O(1) list concatenation, and O(1) access at the front (and optionally back) positions as well as O(1) next element access. Random access has O(N) complexity and is usually unimplemented. (emphasis mine) I was surprised to read this – how can the list insert at a random

How do you copy a linked list into another list?

落爺英雄遲暮 提交于 2019-12-18 17:25:41
问题 I'm studying data structures and linked lists, but I'm not getting the concept of how to make a copy of a linked list. Can someone explain this, possibly using pseudocode or C code? 回答1: The logic for duplicating a linked list is recursive and based on the following observations: The clone of the empty list is the empty list. The clone of a list with first node x and remaining nodes xs is a copy of x prepended to a clone of xs. If you encode the linked list in C++, this can be very clean:

Thread-safe deletion of a linked list node, using the fine-grained approach

北城以北 提交于 2019-12-18 13:39:27
问题 Why is the following snippet for deleting a node in a linked list not thread safe? edit: note every node has a lock of its own // ... lock acquisition here // ... assumption found to be valid here prev->next = p->next; p->next = NULL; p->deleted = 1; 回答1: It is thread safe assuming the scope of your lock (meaning what it locks, nothing to do with the official term "scope" used in C) is large enough. If it locks just the current node p , then you can't rely on other threads not coming in and

Which one runs faster, ArrayList or LinkedList? [duplicate]

流过昼夜 提交于 2019-12-18 12:55:30
问题 This question already has answers here : When to use LinkedList over ArrayList in Java? (32 answers) Performance differences between ArrayList and LinkedList (11 answers) Closed 6 years ago . List li = new LinkedList(); for (int i = 0; i < 100; i++) { li.add(i); } long start1 = System.nanoTime(); li.get(57); long end1 = System.nanoTime(); long diff1 = end1-start1; System.out.println("Time taken by LinkedList = "+diff1); List al = new ArrayList(); for (int i = 0; i < 100; i++) { al.add(i); }

Creating and Understanding linked lists of structs in C

99封情书 提交于 2019-12-18 12:08:05
问题 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

Python linked list O(1) insert/remove

China☆狼群 提交于 2019-12-18 11:34:16
问题 I am looking for a linked list and related algorithms implementation for Python. Everyone I ask just recommends using built in Python lists, but performance measurements indicate that list insertion and removal is a bottleneck for our application. It's trivial to implement a simple linked list, but I wonder if there is a mature library which includes some operations like sort, merge, splice, search, lower/upper bound, etc... I know this is a dupe, but searching for python list on any search

strategies to reverse a linked list in JavaScript

放肆的年华 提交于 2019-12-18 11:27:05
问题 I just struggled through a simple interview question: Please reverse a singly linked list. While I failed to provide a working answer in time to save the interview, I was able to come up with a solution afterwards. Is my solution correct? How would you analyze this with Big-Oh? Are there more efficient ways to reverse a singly linked list? // reverse a linked list var reverseLinkedList = function(linkedlist) { var node = linkedlist; var previous = null; while(node) { // reverse pointer node

C code for XOR linked list

谁说我不能喝 提交于 2019-12-18 10:27:13
问题 I have been trying to implement XOR linked list and its operations but I have not been able to do it properly. Is it possible to implement it in C since XOR link list involves operations on addresses? I would be very thankful if some actual working code is given. 回答1: That's an interesting idea that I have not seen before. With today's fairly abundant memory, it seems like a lot of complexity for little gain (although not all platforms are flush with memory). Edit While doing my real work, my