I am trying to write a program to handle a linked list that is connected from all sides(i.e. each node has a pointer to the node above, below, and to the left and right). I know that each node is not yet connected on all sides, only on one. I also want to maintain a node pointer named current that will always point to the newest node created. Anyways, I am already having trouble. I very new with linked lists, so please excuse my ignorance. Here's the program:
Sorry for the length. In my console writes "(lldb)"(I'm using Xcode, but I assume the error just has to do with my code ignoring some concept about pointers). I greatly appreciate any help,
Where do you ever initialise start? It looks to me like start is pointing to an undefined memory location, so therefore current is pointing to an undefined memory location (because you set current to be equal to start in the linked_list constructor).
Actually, why are you initialising current in that constructor? It's a global variable, not a member of linked_list. Imagine if you had two linked_list objects; when you instantiated the second one, you'd overwrite the value of current with a new one.
Can nodes be connected in a grid? If so then what happens when you add/delete one node in the grid?
Looking at your add_node(), I suggest that you concentrate on getting just one direction working first. Let's take the first one: 'l'. When inserting a node you have to worry about four pointers:
- the new nodes left and right pointers
- the right pointer of the node to the left
- the left pointer of the node to the right.
And you have to worry about whether the left and/or right nodes might be missing.
I very new with linked lists
You've picked a very difficult problem for a beginner. A doubly linked list (right & left, or previous & next), is hard enough. 4 ways sounds very difficult. Are you sure you need to do it this way? Is the for another problem that you're trying to solve? Maybe there's an easier way.