Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

c++ - templated class List<T> for singly linked list implementation

I have a header file ListTraits.h, with the following content:

#pragma once

//------------ Declarations for List traits used in Test1 in main.cpp

template <typename T>
class ListTraits
{
public:
    virtual unsigned int size() = 0;
    virtual ListTraits& insert(const T& item) = 0;
    virtual void print() = 0;
};

//------------ Declarations for List traits used in Test2 in main.cpp
template <typename T>
class ListTraitsExtended
{
public:
    virtual const T* getCurrentElement() const = 0;
    virtual void advance() = 0;
    virtual void rewind() = 0;
};

I am trying to implement a templated class List<T>, that models a singly linked list and implements its functionality, and I could use all the help you could give me.

The templated class List<T> will inherit the methods from the ListTraits.h


Then I must include "ListTraits.h in a main.cpp file and run this code,

that makes calls to the implementation of list in List<T>

Here is the main.cpp::

#include <iostream>
#include <cstdio>

#include "include/List.h"
#include <random>

void Test1()
{
    std::cout << "---------------------------- TEST 1 -------------------------------
";
    std::cout << "Create a simple ordered list of specific items.
";
    List<int> list;
    list.insert(1);
    list.insert(4);
    list.insert(2);
    list.insert(6);
    list.insert(3);
    list.insert(0);
    list.insert(2);
    std::cout << "List size should be 7: " << (list.size() == 7 ? "PASS" : "FAIL") << ".
";
    std::cout << "Test should print: 0 1 2 2 3 4 6
";
    std::cout << "Output:            ";
    list.print();

}

int main(int argc, char ** argv)
{
    if (argc < 2)
    {
        exit(-1);
    }
    int seed = 5;
    
    Test1();
    //Test2(seed);
    //Test3(seed);

    getchar();
}

I am not even sure how to get started with this, so I would be really grateful, and thankful, for any advice.

question from:https://stackoverflow.com/questions/65926542/templated-class-listt-for-singly-linked-list-implementation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...