Linked list with templates

can someone please help me with this program?Im so confused i dont even know where to start. My program is a bit complicated.Im suppose to create two classes.The first class is called Auto with the following
ChassisNum: int
Make: string
Model: string
Type: string
NextAuto: Auto


In the next class I'm supposed to name it ListOfAutos.This class should be used to create a linked list object.The list node should store multiple data items. The nodes in the
linked list should be of type Auto.it should have addAuto to insert at the front of a link list ,Display which should display all the nodes in the list and
CountList which should return the number of nodes in the list.


This is what is in my auto.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef AUTO_H
#define AUTO_H
#include <iostream>

#include<string.h>
using namespace std;

    template< typename Auto > class ListOfAutos;

    template< typename Auto>
    class Autos
    {
       friend class ListOfAutos< Auto >; // make List a friend
    public:
       Autos( const Auto & ); // constructor
       int chassisNum;
       string model;
       string make;
       string type;
       
       Auto getData(int chassisNum) const; 
       Auto getData(string model)const;
       Auto getData(string make)const;
       Auto getData(string type)const;
       
    private:
       Auto data; // data
       Autos< Auto > *nextPtr; // next node in list
    }; // end class ListNode



#endif // AUTO_H 




im not sure if im implementing these right
Auto getData(int chassisNum) const;
Auto getData(string model)const;
Auto getData(string make)const;
Auto getData(string type)const;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
this was my auto.cpp
#include<iostream>
#include<auto.h>
    template< typename NODETYPE>
    Autos< NODETYPE >::Autos( const NODETYPE &info )
       : data( info ), nextPtr( 0 )
    {
       // empty body
    } // end ListNode constructor

        template< typename Auto >
        Auto Autos< Auto>::getData() const
        {
           return data;
        } // end function getData




i haven't gotten the listofautos class as yet either but I want to make sure that I get the auto class first.
Last edited on
Autos shouldn't be a template. It's as you say, an Auto-mobile class that has stuff about a car.

A linked list of Autos is similar to a linked list of int, which is similar to a linked list of Marbles. The similarity is how you treat the list. You add, remove, traverse ... and irrespective of the thing contained, the algorithm is the same. So the idea is you implement the list on some generic label, then use that to create your list of Autos.

So the definitions look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Autos
{
public:
    Autos(
        int chassisNum,
        std::string model,
        std::string make,
        std::string type);
    int chassisNum() const;
    std::string model() const;
    std::string make() const;
    std::string type() const;

private:
    int chassisNum_;
    std::string model_;
    std::string make_;
    std::string type_;
};

template <typename T>
class LinkedList
{
public:
    LinkedList();
    ~LinkedList();
    void clear();
    void insert(T value);
    // ... and other methods

private:
    struct Node {
        Node<T>* next; 
        T data;
    };
    Node<T>* head_;
};


I leave the rest to you.
Hi,

Just a mention if you are doing templates, all the template code (class definition and implementation) should go into 1 header file, the compiler needs to see all of it at once. So this is different to the normal method of splitting classes into *.cpp and *.hpp files.

The compiler should have complained big time with the 2 files in your OP.
You'll find things easier when you come to code the list if you have:

1
2
3
4
5
6
struct Node {
    Node<T>* next {};
    T data {};

    Node(const T& t) : data(t) {}
};

Topic archived. No new replies allowed.