如何在C ++中使用模板制作链表 [英] How to make linked list using templates in C++

查看:100
本文介绍了如何在C ++中使用模板制作链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现单链表的模板类。



1.定义模板类包含两个数据成员的节点:A接下来是模板数据和节点指针。您可以为模板类定义必要的成员函数。



2.在MyList.h文件中定义模板类MyList。模板类包含两个数据成员:节点指针头和节点指针尾。指针头指向链表的第一个节点。指针尾指向链表的最后一个节点。最初链表是空的。为类MyList定义以下成员函数



1.Default构造函数。

2.Copy构造函数。

3.Destructor。

4.push_front函数将模板数据作为参数并在前面添加链接列表(如果它不是空的话,在第一个节点前面)。

5.push_back函数将模板数据作为参数并将其添加到链表的尾部(在最后一个节点,如果它不是空的。)

6.isEmpty函数如果链表是空的,则返回true。

7.front函数返回第一个节点中的数据一个链表。

8.back函数返回链表最后一个节点中的数据。

9.pop_front函数删除链表中的第一个节点。它不会返回任何数据。

10.pop_back函数删除链表中的最后一个节点。它不返回任何数据。

11.在类MyList中定义嵌套类迭代器,它包含数据成员:节点指针当前和节点指针最后。为迭代器类定义以下成员函数和重载运算符。

11.1默认构造函数。

11.2解除引用运算符*返回当前节点的数据。

11.3后增量运算符++将当前指针指向列表中的下一个节点。

11.4重载比较运算符not equals将迭代器与另一个迭代器进行比较。如果两个迭代器的当前指针不相同,则返回true。



12. begin函数返回一个迭代器,指针当前指向列表的第一个节点,指针最后指向列表的尾部。

13. end函数返回一个迭代器,指针当前设置为NULL,指针最后指向列表的尾部。

14. print函数接受std :: ostream引用参数并打印通过使用上面的迭代器定义存储在链表中的所有数据到ostream。



在MyList.h文件中实现模板类Node,MyList和iterator的成员函数







implement a template class of a singly linked list.

1.Define a template class Node that consist two data members: A template data and a Node pointer next. You may define the necessary member functions for the template class.

2.Define a template class MyList in the file MyList.h. The template class consist two data members: A Node pointer head and a Node pointer tail. The pointer head points to the first node of a linked list. The pointer tail points to the last node of the linked list. Initially the linked list is empty. Define the following member functions for the class MyList

1.Default constructor.
2.Copy constructor.
3.Destructor.
4.push_front function takes a template data as a parameter and adds it at the front of a linked list (in front of the first node if it is not empty).
5.push_back function takes a template data as a parameter and adds it at the tail of a linked list (after the last node if it is not empty).
6.isEmpty function returns true if a linked list is empty.
7.front function returns the data in the first node of a linked list.
8.back function returns the data in the last node of a linked list.
9.pop_front function removes the first node in a linked list. It does not return any data.
10.pop_back function removes the last node in a linked list. It does not return any data.
11.Define nested class iterator in the class MyList which consist data members: A Node pointer current and a Node pointer last. Define the following member functions and overloading operators for the iterator class.
11.1 Default constructor.
11.2 Dereference operator * returns the current node’s data.
11.3 Post increment operator ++ moves the current pointer points to the next node in the list.
11.4 Overloading comparison operator "not equals" compares the iterator with another iterator. Returns true if both iterators’ current pointers are not the same.

12. begin function returns an iterator with pointer current points to the first node of the list, and pointer last points to the tail of the list.
13. end function returns an iterator with pointer current set to NULL and pointer last points to the tail of the list.
14. print function takes a std::ostream reference parameter and prints all data stored in the linked list to the ostream by using the iterator define above.

Implement member functions for the template classes Node, MyList and iterator in the file MyList.h



template<class T> 
class List 
{
    public:
        //Class declarations.
        class Iterator;
        class ConstIterator;

        //Constructors and Destructors.
        List() : head(NULL), tail(NULL), size(0) {} //Constructor
        ~List(); //Destructor

        //Methods
        Iterator<T> begin();
        Iterator<T> end();
        void insert(const T& data);
        void insert(const T& data, const Iterator<T>& iterator);
        void remove(const Iterator<T>& iterator);
        int getSize() const;
        Iterator<T> find();
        void sort();

    private:
        class Node<T>;
        Node<T>* head;
        Node<T>* tail;
        int size;
};


template<class T> 
class List<class T>::Iterator 
{
    public:
        Iterator(); //Constructor
        ~Iterator(); //Destructor

        T& operator ++ ();
        T operator ++ (int);
        T& operator -- ();
        T operator -- (int);
        bool operator == (const Iterator<T>& iterator) const;
        bool operator != (const Iterator<T>& iterator) const;
        T& operator * ();

    private:
        List<T>* list;
        Node<T>* node;
};


template<class T>
class List<class T>::ConstIterator 
{
    public:
        ConstIterator(); //Constructor
        ~ConstIterator(); //Destructor

        T& operator ++ ();
        T operator ++ (int);
        T& operator -- ();
        T operator -- (int);
        bool operator == (const ConstIterator<T>& iterator) const;
        bool operator != (const ConstIterator<T>& iterator) const;
        T& operator * ();

    private:
        const List<T> * list;
        const Node<T> * node;
};


template<class T>
class List<class T>::Node
{
    public:
        Node(const T& _data, const Node* _next = NULL) : data(_data), next(_next) {} //Constructor
        ~Node(); //Destructor 

    private:
        T data;
        Node* next;
};





我尝试过:



我的50%代码。请帮我再做50%。



What I have tried:

Its my 50% code. kindly help me to make another 50%.

推荐答案

你应该做好功课来学习编码。它会增强你的知识和技能,也许你有一天会赚很多钱。



这是一个很好的教程,关于如何使用C / C ++创建链接列表以支持您的工作。



; - )
You should make your homework to learn coding. It will enhance your knowledge and skills and maybe you earn a lot of money someday.

Here is a fine tutorial about How to create Linked list using C/C++ to support your efforts.

;-)


这篇关于如何在C ++中使用模板制作链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆