Reputation: 105
I'm trying to write a linked list of Node
objects, where each Node
contains: a string - data
, and a pointer - next
. To manage the list (e.g: add/remove nodes), I have Node objects: head
, curr
and temp
. The code compiles fine, but the result is not what I want.
What I'm having trouble with is making the next
pointer in the curr
node actually point to the next node. I've been adding output statements to narrow down the error and it seems to be coming from the line: curr.setNext(n);
in the addNode()
function.
LinkedList.cpp:
#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
#include "Node.h"
using namespace std;
LinkedList::LinkedList() {
}
void LinkedList::addNode(value_type addData) {
Node n;
n.setData(addData);
if (head.getData() != "") {
curr = head;
while(curr.getNext() != NULL) {
curr = *(curr.getNext());
}
curr.setNext(n); //This line is not linking the nodes.
cout << "(2) Curr is pointing to: " << curr.getNext() << " , The location of n is:" << &n << endl;
}
else {
head = n;
}
}
LinkedList.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
class LinkedList {
public:
typedef std::string value_type;
LinkedList();
LinkedList(value_type setData);
void addNode(value_type addData);
private:
Node head;
Node curr;
Node temp;
};
#endif
Node.cpp:
#include <iostream>
#include <cstdlib>
#include "Node.h"
using namespace std;
Node::Node() {
data = "";
next = NULL;
}
void Node::setData(value_type setData) {
data = setData;
}
string Node::getData() {
return data;
}
void Node::setNext(Node n) {
next = &n;
cout << "(1) Curr is pointing to: " << next << " , The location of n is:" << &n << endl;
}
Node * Node::getNext() {
return next;
}
Node.h:
#ifndef NODE_H
#define NODE_H
class Node {
private:
typedef std::string value_type;
value_type data;
Node* next;
Node* prev;
public:
Node();
void setData(value_type setData);
value_type getData();
void setNext(Node n);
Node * getNext();
};
#endif
So when I call the function a few times with test strings and a blank list, the cout
statements should be printing out the same memory address in both instances. However, this is the output:
(1) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c9a0
(2) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c960
(1) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c9a0
(2) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c960
(1) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c9a0
(2) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c960
So in the Node
class, the memory addresses line up correctly. But as soon as it leaves that class and prints again, they are different. Does it have something to do with next
and curr.getNext()
being stored differently? How would I go about fixing this?
Thanks in advance!
Upvotes: 0
Views: 73
Reputation: 206567
The following function is not right:
void Node::setNext(Node n) {
next = &n;
cout << "(1) Curr is pointing to: " << next << " , The location of n is:" << &n << endl;
}
You are storing the address of an object that is on the stack. The pointer becomes invalid as soon as the function returns.
PS There may be other problems in your code.
Upvotes: 0
Reputation: 14688
You are making a copy of the nodes, rather than operating on the linked list;
This line copies the node
curr = *(curr.getNext());
Try instead to change Node curr;
to Node *curr;
and then operate on pointers instead of copies, like
curr = curr->getNext()
and
curr->setNext(n);
You also need to make proper memory management, so code like this will not work as the Node n
is a local variable will not survived beyound the scope of the function
Node n;
n.setData(addData);
Upvotes: 1