Linked list node

Hi guys, I was just wondering why I kept getting the error message:
Error: identifier "curr1" is undefined.

my header:
1
2
3
4
5
6
7
8
9
10
11
12
13
class llist{
private:
	struct Node{
		Node* next;
		int data;
	};
	Node* head;
public:
	llist(){ head = NULL; }
	void addNode(int);
	void delNode(int);
	void iterate();
};


my llist.cpp file
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
#include "llist.h"
#include <iostream>

using namespace std;

void llist::iterate(){
	Node* curr;
	curr = head;
	if(head != NULL){
		curr = head;
		while(curr!=NULL){
			cout << curr->data;
			curr = curr->next;
		}
	}
	else 
		cout << "\n no llist \n";
}

void llist::addNode(int x){
	Node* node;
	node->next = NULL;
	node->data = x;
	if(head == NULL)
		node = head;
	else 
		Node* curr1;
		curr1 = head;
		while(curr1->next != NULL){
			curr1 = curr1->next;
		}
		node = curr1->next;
}

I am getting the error in addNode where I initialize curr1 to head
I have my #include "llist.h"
so I'm not sure why exactly it is saying it is unidentified

Thanks for the help!
Last edited on
closed account (1vRz3TCk)
Hint: Have a look at the else side in addNode()
Oh I see, I needed the braces, else curr1 would be declared within the scope of else, therefore not being seen by the rest of the program?
Now I am getting an error saying that in line 22,
I am using the variable node without first initializing it.
I thought that was what I did at line 21 with Node* node;?
closed account (1vRz3TCk)
You need to be doing something along the lines of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void llist::addNode(int x){
    Node* node = new Node;
    node->next = NULL;
    node->data = x;
    if(head == NULL)
        head->next = node;
    else 
    {
        Node* curr1;
        curr1 = head;
        while(curr1->next != NULL)
        {
            curr1 = curr1->next;
        }
        curr1->next = node;
    }
}
NB: I'm very tired and off to bed as I have to get up in six hours for work, hope it helps.
Last edited on
Oh ok well thank you for the help I think I can get it from here.
closed account (1vRz3TCk)
You will need to look at deleting the nodes in the list when you have finished with them.
Topic archived. No new replies allowed.