Hello,
im trying to implement a generic double linked list, and i have added just one function(insert).
But im getting some errors. Here is the code:
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
template <typename T>
class doubleList {
private:
struct Node {
T info;
Node *leftPtr;
Node *rightPtr;
};
// head and tail of the list.
static Node* head;
static Node* tail;
Node* getNode();
public:
void addNode(T var);
void traverseList();
};
template<typename T>
Node* doubleList<T> :: getNode()
{
Node* ptr = new Node();
return ptr;
}
template<typename T>
void doubleList<T> :: addNode(T var)
{
Node *temp = getNode();
if (head == NULL) {
// First element.
temp->info = var;
head = temp;
tail = temp;
temp->leftPtr = temp->rightPtr = NULL;
} else {
temp->info = var;
temp->rightPtr = NULL;
temp->leftPtr = head;
head = temp;
}
}
int main()
{
string s[5] = {"hello", "world", "bye", "wirld", "hey!"};
int a[5] = {1, 2, 3, 4, 5};
doubleList<string> ob1;
doubleList<int> ob2;
// add all elements into the linked list.
for (int i = 0; i < 5; i++) {
ob1.addNode(s[i]);
ob2.addNode(a[i]);
}
getchar();
return 0;
}
Can anyone please help?
Thanks.