linked-list

adding two linked lists efficiently in C

天大地大妈咪最大 提交于 2019-12-03 08:19:53
I have two linked lists representing the digits of decimal numbers in order from most- to least-significant. for eg 4->7->9->6 and 5->7 The answer should be 4->8->5->3 without reversing the lists because reversing the lists would result in decrease of efficiency. I am thinking of solving the problem using stack.I will traverse both the lists and push the data elements into two separate stacks.One for each linked list.Then I pop both the stacks together and add both the elements and if the result is a two digit no I 10 modulo it and store the carry in a temp variable.The remainder is stored in

Queue performance wise which is better implementation - Array or Linked list

ⅰ亾dé卋堺 提交于 2019-12-03 08:03:28
Which way gives the faster enqueueing and dequeuing when I have to insert very few elements, Is array better than a linked list? I need to insert few element and I have to remove and read that removed element from queue. If it is array I may have to modify the indexes every time I remove an element. Inserting and deleting may happen simultaneously also. Which one is better from below case? typedef struct{ mylist list; struct mylistQ *next; }mylistQ; Array Code static mylist myListQ[QUEUESIZE+1]; int qLast = 0; void enqueue_element(mylist qItem) { myListQ[qLast] = qItem; qLast++; } mylist

Best way to store Country codes, names, and Continent in Java

醉酒当歌 提交于 2019-12-03 07:46:20
问题 I want to have a List or Array of some sort, storing this information about each country: 2 letter code Country name such as Brazil Continent/region of the world such as Eastern Europe, North America, etc. I will classify each country into the region/continent manually (but if there exists a way to do this automatically, do let me know). This question is about how to store and access the countries. For example, I want to be able to retrieve all the countries in North America. I don't want to

Delete a node in singly link list

与世无争的帅哥 提交于 2019-12-03 07:36:45
问题 How to delete a node in a singly link list with only one pointer pointing to node to be deleted? [Start and end pointers are not known, the available information is pointer to node which should be deleted] 回答1: You can delete a node without getting the previous node, by having it mimic the following node and deleting that one instead: void delete(Node *n) { if (!is_sentinel(n->next)) { n->content = n->next->content; Node *next = n->next; n->next = n->next->next; free(next); } else { n-

“warning: useless storage class specifier in empty declaration” in struct

浪子不回头ぞ 提交于 2019-12-03 06:57:55
typedef struct item { char *text; int count; struct item *next; }; So I have this struct with nodes defined as above, but Im getting the error below and Im not able to figure out whats wrong. warning: useless storage class specifier in empty declaration }; I'm not sure, but try like that : typedef struct item { char *text; int count; struct item *next; }item; The typedef is useless because you didn't give it a name. You cannot use the typedef in any way. That's why you get a warning, because the typedef is useless. The struct is actually still usable without the warning if you remove the

Efficient data structure for fast random access, search, insertion and deletion

好久不见. 提交于 2019-12-03 06:02:52
问题 I'm looking for a data structure (or structures) that would allow me keep me an ordered list of integers, no duplicates, with indexes and values in the same range. I need four main operations to be efficient, in rough order of importance: taking the value from a given index finding the index of a given value inserting a value at a given index deleting a value at a given index Using an array I have 1 at O(1), but 2 is O(N) and insertion and deletions are expensive (O(N) as well, I believe). A

C++ “Object” class

扶醉桌前 提交于 2019-12-03 05:53:10
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. 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 smart pointers) to take advantage of polymorphism. EDIT: After re-analyzing your question, I have to point out

Why is Haskell's default string implementation a linked list of chars?

流过昼夜 提交于 2019-12-03 05:46:41
问题 The fact that Haskell's default String implementation is not efficient both in terms of speed and memory is well known. As far as I know the [] lists in general are implemented in Haskell as singly-linked lists and for most small/simple data types (e.g. Int ) it doesn't seem like a very good idea, but for String it seems like total overkill. Some of the opinions on this matter include: Real World Haskell On simple benchmarks like this, even programs written in interpreted languages such as

Why is mergesort space complexity O(log(n)) with linked lists?

不打扰是莪最后的温柔 提交于 2019-12-03 05:44:05
问题 Mergesort on an array has space complexity of O(n), while mergesort on a linked list has space complexity of O(log(n)), documented here I believe that I understand the array case, because we need auxiliary storage when merging the two sub-arrays. But wouldn't a linked list merge sort just merge the two sub-linked lists in place? I think this would have space complexity O(1) for creating a new head. In place merge (no auxiliary storage): public Node merge(Node a, Node b) { Node dummyHead, curr

Interview question: remove duplicates from an unsorted linked list

戏子无情 提交于 2019-12-03 03:58:51
问题 I'm reading Cracking the Coding Interview, Fourth Edition: 150 Programming Interview Questions and Solutions and I'm trying to solve the following question: 2.1 Write code to remove duplicates from an unsorted linked list. FOLLOW UP: How would you solve this problem if a temporary buffer is not allowed? I'm solving it in C#, so I made my own Node class: public class Node<T> where T : class { public Node<T> Next { get; set; } public T Value { get; set; } public Node(T value) { Next = null;