Problems with a linked list where every node has a pointer to the left, right, above, and below.

Hello,

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <time.h>
using namespace std;


class node
{
public:
    
    node();
    
    node * left;
    node * right;
    node * up;
    node * down;
    
    int num;
}*start,*current;

node::node()
{
    srand(time(NULL));
    this->num=rand()%1000;
}

class linked_list
{
public:
    linked_list();
    
    void add_node(char dir);
    void print();
    
};

linked_list::linked_list()
{
    current=start;
}

void linked_list::add_node(char dir)
{
    if(dir=='l')
    {
        node *new_node;
        new_node=new(node);
        
        current->left=new_node;
        new_node->right=current;
        
        current=new_node;
    }
    else if(dir=='r')
    {
        node *new_node;
        new_node=new(node);
        
        current->right=new_node;
        new_node->left=current;
        
        current=new_node;
    }
    else if(dir=='u')
    {
        node *new_node;
        new_node=new(node);
        
        current->up=new_node;
        new_node->down=current;
    
        current=new_node;
    }
    else if(dir=='d')
    {
        node *new_node;
        new_node=new(node);
        
        current->down=new_node;
        new_node->up=current;
        
        current=new_node;
    }
}

void linked_list::print()
{
    cout<<current->num<<endl<<endl;; //EXC_BAD_ACCESS(code=1,address=0x20)
}

int main()
{
    linked_list myList;
    string ans;
    
    while(true)
    {
        cin>>ans;
        if(ans=="left")
        {
            myList.add_node('l');
        }
        else if(ans=="right")
        {
            myList.add_node('r');
        }
        else if(ans=="up")
        {
            myList.add_node('u');
        }
        else if(ans=="down")
        {
            myList.add_node('d');
        }
        else if(ans=="print")
        {
            myList.print();
        }
        else if(ans=="break")
        {
            break;
        }
    }
    
    return 0;
}



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,

Thank you!
Forgot to mention: This error occurs when I write "print" in the console.

Thank you!
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.
Where do you ever initialise start?
As a global, it is zero-initialized.

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.
As a global, it is zero-initialized.

Ah, I'd forgotten that - thanks for the correction!

Topic archived. No new replies allowed.