Reputation:
I have implemented a fairly simple idea of a double linked list. I don't know what am I doing wrong! I have tried making the member variables of the node as public but doesn't help. Friend class doesn't help either. What is the nonclass type here?
d_list.h
#include "node.h"
#ifndef NODE_H
#define NODE_H
template<class T>
class d_list{
private:
int list_size;
T* head;
T* tail;
public:
//parametrized Default constructor
d_list(T* h=nullptr, T* t=nullptr):head(h),tail(t){}
//get Head of the List
T* gethead(){return this->head;}
T* gettail(){return this->tail;}
void addnodeastail(T* new_node){
if(this->head==nullptr){
this->head=new_node;//this->head will point towards new_node
this->tail=this->head;
this->list_size=list_size+1;
}
else{
this->tail= new_node;
this->tail->next=new_node->previous;
}
}
};
#endif
'''
node.h
template<class T>
class Node{
private:
Node* next;
Node* previous;
T data;
public:
Node()=default;
Node(T dta):data(dta){}
~Node(){}
};
main.cpp
#include<iostream>
#include"d_list.h"
using namespace std;
int main(){
d_list<int> d1;
cout<<d1.gethead()<<endl;
cout<<d1.gettail()<<endl;
int var=20;
int* n1= &var;
int var2 =40;
int* n2= &var2;
d1.addnodeastail(n1);
d1.addnodeastail(n2);
cout<<d1.gethead()<<endl;
cout<<d1.gettail()<<endl;
return 0;
}
Error which I am receiving is something like
In file included from main.cpp:2:
d_list.h: In instantiation of 'void d_list<T>::addnodeastail(T*) [with T = int]':
main.cpp:14:24: required from here
d_list.h:28:29: error: request for member 'next' in '*((d_list<int>*)this)->d_list<int>::tail', which is of non-class type 'int'
28 | this->tail->next=new_node->previous;
| ~~~~~~~~~~~~^~~~
d_list.h:28:44: error: request for member 'previous' in '* new_node', which is of non-class type 'int'
28 | this->tail->next=new_node->previous;
| ~~~~~~~~~~^~~~~~~~
Upvotes: 1
Views: 720
Reputation:
The thing is that head
and tail
shouldn't be T
, but rather node<T>
:
Node<T>* head;
Node<T>* tail;
Remember, T
is the type of object you want to hold in your list. It has no link associated with it. So if you do a d_list<int>
currently as written, the templated code will look something like this:
int* head;
int* tail;
These are not linked to each other, this is just a pointer to an int
(or a list of int
s if you used something like new
). To create a link, you need to use Node
so o matter where in memory they are stored, they will have a logical connection. That way, d_list<int>
would look something like this instead:
Node<int>* head;
Node<int>* tail;
This will let you use nodes to develop a logically connected list of int
s, which is exactly what you want for a linked list.
Upvotes: 0
Reputation: 409482
With
template<class T>
class d_list{
private:
int list_size;
T* head;
T* tail;
you declare that head
and tail
are pointers to the template type T
.
That means for d_list<int>
you effectively have
int* head;
int* tail;
That makes no sense, your head and tail pointers should be pointers to the first and last nodes in the list:
Node<T>* head;
Node<T>* tail;
And when adding elements to the list, you need to create new Node<T>
instances to hold the data, and add the nodes to the list.
Upvotes: 1