I want to do a double linked list that has has in each element one operator(+-*/) and one value of type double. I am having trouble in defining the class of the list. Should I use C++ Class Template Specialization or what?

include<iostream>

using namespace std;

template <typename T>
class dblelist<T,double>
{
    private: 
    
    struct node
    
    {
        double wert;
        T zeichen; 
        node *prev;
        node *nxt; 
        
    };

How do I write the node constructor above??

Thank you heaps!!!!

Recommended Answers

All 10 Replies

Why do you need a template at all? Can't your second member be a char to hold the operator? (or do you have more than '+','-', etc?

The user will enter several numbers with several operators and the calculation is to be done. Each member element of the list will be a number and operator. Can I do it without templates? I checked the internet and saw how to make double linked lists, and it contained templates.
Thank you for ur reply

Will each operator be a single character? If so then you don't need the template. A templated list is handy for use with a bunch of different data types (say you wanted a node to hold either a double or an integer) but it just complicates your task right now.

struct node
{
    double number;
    char op;
    node *prev;
    node *nxt; 
};

Although it was preferred that the list should be implemented in a class, can it be done without being in a class alone.
I know theoretically the difference between single linked lists and double linked lists; however I mostly find codes for single linked lists. What is the difference in the programming language?

You can still wrap a class around for your list and have the struct be a member of the class.

A doubly linked list means that you have the pointers for the next and the prior node. It makes traversing the list easier, as going in reverse in a singly linked list requires keeping track of where you've been. I'm not sure what you mean by "What is the difference in the programming language?"

The only code i found that could help me is here. Is it proper to be used in my case? Do I have to do a separate class for the list and for the node or can I define the node in the definition of the class?
Sorry for the question and thanks in advance!

This title is so inappropriate...

commented: the post itself is "inappropriate"...i was in a hurry and I can'y change it +0

The only code i found that could help me is here. Is it proper to be used in my case? Do I have to do a separate class for the list and for the node or can I define the node in the definition of the class?
Sorry for the question and thanks in advance!

Yes, that will work, but don't just copy their code you won't learn anything. Where you define the node and whether it is struct (all members public by default) or class (all members private by default) is not critical. The notation will be virtually identical.

This title is so inappropriate...

Yes that's true, but there's no way for the OP to go back and change it. I agree there would be lots of better choices.

commented: goood +1

Is it better to use the list container object or the way I am trying to use above?

You need to take a step back and look at your objectives. If this is for a course assignment which has the express purpose of teaching you how to write a doubly linked list, then no. Use that example you found as a guide and nothing more. If you are looking for an effective list implementation for another piece of code then definitely use the STL container.

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.