linked-list

C Linked List valgrind Invalid Read of Size

南笙酒味 提交于 2019-12-02 03:10:23
I have a problem with my Linked List and the valgrind output. Without further adieu here is my linked list: typedef struct Map map; struct Map { void *address; double free_time; map* next; }*map_list; The list is created using a dummy head node. As you can see, the struct holds an address and a free time, which I try to associate them. In the find_and_free function I search this list using a time and if this time is smaller than the one stored in the list, I deallocate the saved address. And then I deallocate the list node as well. This is the function used to find any free time that is

Implementing a simple singly linked list with smart pointers

天大地大妈咪最大 提交于 2019-12-02 03:07:54
问题 Hi I'm trying to implement a simple singly linked list using smart pointers, here is what I have so far, I opted with using C++'s shared_ptr but I read that a unique_ptr would be more appropriate for this case but, I don't really know how you would iterate over the list (i.e currentNode = currentNode->next) to get to the end of the list in order to insert an element using a unique_ptr. Here is the code I have so far: template <typename T> class LinkedList; template <typename T> class ListNode

How to check if a linked list is a palindrome or not in Java?

老子叫甜甜 提交于 2019-12-02 02:26:16
I wrote a code to check if a singly linked list is a palindrome. And I made two steps: 1st. reverse the original linked list. 2nd. Check if the original and reversed linked list have the same element. public static Boolean isPalindrome(Node input){ Node reversed= reverse(input); while (input!=null){ if(input.item!=reversed.item) return false; input=input.next; reversed=reversed.next; } return true; } static Node head; public static Node reverse(Node input){ if(input==null || input.next==null){ head=input; return input; } else{ reverse(input.next); input.next.next=input; input.next=null; return

Reverse a LinkedList c++ [duplicate]

混江龙づ霸主 提交于 2019-12-02 01:13:48
Possible Duplicate: Unable to reverse a linked list I'm trying to reverse a linked list: void LinkedList::reverseList() { Node *next=_head; Node *prev=0; while(next!=0) { Node *tmp=next->_next; next->_next=prev; prev=next; next=tmp; } } Lets say the list is: 4->3->2->1 When I print the list, I only see 1 (The print function is good). Any help? Thanks Since you said you wanted to find the problem on your own, I'll just give you a hint instead of the solution. Your reverse function works in that it successfully reverses the list. That isn't the problem. You probably have 2 calls to print . One

C adding node to head of linked list

我是研究僧i 提交于 2019-12-02 00:54:19
I have created a linked list struct in c struct node{ int value; struct node* next; }; a method to add a node at the start of the list : void addFirst(struct node *list, int value){ struct node *new_node = (struct node*) malloc (sizeof (struct node)); new_node->value = value; new_node->next = list; list = new_node; } I create a list (malloc and everything), then call this method, it adds the new node inside the method but when i get back to my main my old list remains unchanged. Using DDD debugger to check everything. How is this possible? I am not able to change the method signature so it has

How to insert an item at a given position in a linked list?

你离开我真会死。 提交于 2019-12-02 00:50:00
问题 This how you would add an item: public void insert (Object item) { Link add = new Link(); add.data = item; add.next = head; _head = add; ++_listsize; } But how do you add an item at a given position. So far this is what I got: public void insert (Object item, int pos) { Link add = new Link(); int ix = pos - 1; add.next = _head; for (int i = _listsize - 1; i >= ix; --i) add = add.next; add.data = item; _head = add; ++_listsize; } This will insert the item correctly if it is sequential, but let

What is the performace difference in using List<T> vs LinkedList<T> say in the (c#) library [duplicate]

微笑、不失礼 提交于 2019-12-01 22:04:58
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: When should I use a List vs a LinkedList This question is related to my earlier question which was merged: related to List vs LinkedList If I expect not to use the access by index for my data structure how much do I save by using LinkedList over List ? If if am not 100% sure I will never use access by index, I would like to know the difference. Suppose I have N instances. inserting and removing in a LinkedList

How to insert an item at a given position in a linked list?

时光毁灭记忆、已成空白 提交于 2019-12-01 21:34:29
This how you would add an item: public void insert (Object item) { Link add = new Link(); add.data = item; add.next = head; _head = add; ++_listsize; } But how do you add an item at a given position. So far this is what I got: public void insert (Object item, int pos) { Link add = new Link(); int ix = pos - 1; add.next = _head; for (int i = _listsize - 1; i >= ix; --i) add = add.next; add.data = item; _head = add; ++_listsize; } This will insert the item correctly if it is sequential, but let say I am given a position which is in the middle, what it will do it will insert the item but it will

What is the performace difference in using List<T> vs LinkedList<T> say in the (c#) library [duplicate]

天大地大妈咪最大 提交于 2019-12-01 20:52:11
Possible Duplicate: When should I use a List vs a LinkedList This question is related to my earlier question which was merged: related to List vs LinkedList If I expect not to use the access by index for my data structure how much do I save by using LinkedList over List ? If if am not 100% sure I will never use access by index, I would like to know the difference. Suppose I have N instances. inserting and removing in a LinkedList will only be a o(1) op , where as in List it may me be O(n), but since it it optimized, it would be nice to know what the difference is for some values of n. say N =

Haskell/GHC performance of `any`/`all`

╄→гoц情女王★ 提交于 2019-12-01 19:32:55
I wrote quantification functions exists , forall , and none for Haskell's build-in [] list data type. On multiple occasions, these seemed to prove much more efficient than Prelude / Data.List s any and all . I naively suspect that this performance is due to any and all being implemented using Θ(n) folds. Since I am relatively new to Haskell, I think I must be mistaken, or that there would be a good reason for this phenomenon. From Data.Foldable : -- | Determines whether any element of the structure satisfies the predicate. any :: Foldable t => (a -> Bool) -> t a -> Bool any p = getAny #.