template class

I have a doubly linked list and I insert different data items into the nodes. All works fine! The structure is identical for different category items. Is it possible to use this same template and structure to create a separate doubly linked list for a different category item? For example; one list for drinks a separate list for snacks using same template class. I have almost 12 category’s and don’t really want to repeat all of the code 12 times.

template <class T>
class doublylinkedlist {
private :
struct node
{
int itemNum; // *** Data items for Node
char itemName[30];
char itemContainer[30];
int itemCount;
char itemLocation[30];
> I have a doubly linked list and I insert different data items into the nodes.

Why aren't you using std::list<>? http://en.cppreference.com/w/cpp/container/list


> Is it possible to use this same template and structure to create
> a separate doubly linked list for a different category item?

If 'different category' means different type (eg. drink and snack are two different classes), yes.
Each time the list template is instantiated with a different type, we get a different list.
First; thank you for such a quick response.
The category items are of the same type so the list template is not instantiated with a different type. Although; I was aware that I could create a different list if using "int" or "float" etc. and I know how to do that.

I am self taught and the books I have been using have not entered into the usage of std::list, only mentioned that it is part of the library functionality. Yet, all the books have gone into to broad examples of how to create a db linked list using templates. I guess if it is not feasible to use the template class in this manner, maybe I should continue studying to learn how to use the built-in
std::list functionality. Thank you for your suggestion and the link to the information.

I could use a brute force technique and copy the code into 12 different named template class's, but I'm sure that constitutes a poor programming technique and would add a lot of bulk to the program. I guess before I continue I will study the information in the link you attached; once again, Thank you.
> I am self taught and the books I have been using have not entered into the usage of std::list,
> maybe I should continue studying to learn how to use the built-in std::list functionality.

May I suggest that you should continue studying with a different book?
For instance, this one: http://www.amazon.com/Accelerated-C-Practical-Programming-Example/dp/020170353X


> I could use a brute force technique and copy the code into 12 different named template class's

You can create twelve different instances of the same class; one for each category. For instance,

1
2
3
4
5
doublylinkedlist<item> drinks_list ; // put only drinks into this

doublylinkedlist<item> snacks_list ; // put only snacks into this

// etc... 
Last edited on
Oh! Wow! A light bulb went off in my head when I seen those two statements. I am embarrassed to say I have been studying and trying to figure this out for two weeks now. It never dawned on me to simply modify my driver. Thank you so very much for your help and I am going to follow your suggestions. I try real hard to research and figure things out on my own but my old tired brain don’t work so good anymore and that is why I continue to try and push my mind with the challenges presented by programming. Thanks again.
reply for Tom B
I hope I do this correctly, never posted code so here are some snippets.
I actually got that part of the code in my program working. Below is partial code starting with the layout for the template class then in the driver using a switch; I make function calls to the individual category. Once inside the individual category, I then make the distinction for the template class example: doublylinkedlist&ltint&gt dlist;
for the drinks list.

in dllist.h file
using namespace std;
<pre>
template&lt;class T&gt;
class doublylinkedlist {
private :
struct node
{
int itemNum;
char itemName[30]; // Data items for Node **
char itemContainer[30];// **
int itemCount; // **
char itemLocation[30]; // *********************

T data;
node *prev;
node *next;
friend ostream& operator<< (ostream&, const node &cnode);
};
node *head;
node *tail;
node *temp;
<pre>
In driver :
<pre>
#include "dllist.h"

using namespace std;

void instructions();
void drinks_list();
void fruits_list();
etc.

switch (selection) {
case 1:
cout << "Entering items into Drinks Category." << endl;
drinks_list();
break;

case 2:
cout << "Entering items into Fruits & Spreads Category." << endl;
fruits_list();
break;

etc.


void drinks_list()
{
int choice; // for switch
int value, value2;
int z;

doublylinkedlist&lt;int&gt; dlist; // This is the Drinks List
in fruits category this would be: doublylinkedlist&ltint&gt; fruitslist
<pre>
Topic archived. No new replies allowed.