I am trying to get more comfortable with data structures, so I'm trying out double linked lists. I have a class for the nodes and a class for what I write. I am doing this because I feel like it makes everything cleaner.
#include "body.h"
body::body()
{
}
body::~body()
{
}
void body::askUser()
{
string name;
string current;
int days;
cout << "please enter name,current friendship level,and how many momre days";
cin >> name;
cin >> current;
cin >> days;
insert(name, current, days);
}
void body::insert(string s, string n, int p)
{
node ob;
ob.push(&head,s, n, p);//this is where i get the error
}
I get an error saying "identifier head is undefined". I tried defining it in my node.h's constructor, but that didn't work. I'm not sure how to fix this error
Why does the node class have a push function? Are you pushing something to the node? What's the thinking behind it? Surely you should create a node object, put your data in it, and then add that node to a linked list
I was told to make my code as clean as possible, so I thought by making 2 separate classes (body and node), it would make the overall writing much cleaner. I also thought it might be useful for the node to have its own class, in case I wanted to use it, in the future, in a different class.
The node has a push because I'm using the format given by geeks for geeks(gave credit on the top).
In which case, each node would logically contain a body, and a pointer to the next node, and a pointer to the previous node, would it not?
1 2 3 4 5 6
class node
{
node* nextNode = nullptr; // nullptr since C++11. Don't use NULL.
node* previousNode = nullptr;
body the_actual_data;
}
You need to now create a node, put the data in it, and that first node it then your entire linked list. It has no next node, no previous node. Never lose the first node. The first node is the head of the list.
Then, create another node. Put the data in it. Make the first node's nextNode pointer point at the second node. Make the second node's previousNode pointer point at the first node. Now you have a linked list.
Linked list is a very very simple data structure. You do yourself a disservice by trying to copy someone else's implementation. You can do this yourself by thinking and sketching on paper.