Lets say you have 2 classes:
- Node
- Doubly linked list (DLL)
Your main creates a bunch of nodes, who know something about themselves, like their name for example and then calls add(Node*) method of the DLL.
In order to keep track of these Node pointers, (i would think) DLL should maintain an array of them. Is there a way to avoid it? (or any other data structure for that matter)
How is DLL implemented under the hood? Does it use arrays or something else?
There is a way to avoid it. You in fact don’t need to keep track of all the pointers in the one spot at all, because the
Nodes themselves ARE the data structure.EDIT: Just saw your Objective-C comment.
The
Nodeshould have (Objective-C syntax):And then your
DLLclass is simply a class with:That’s all you need. Inserting or deleting a Node involves pointer shuffling and a
countadjustment, and access is sequential from either end.add:(Node*)newNodewould literally be:..whereas
add:(Node*)newNode atIndex:(NSUInteger)indexwould be a little more complex: