Using a double linked list in a different class

Format:https://www.geeksforgeeks.org/doubly-linked-list/

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.

Here is my node class
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
#pragma once
#include <string>
#include <iostream>
using namespace std;
class node
{
public:
	node();
	~node();

	class Node
	{
	
	public:
		string name;	
		string fLevel;
		int days;
		Node* next;
		Node* prev;
	};  Node* head = NULL;
	void push(Node** head_ref, string newName,string newFlevel, int newDays);
	void insertAfter(Node* prev_node, string newName, string newFlevel, int newDays);
	void append(Node** head_ref, string newName, string newFlevel, int newDays);
	void printList(Node* node);
};


Here is the body starting with the header
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once
#include "node.h"

class body
{
public:
	
	body();
	~body();
	void askUser();
	void insert(string s, string n, int p);
};


here is the cpp for the body
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
#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
Last edited on
head is a member of the class node

You're trying to use it in a member function of the class body.

You can't use member variable from other classes. body can only see the member variables of body.

ob.push(&ob.head,s, n, p); might make more sense, although even then not much.
Last edited on
oh, I see. Should I make body a friend class of node?
No.

What's the body class for?

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).
if what I'm doing is totally wrong, just let me know. I will just do everything in one class.
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.

Last edited on
Thank you very much for your help. I appreciate it.
Topic archived. No new replies allowed.