-4

Normally, I would just input head = new Node in main, which would set everything up, but the stipulation is that I do not have permission to mess with global variables. This is an assignment where I only have access to main, and due to other backend features, I have to leave the global variables intact so I can't overwrite it with head = new Node.

The point is just to add characters to a linked list. I just hardcoded inserting one just as an example, but I still can't avoid the error.

Is there a correct way to add them?

struct Node{
  char key;
  Node *next;
};

Node *head = NULL;

int main(){
    char x = 'a';
    cout<<x<<endl;
    head->key=x;

}

assignment: Find all nodes in a BST that are between a given range of values. Then build a linked list of the values and the list should be in ascending order.

NOTE: the head of the linked list is declared globally in the back end and its initial value is NULL. Just add the nodes to the linked list using head. The printing of the linked list will also be done in the backend. Helper functions can be used.

void RangeSearch(TreeNode *node, char m, char n);

  • You need to allocate some memory for it befor you use it (hint: `head = new Node();`) – πάντα ῥεῖ Mar 07 '17 at 07:51
  • If `head` is pointing to null then you cannot "add a value" without invoking UB – UnholySheep Mar 07 '17 at 07:52
  • 2
    _"but the stipulation is that I do not have permission to mess with global variables"_ Huh? you must have been misunderstanding something from that assignment. – πάντα ῥεῖ Mar 07 '17 at 07:54
  • I probably am misunderstanding the assingment. I added an edit of the assingment. How can I mess with "header" without having to keep re declaring it with the recursive properties of a BST? – user7554736 Mar 07 '17 at 07:54
  • I assume the "NOTE" part is copied from the assignment: It doesn't state that you aren't allowed to modify `head` only that it needs to be a global variable (with initial value `NULL`, so `head = new Node();` in `main` is allowed) – UnholySheep Mar 07 '17 at 07:56
  • but if I add head = new Node to my code, then it no longer stays global right? edit: oh. – user7554736 Mar 07 '17 at 07:57
  • @user7554736 What? Of course it stays global, why shouldn't it? – UnholySheep Mar 07 '17 at 07:57
  • It was the parenthesis. I don't know enough about this stuff to know why that was a problem, but it needed parenthesis – user7554736 Mar 07 '17 at 07:58
  • It rather seems like your assignment is to implement the functions that access `head` in another C file, where `head` is allocated, and not from main(). main() can never be a "backend"... – Lundin Mar 07 '17 at 07:59
  • The difference between `new` with and without parentheses (after the type name) is discussed in this question: http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new – UnholySheep Mar 07 '17 at 08:15
  • Nevermind. Ignore this – user7554736 Mar 07 '17 at 08:39

1 Answers1

0

Head is just a pointer, pointing to NULL. There is no real object / memory allocated for Node. You have first to allocate memory for it.

In your assignment, you can and should (as far as i understand it) add nodes to linked list, so you will have to allocate new nodes .

    struct Node {
    char key;
    Node *next;
};

Node *head = NULL;

int main() {
    char x = 'a';
    head = new Node();
    cout << x << endl;
    head->key = x;
    delete head;
    return 0;
}
  • The question is tagged C++ so don't use `malloc` - also if it were C then you shouldn't cast the return of `malloc` (and in C++ you should prefer using the C++ style casts) – UnholySheep Mar 07 '17 at 08:10