Nakib
Nakib

Reputation: 4703

How to convert list class to template so that it work with any data?

How to convert list class to templated linked class so that it work with any data. Below is a class that works with integers. I wanna convert it to templated class so that it works with any data type

#ifndef _INTLIST_H_
#define _INTLIST_H_
#include <iostream>
class IntListNode
{
    protected:
        int value;
    public:
        IntListNode* nxt;
        IntListNode(int = 0, IntListNode* = NULL);
        IntListNode(const IntListNode&);
        IntListNode& operator=(const IntListNode&);
        int val() const;
        void val(int);
        IntListNode* next() const;
};

class IntList
{
    protected:
        IntListNode* nhead;
        IntListNode* tail;
        int csize;
    public:
        IntList();
        IntList(const IntList&);
        IntList& operator=(const IntList&);
        int size() const;
        IntListNode* head() const;
        void push(int);
        void pop();
        void clear();
        ~IntList();
};

#endif

This is what i have done

#ifndef _INTLIST_H_
#define _INTLIST_H_
#include <iostream>

template <class T>
class IntListNode
{
    protected:
        T value;
    public:
        IntListNode* nxt;
        IntListNode(int = 0, IntListNode* = NULL);
        IntListNode(const IntListNode&);
        IntListNode& operator=(const IntListNode&);
        T val() const;
        void val(T);
        IntListNode* next() const;
};


template <class T>
class IntList
{
    protected:
        IntListNode<T> nhead;
        IntListNode<T>tail;
        int csize;
    public:
        IntList();
        IntList(const IntList&);
        IntList& operator=(const IntList&);
        int size() const;
        IntListNode* head() const;
        void push(T);
        void pop();
        void clear();
        ~IntList();
};

#endif

Upvotes: 0

Views: 184

Answers (1)

R Sahu
R Sahu

Reputation: 206567

  1. Change IntLisNode to ListNode and make it a class template.
  2. Change IntLis to List and make it a class template. Use ListNode<T> in List instead of IntListNode.
  3. Replace use of int by T in some places and by T const& in the function signatures.

Here's a quick makeover.

#ifndef _LIST_H_
#define _LIST_H_
#include <iostream>

template <typename T>
class ListNode
{
    protected:
        T value;
    public:
        ListNode* nxt;
        ListNode(T const& in, ListNode* = NULL);
        ListNode(const ListNode&);
        ListNode& operator=(const ListNode&);
        T const& val() const;
        void val(T const&);
        ListNode* next() const;
};

template <typename T>
class List
{
    protected:
        ListNode<T>* nhead;
        ListNode<T>* tail;
        int csize;
    public:
        List();
        List(const List&);
        List& operator=(const List&);
        int size() const;
        ListNode<T>* head() const;
        void push(T const& val);
        void pop();
        void clear();
        ~List();
};

Upvotes: 1

Related Questions