linked-list

Array of Linked Lists C++

耗尽温柔 提交于 2019-12-03 18:14:21
So I thought I understood how to implement an array of pointers but my compiler says otherwise =(. Any help would be appreciated, I feel like I'm close but am missing something crucial. 1.) I have a struct called node declared:. struct node { int num; node *next; } 2.) I've declared a pointer to an array of pointers like so: node **arrayOfPointers; 3.) I've then dynamically created the array of pointers by doing this: arrayOfPointers = new node*[arraySize]; My understanding is at this point, arrayOfPointers is now pointing to an array of x node type, with x being = to arraySize. 4.) But when I

Production code for finding junction in a linked list

爷,独闯天下 提交于 2019-12-03 17:45:56
I was asked this question in some interview. I was required to write code for finding junction in a linked list (which is in form of Y with both arms not necessarily equal) for production environment in O(1) space and linear time. I came up with this solution (which i had previously seen somewhere) : 1. Measure lengths of both lists, let them be l1 and l2 2. Move the pointer of larger list by |(l1-l2)|. 3. Now move together both the pointers, if they point to same location, that is the junction. Interviewer: How will your code handle ? Case 1. The Y-format linked list has loop in the end after

double free or corruption (fasttop)

邮差的信 提交于 2019-12-03 16:14:19
问题 The following section of my code gives me this messege when executing * glibc detected ./a.out: double free or corruption (fasttop): 0x08e065d0 ** i have gone through the code many times but i cant clealry see how i am misusing the free (temp2) bool found= false; int x=0; for ( x=0; x<=312500; x++) { while (count <=32) { fscanf (file, "%d", &temp->num); temp->ptr=NULL; newNode = (NODE *)malloc(sizeof(NODE)); newNode->num=temp->num; newNode->ptr=NULL; if (first != NULL) { temp2=(NODE *)malloc

Visual Explanation Guidance needed for reversal of Linked List datastructure code?

不想你离开。 提交于 2019-12-03 15:50:37
I have following piece of code for reversing the linked list. I am getting confused in while loop and so would certainly appreciate if someone can provide visual explanation of how actually it's working. static void Reverse (struct node** headRef) { struct node* result = NULL; struct node* current = *headref; struct node* next; while(current != NULL) { next = current->next; current->next = result; result = current; current = next; } *headRef = result; } OK, here's my attempt to make valya's answer even clearer (though I thought it was pretty good already): Say we have this list: // a->b->c->d-

Linked List Concatenation In O(1) Time

五迷三道 提交于 2019-12-03 15:14:15
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 explanation will be most helpful. Thanks. You can easily concatenate two lists in O(1) time using either a

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

[亡魂溺海] 提交于 2019-12-03 14:49:34
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)? It's difficult to compare the two, because the patterns of their memory usage are quite different. Vector resizing A vector resizes itself

How to properly define an array of linked list in Java ? [duplicate]

萝らか妹 提交于 2019-12-03 14:31:19
This question already has answers here : Java 1.6: Creating an array of List<T> (4 answers) I tried to define an array of linked list in Java like the following, which compiled fine but it generated 2 warning messages. LinkedList<Long> [] hashtable = new LinkedList[10]; warning: [rawtypes] found raw type: LinkedList LinkedList<Long> [] hashtable = new LinkedList[10]; ^ missing type arguments for generic class LinkedList<E> where E is a type-variable: E extends Object declared in class LinkedList HashTable.java:13: warning: [unchecked] unchecked conversion LinkedList<Long> [] hashtable = new

Creating a linked list or similar queue in MySQL?

戏子无情 提交于 2019-12-03 14:29:41
I have a table of items that need to be displayed in a certain order, but that order can be changed. Items can be added at the beginning, end, or in the middle, and items can be rearranged. How can I set up the table to keep track of that order in such a way that it's easy to modify but the list can also be fetched in order with a single query? For example, I could have a "NEXT_ID" column to do it linked list-style, but then how would I run a SELECT query to get the rows in order of the NEXT_ID chain? Apologies in advance for the super-obvious solution I'm probably missing. I have this problem

What is LinkedListNode in Java

偶尔善良 提交于 2019-12-03 13:41:33
Excuse my ignorance but I am beginning to prepare for my first technical interview and came across this question and answer on the topic linkedlist Question: Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node public static boolean deleteNode(LinkedListNode n) { if (n == null || n.next == null) { return false; // Failure } LinkedListNode next = n.next; n.data = next.data; n.next = next.next; return true; } I want to start playing with this code (making changes compile test) but I'm not sure how to start doing this in Java. I cannot find

How to count the number of nodes in a linked list without traversing it?

点点圈 提交于 2019-12-03 13:38:30
I have been asked in an interview how to count the number of nodes in a linked list without traversing the list? Is there any way to achieve this? The only way I can think of is to add a counter of the number of nodes which is incremented each time the add or insert methods are invoked, and decremented when delete is invoked. You cannot make assumptions about memory occupied because, being a linked list, you cannot guarantee that all nodes will be in the same memory block (indeed, this is highly improbable). If you are doing allocation dynamically with something like malloc then there is no