I'm trying to write a program that will use a generic linked list.
I created the linked list.I need that it will hold once an int and once a pointer to a list of ints.
this is what I've done so far:
double_linked.h
#ifndef _DOUBLE_LINKED_ #define _DOUBLE_LINKED_ #include <iostream> using namespace std; template <typename T> class double_linked { struct node { T data; node* prev; node* next; node(T t, node* p, node* n) : data(t), prev(p), next(n) {} }; node* head; node* tail; public: double_linked() : head( NULL ), tail ( NULL ) {} template<int N> double_linked( T (&arr) [N]) : head( NULL ), tail ( NULL ) { for( int i(0); i != N; ++i) { push_back(arr[i]); } } bool empty() const { return ( !head  !tail ); } operator bool() const { return !empty(); } void push_back(T&); void push_front(T); T pop_back(); T pop_front(); void const print(); ~double_linked() { while(head) { node* temp(head); head=head>next; delete temp; } } }; template <typename T> void double_linked<T>::push_back(T &data) { tail = new node(data, tail, NULL); if( tail>prev ) tail>prev>next = tail; if( empty() ) head = tail; } template <typename T> void double_linked<T>::push_front(T data) { head = new node(data, NULL, head); if( head>next ) head>next>prev = head; if( empty() ) tail = head; } template<typename T> T double_linked<T>::pop_back() { if( empty() ) throw("double_linked : list empty"); node* temp(tail); T data( tail>data ); tail = tail>prev ; if( tail ) tail>next = NULL; else head = NULL ; delete temp; return data; } template<typename T> T double_linked<T>::pop_front() { if( empty() ) throw("double_linked : list empty"); node* temp(head); T data( head>data ); head = head>next ; if( head ) head>prev = NULL; else tail = NULL; delete temp; return data; } template<typename T> void const double_linked<T>::print() { node* curr2(head); // cout<<"size:"<<getQueueSize()<<endl; while(curr2!=NULL) { cout<<" "<<curr2>data<<" "; curr2=curr2>next; } cout<<endl; } #endif
try.h
#ifndef _TRY_ #define _TRY_ #include <iostream> #include "double_linked.h" using namespace std; class tryMe { int value; double_linked<int> *tryList; double_linked<double_linked<int>*> *_plist; public : tryMe(int array[], int size); ~tryMe(); void const print(); }; #endif
try.cpp
#include <iostream> #include "double_linked.h" #include "try.h" using namespace std; tryMe::tryMe(int array[], int size) { int i; double_linked<int> *tryList; double_linked<double_linked<int>*> *_plist; for (i=0 ; i < size; ++i) { tryList.push_back(&array[i]); cout<<array[i]<<" "; } _plist.push_front(tryList); }; void const print() { tryList *tryListM = new tryList; }
main.cpp
#include <iostream> #include "double_linked.h" #include "try.h" using namespace std; int main() { int arr[] = { 4, 6, 8, 32, 19 } ; double_linked<int> dlist ( arr ); while( dlist ) std::cout << dlist.pop_back() << " "; tryMe *tryMe1 = new tryMe(arr , 5); return 0 ; }
code blocks sends me an error that :
In constructor ‘tryMe::tryMe(int*, int)’:
error: request for member ‘push_back’ in ‘tryList’, which is of nonclass type ‘double_linked<int>*’
error: request for member ‘push_front’ in ‘_plist’, which is of nonclass type ‘double_linked<double_linked<int>*>*’
I'm probably getting the pointers all wrong.
Can anyone help me to understand what I have done wrong?
Thank you. (I'm sorry for my bad English).
Attached File(s)

assTry.zip (1.95K)
Number of downloads: 21