linkedList remove_all函数

时间:2013-05-17 02:04:56

标签: c++

我正在编写列表和迭代器类函数,我差不多完成了,但是我在主文件中遇到了一些错误,当我编写列表remove_all函数时,但当我删除它时,任何地方都没有错误,我不知道为什么!!另外,实际上我不确定我的Iterator运算符和bool Iterator::is_item()

感谢任何帮助。感谢

这是我的代码:

Node.h

pragma once

namespace list_1
{
    template <typename T>
    struct Node
    {
        T data;
        Node<T> *next;


        // Constructor
        // Postcondition: 
        Node<T> (T d);
    };

    template <typename T>
Node<T>::Node(T d)
{

}
}

Iterator.h

// Template CLASS PROVIDED: Iterator 

#pragma once
#include "Node.h"

namespace list_1
{
    template<typename T>
    class Iterator
    {
    public:
        Iterator<T> (Node<T> *np);
        // precondition: is_item is true
        // post condition n points to the next item in the list
        void operator++();
        // precondition: 
        // postcondition: returns true if there is a valid item
        bool is_item();
        // precondition: is_item == true
        // postcondition returns data that n is pointing at
        T operator* ();

    private:
        Node<T>* n;

    };

List.h

#ifndef LIST_H
#define LIST_H
#include "Node.h"
#include "Iterator.h"

namespace list_1
{
    template <typename T>
    class list
    {
    public:
        // CONSTRUCTOR
        list( );
        // postcondition: all nodes in the list are destroyed.
        ~list();
        // MODIFICATION MEMBER FUNCTIONS
        //postcondition: entry is added to the front of the list
        void insert_front(const T& entry);
        //postcondition: entry is added to the back of the list
        void add_back(const T& entry);
        // postcondition: all nodes with data == entry are removed from the list
        void remove_all(const T& entry);
        // postcondition: an iterator is created pointing to the head of the list
        Iterator<T> begin(void);

        // CONSTANT MEMBER FUNCTIONS
        // postcondition: the size of the list is returned
        int size( ) const;
    private:
        Node<T>* head;

    };

1 个答案:

答案 0 :(得分:1)

您的语法错误是因为您在remove_all函数末尾的“else”块上缺少一对大括号。

尝试用此

替换它

(编辑:包括上述评论中提到的关于cout的建议)

void list<T>::remove_all(const T& entry)
{
    if(head == 0)
    {
        std::cout<<" node cannot be delted";

    }
    else
    {
        Node<T> *curr = head;
        Node<T> *trail = 0;

        while( curr != 0)
        {
            if(curr->entry == entry)
            {
                break;
            }
            else
            {
                trail = curr;
                curr = curr->next;
            }
        }
            if(curr == 0)
            {
                std::cout<<" Node " << entry<< " is not found";
            }
            else
            {
                if ( head == curr)
                {
                    head = head->next;
                }
                else
                {
                    trail->next = curr->next;
                }
            } // missing this one
     delete curr;
     } // and this one as well
}