ravi_14 0 Light Poster

Bold Text HereI am trying to make a generic linked list .
below is the code for linkedlist .

#ifndef GenericLinkedList_h
#define GenericLinkedList_h

#include <iostream>
#include "GenericLinkNode.h"

//class GenericLinkedList;


template <typename T>
class GenericLinkedList
{
private :
    GenericLinkNode<T> *head;
public:
    GenericLinkedList();
    void traverse();
    bool isEmpty() { return head == NULL ;}
    void addAtFront(T itemToAdd);
    void removeAtFront();
};

//class method definitions
template <typename T>
GenericLinkedList<T>::GenericLinkedList()
        {
    head = NULL;
        }
template <typename T>
void GenericLinkedList<T>::traverse()
{
    if (isEmpty()) //return if the list is empty
        return ;
    GenericLinkNode<T>* currentNode = head;
    while(currentNode != NULL)
    {
        std::cout<<currentNode->key<<std::endl;
        currentNode = currentNode->next;
    }
}

template <typename T>
void GenericLinkedList<T>::addAtFront(T itemToAdd)
{
GenericLinkNode<T>* nodeToAdd = new GenericLinkNode<T>;
nodeToAdd->key = itemToAdd;
nodeToAdd->next = head ;
head = nodeToAdd ;
}


template <typename T>
void GenericLinkedList<T>::removeAtFront()
{
if(isEmpty())
    return;
GenericLinkNode<T> *oldNode;
oldNode = head ;
head = oldNode->next;
delete oldNode;
}



#endif

and code for linknode class in a different file

#ifndef GenericLinkNode_h
#define GenericLinkNode_h

template <typename T> class GenericLinkedList<T>;
template <typename T>
class GenericLinkNode
{    private:
    T key;
    GenericLinkNode<T>* next;
friend class GenericLinkedList<T>;
};

#endif

*** //i am struggling to make a forward declaration of generic linklist class .since i have to make it friend of linknode.***Emphasized Text Here******

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.