I'm asked in my study to make structure Queue implemented as 2-way Linked List with methods that allow to remove elements from both ends. I'm also asked to make sracture stack that inheritage from Queue (e.g stack is derived class) that support method of push and pop such that push&pop will use the father method to insert\take out from one of the ends.
I also created a node class for generate the linked list. also, I'm not allowed to change the "main". I'm asked to implement it twice (two seperate programs, two seperate main functions): one for double type and one generic type (tamplate). the "double" program works fine, but the generic program encountering a lot of errors that I dont really no what it means, something with global namespace. also, i've checked for missing syntax like semicolon/ barracks and such and it seems to be fine.
I attach both of the programs and the errors of the generic program at the end of the post:
the "double" program (works fine):
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class dNode
{
private:
dNode* prev_value;
dNode* next_value;
double data_value;
friend class ddutor;
friend class dstack;
public:
//default constructor
dNode(double val);
~dNode();
};
dNode::dNode(double val)
{
next_value=NULL;
prev_value=NULL;
data_value=val;
}
dNode::~dNode()
{
data_value=-1;
}
/*************************************************************************************************************************************/
class ddutor
{
protected:
dNode* current;
dNode* head;
dNode* tail;
int size;
public:
//default constructor
ddutor(double val);
//destructor
~ddutor();
//members functions
void add_first(double data);
void add_last(double data);
double remove_first();
void remove_last();
};
ddutor::ddutor(double val)
{
head=new dNode(val);
tail = new dNode(-1);
tail=head;
head->next_value=tail;
head->prev_value=NULL;
size=1;
}
void ddutor::add_first(double data)
{
current = new dNode(data);
head->prev_value = current;
current->next_value = head;
head = current;
head->prev_value= NULL;
size++;
}
void ddutor::add_last(double data)
{
current = new dNode(data);
tail->next_value = current;
current->prev_value= tail;
tail = current;
tail->next_value = NULL;
size++;
}
double ddutor::remove_first()
{
double temp=head->data_value;
if (head->next_value == NULL)
head = NULL;
else
head = head->next_value;
size--;
return temp;
}
void ddutor::remove_last()
{
if (tail->prev_value == NULL)
tail = NULL;
else
tail = tail->prev_value;
size--;
}
ddutor::~ddutor()
{
delete current;
delete head;
delete tail;
size=-1;
}
/******************************************************************************************************************************************/
class dstack:public ddutor
{
public:
void push(double d);
double pop();
dstack(double d);
};
dstack::dstack(double d):ddutor(d){};
void dstack::push(double d)
{
add_last(d);
}
double dstack::pop()
{
double temp=tail->data_value;
remove_last();
return temp;
}
/*******************************************************************************************************************************/
int main()
{
int i;
ddutor dd(1.1);
dstack ds(11.1);
for(i=2; i < 14; i += 2)
{
dd.add_first((double)i*1.1);
dd.add_last((double)((i+1)*1.1));
} //for
cout<<"dd print:\n";
for(i=1; i < 14; i++)
cout << " " << dd.remove_first();
cout << endl;
for(i=2; i < 14; i ++)
ds.push((double)i*11.1);
cout << "ds print:\n";
for(i=1; i < 14; i++)
cout << " " << ds.pop();
cout << endl;
cin>>i;
} // main
the "generic program (lot of errors)
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class ELEMENT_TYPE>
class gNode
{
private:
gNode* prev_value;
gNode* next_value;
ELEMENT_TYPE data_value;
friend class tdutor;
friend class tstack;
public:
//default constructor
gNode(ELEMENT_TYPE val);
~gNode();
};
template <class ELEMENT_TYPE>
gNode<ELEMENT_TYPE>::gNode(ELEMENT_TYPE val)
{
next_value=NULL;
prev_value=NULL;
data_value=val;
}
template <class ELEMENT_TYPE>
gNode<ELEMENT_TYPE>::~gNode()
{
data_value=-1;
}
//***************************************************************************************************************************//
template <class ELEMENT_TYPE>
class tdutor
{
protected:
gNode* current;
gNode* head;
gNode* tail;
int size;
public:
//default constructor
tdutor(ELEMENT_TYPE val);
//destructor
~tdutor();
//members functions
void add_first(ELEMENT_TYPE data);
void add_last(ELEMENT_TYPE data);
ELEMENT_TYPE remove_first();
void remove_last();
};
template <class ELEMENT_TYPE>
tdutor<ELEMENT_TYPE>::tdutor(ELEMENT_TYPE val)
{
head=new gNode(val);
tail = new gNode(-1);
tail=head;
head->next_value=tail;
head->prev_value=NULL;
size=1;
}
template <class ELEMENT_TYPE>
void tdutor<ELEMENT_TYPE>::add_first(ELEMENT_TYPE data)
{
current = new gNode(data);
head->prev_value = current;
current->next_value = head;
head = current;
head->prev_value= NULL;
size++;
}
template <class ELEMENT_TYPE>
void tdutor<ELEMENT_TYPE>::add_last(ELEMENT_TYPE data)
{
current = new gNode(data);
tail->next_value = current;
current->prev_value= tail;
tail = current;
tail->next_value = NULL;
size++;
}
template <class ELEMENT_TYPE>
ELEMENT_TYPE tdutor<ELEMENT_TYPE>::remove_first()
{
ELEMENT_TYPE temp=head->data_value;
if (head->next_value == NULL)
head = NULL;
else
head = head->next_value;
size--;
return temp;
}
template <class ELEMENT_TYPE>
void tdutor<ELEMENT_TYPE>::remove_last()
{
if (tail->prev_value == NULL)
tail = NULL;
else
tail = tail->prev_value;
size--;
}
template <class ELEMENT_TYPE>
tdutor<ELEMENT_TYPE>::~tdutor()
{
delete current;
delete head;
delete tail;
size=-1;
}
/*******************************************************************************************************************************/
template <class ELEMENT_TYPE>
class tstack:public tdutor
{
public:
void push(ELEMENT_TYPE d);
ELEMENT_TYPE pop();
tstack(ELEMENT_TYPE d);
};
template <class ELEMENT_TYPE>
tstack<ELEMENT_TYPE>::tstack(ELEMENT_TYPE d):tdutor(d){};
template <class ELEMENT_TYPE>
void tstack<ELEMENT_TYPE>::push(ELEMENT_TYPE d)
{
add_last(d);
}
template <class ELEMENT_TYPE>
ELEMENT_TYPE tstack<ELEMENT_TYPE>::pop()
{
ELEMENT_TYPE temp=tail->data_value;
remove_last();
return temp;
}
int main()
{
int i;
tdutor<double> dd(1.1);
tstack<double> ds(11.1);
for(i=2; i < 14; i += 2)
{
dd.add_first((double)i*1.1);
dd.add_last((double)((i+1)*1.1));
} //for
cout << "dd print:\n";
for(i=1; i < 14; i++)
cout << " " << dd.remove_first();
cout << endl;
for(i=2; i < 14; i ++)
ds.push((double)i*11.1);
cout << "ds print:\n";
for(i=1; i < 14; i++)
cout << " " << ds.pop();
cout << endl;
cin>>i;
} // main
errors of the generic:
Error 23 error C1903: unable to recover from previous error(s); stopping compilation
Error 5 error C2039: 'add_first' : is not a member of '`global namespace''
Error 12 error C2039: 'add_last' : is not a member of '`global namespace''
Error 13 error C2039: 'remove_first' : is not a member of '`global namespace''
Error 21 error C2039: 'remove_last' : is not a member of '`global namespace''
Error 4 error C2059: syntax error : '<'
Error 11 error C2059: syntax error : '<'
Error 20 error C2059: syntax error : '<'
Error 18 error C2086: 'int tdutor' : redefinition
Error 6 error C2143: syntax error : missing ';' before '{'
Error 14 error C2143: syntax error : missing ';' before '{'
Error 8 error C2143: syntax error : missing ';' before '<'
Error 16 error C2143: syntax error : missing ';' before '<'
Error 9 error C2182: 'tdutor' : illegal use of type 'void'
Error 17 error C2182: 'tdutor' : illegal use of type 'void'
Error 7 error C2447: '{' : missing function header (old-style formal list?)
Error 15 error C2447: '{' : missing function header (old-style formal list?)
Error 22 error C2588: '::~tdutor' : illegal global destructor
Error 3 error C2988: unrecognizable template declaration/definition
Error 10 error C2988: unrecognizable template declaration/definition
Error 19 error C2988: unrecognizable template declaration/definition
Error 1 error C2989: 'tdutor' : class template has already been declared as a non-class template
Error 2 error C3857: 'tdutor': multiple template parameter lists are not allowed