RE: [C++] Doubly-Linked Lists - Data Structures 09-05-2015, 01:50 PM
#5
(09-05-2015, 01:31 AM)0xDEAD10CC Wrote: In C++ typically it's not a great idea to use a linked-list, in C it's an easy way to chain things together, but linked-lists in general are very bad for performance. Additionally, they also make it more prone to make tons of cache-misses, and this is even more true the larger the list is. Aside from the allocations too, you have to store pointers bidirectionally so that you can make proper manipulations to the web of data. Linked lists are probably 100 times slower in some cases than a vector because of this. Sure, with a vector you have to move a lot of elements within an allocated space but caches are very efficient at doing this. Using pointers in the way a linked list uses them is like poison to caches.True but this ain't just about speed, linked lists offers a lot more and the fact that you can do whatever you want with any item you want easily is what makes it a little bit special. Now, just like I mentioned in the main thread, the programmer himself needs to know what's needed and what isn't in his application.
(09-05-2015, 01:31 AM)0xDEAD10CC Wrote: You can only use realloc() on memory allocated on the heap with a function like malloc() though, just to clarify.Obviously mate...
(09-05-2015, 01:31 AM)0xDEAD10CC Wrote: Other advantages of a vector:Nice share right there mate.
- It takes care of memory for you, and as of C++11 all STL containers utilize RAII to ensure safe memory management even in the case of an exception
- C++11 - std::vector<bool> is specialized to essentially store each element as a bit in size
(09-05-2015, 01:31 AM)0xDEAD10CC Wrote: edit: Don't forget the semi-colon at the end of your structlol.
Code:struct NODE { int id; char username; NODE *next = NULL; NODE *prev = NULL; }; <---
(09-05-2015, 01:31 AM)0xDEAD10CC Wrote: Why do you keep nodes for front and back though? Just globally too? That may avoid the requirement to linear traverse the linked list for each deletion and insertion, but you have to do this anyways if you want your linked list to be sorted in some way anyways or if you're deleting specific data or searching for something.Keeping nodes for front and back gives you more control over your linked list, ofcorse, in these examples, this doesn't show up much but this will change as soon as you start getting deeper into linked lists which makes their declaration and their usage always a good practice.
For the second question, no one said anything about globally mentioning them, what you see on this thread is pieces of codes to make people think, it ain't a whole program, there ain't no includes, no main, no output and no nothing, again, it's just pieces of code. Now, if I ever wanted to use these then I would probably store them both in a structure and keep it global or pass its address to other functions.
(09-05-2015, 01:31 AM)0xDEAD10CC Wrote: Btw, for your deletion function:True, you can always check prev and next in a node but since we already declared front and rear then why not use them? After all, we have to go for a comparison operation.
Code:if (curr == front) else if (curr == rear)
These checks are not as useful because you should know if they are the front or the back of the linked list simply by the node's contained pointers, you shouldn't have to compare the pointer to the current node at all. If prev is null when you compare the id, then you know that it should be the front, and reversely, if the next pointer is null, then you should automatically be able to deduce that it's the end of the linked list.
i.e. Here's how I'd write it.
Code:void delete_item( int id) { NODE *curr = front; while (curr) { if (curr->id == id) { if (!curr->prev) front = curr->next; else if (!curr->next) rear = curr->prev; curr->prev->next = curr->next; curr->next->prev = cur->prev; delete curr; return; } curr = curr->next; } }
(09-05-2015, 01:31 AM)0xDEAD10CC Wrote: I'd also put the front and rear pointers into an object of its own, that way you don't have to rely on these being global, and you can pass a pointer to the struct containing these 2 pointers to modify them within the function if your strategy is to keep a pointer to the start and end of the doubly linked list.I replied to this in the middle of this whole reply.



![[+]](https://sinister.li/images/modern/collapse_collapsed.png)