linked-list

Free() pointer to Struct, inside a Struct

≯℡__Kan透↙ 提交于 2019-12-11 13:11:17
问题 I can't seem to find how to free() the sub struct. Structs: typedef struct { char ID[5]; location *loc; } observer; typedef struct { double lat; double lng; } location; My Wrapper of Free() is: void free_obs_list_node(void *data) { free(*(observer **)data); } I can free the observer struct. I cannot free the pointer to the location Struct. This is my question: how do I free the location struct pointed to by location *loc; I used a wrapper of free() as these are node's in a generic linked list

Removing from Doubly LinkedList in O(1)

喜夏-厌秋 提交于 2019-12-11 12:47:57
问题 I know that's the time complexity to remove an element from doubly linkedlist is O(1) and it seems obvious and clear BUT what happens if we don't receive an element to remove it but the one which is wrapped by the element? for example if we define a class Element to wrap a String (To provide pointer pointing to the next and previous element) , we can do the removal in O(1) if the method receives that element as input not the string ! if the remove method receives the string , it has to search

What is the difference between passing &var to *var and var to var?

烂漫一生 提交于 2019-12-11 12:44:28
问题 Basically, I want to know why this (passing the memory adress of list as parameter): void init_lista (elemPtr *list) { *list = NULL; } int main(){ elemPtr list; init_list(&list); //[...] } is different than this (passing just the content of list): void init_lista (elemPtr list) { list = NULL; } int main(){ elemPtr list; init_list(list); //[...] } OBS: elemPtr is a pointer type of a structure ( typedef struct elem *elemPtr ). What I understand from & and * is that the first will get the var's

C++ Linked list

你离开我真会死。 提交于 2019-12-11 12:35:01
问题 I'm trying to make linked list similar too the one here: linked list in C That is to have the "head", I called it first, inside another struct. However I found doing that change. Makes it hard to add values to the list_item struct. I have tried some few things to see if it works. It compiles, however when I run the code it will crash. Any help would be helpful here. I know the cause of the crash is when I want to point the new_node to the linked_list. #include <iostream> using namespace std;

insert to sorted position linked list

懵懂的女人 提交于 2019-12-11 12:27:10
问题 I have question quite much related to this one I asked a while ago place a value in the sorted position immediately I wonder if you can use the same approach in that you step backward in a linked list to find the position it should be inserted into. If it is possible how do you loop a linked list backward? I can't figure it out because it seems not possible since it should be a double linked listed then if I'm not wrong? Anyway I'm working with singly linked list. EDIT I think I'm going for

Shifting big numbers

こ雲淡風輕ζ 提交于 2019-12-11 11:29:10
问题 X = 712360810625491574981234007851998 is represented using a linked list and each node is an unsigned int Is there a fast way to do X << 8 X << 591 other than X * 2^8 X * 2^591 ? 回答1: Bit shifting is very easy in any arbitrary number of bits. Just remember to shift the overflowed bits to the next element. That's all Below is a left shift by 3 example uint64_t i1, i2, i3, o1, o2, o3; // {o3, o2, o1} = {i3, i2, i1} << 3; o3 = i3 << 3 | i2 >> (32 - 3); o2 = i2 << 3 | i1 >> (32 - 3); o1 = i1 << 3

Memory leak in removehead function

旧巷老猫 提交于 2019-12-11 11:17:53
问题 Hey everyone I am getting a memory leak in my remove head function. I think it could be that im not freeing the head but I can't seem to find the correct place to put it. I was thinking right above the line that says L->head = L->head->next; data_t * removehead(list_t *L) { data_t *temp = NULL; } if (L->head != NULL) { temp = L->head->data_ptr; L->head = L->head->next; L->size--; } return temp; } Any thoughts? 回答1: It's a matter of taste, if you don't want to declare a specific variable then

How to manage different kinds of data in a linked list?

ぃ、小莉子 提交于 2019-12-11 10:58:50
问题 I have a project that the teacher asks us to do some operations in a linked list. Okay, they are pretty easy to implement but I'm having trouble to manage the data inside of my list. They can be any of those: int, char, float or string(char array). I know how to link any of those individually but when they're mixed up things start to get messy. I have not tried much, I'm stuck. Here are some thoughts that passed through my mind: create 4 structs, 1 for each data type (but I've never seen a

Sortable linked list of objects

主宰稳场 提交于 2019-12-11 10:57:37
问题 For a school lab I have to make a linked list of messages and then sort those messages by priority, with "High" priority being pulled out first, then medium, then low. I've been trying to figure this out for days and I can't wrap my mind around the sorting. I've been trying to get it to sort without adding anything other than a head and a size field in my ListofMessages class but all I seem to do is add garbage code. I wanted to figure this out myself but right now I'm stumped. Here's what I

Scanning in a text file into a linked list

冷暖自知 提交于 2019-12-11 10:16:57
问题 I'm just learning about linked lists and I have to do an assignment that has many parts, but I'm starting out and the very first thing I need to do is read in an input file into a linked list. Part of the file is: George Washington, 2345678 John Adams, 3456789 Thomas Jefferson, 4567890 James Madison, 0987654 James Monroe, 9876543 John Quincy Adams, 8765432 and contains a total of 26 lines. All I want to do now is simply read in the file. I try by using this code (in main for now) #include