实现单个链表的功能时的分段错误错误

时间:2016-09-22 07:45:36

标签: c++ c++11

我正在尝试为单链接列表创建一个函数,从i(参数)成员开始,删除每个第二个节点。 我这样做是出于基准测试的目的,我已经在Java中很好地工作了,但是我已经用C ++做了很多编程了几年,所以我有点生疏。

void List::remove(long i) {
    bool changelastonfirstrun=false;
    if(i==size){                                                
        changelastonfirstrun=true;                            
    }
if(size==0||i>size){
    cout <<"Index out of bounds or empty list";
}
else{
    Node* aux=first;
    Node* aux2=new Node();
    int j=1;
    while(size>1&&i>1){

            for(j=1; j<(i-1); j++){
            aux=aux->Next();
            }
        aux2=NULL;
        aux2=aux->Next();
        aux->SetNext(aux2->Next());
        size--;
        i=i-2;
        if(changelastonfirstrun){
            last=aux;
            changelastonfirstrun=false;
        }
        if(i==1){                                             //updates reference if removing the first
            if(size>1){
                first=first->Next();
                size--;
                }
            if(size==1){                                      //Removing a unique member
                    clear();
                }

            }
    }
    delete aux, aux2;
}    
}

我遇到了问题

        while(size>1&&i>1){
        for(j=1; j<(i-1); j++){
        aux=aux->Next();
        }

我第一次减少i。如果您需要更多信息,请告诉我。谢谢。

1 个答案:

答案 0 :(得分:0)

递归是一个很好的工具

// Example program
#include <iostream>

template <class T>
class Node {
private:
    T data_;
    Node *next_;
public:
    Node(const T &data) : data_(data), next_(nullptr) {}
    T data() const {return data_;}
    Node *next() const {return next_;}
    void setNext(Node *next) {next_ = next;}
};

template <class T>
class List {
private:
    size_t size_;
    T *begin_;
public:
    List() : size_(0), begin_(nullptr) {}
    size_t size() const {return size_;}
    void push_back(T *node) {
        if ( size_ ) {
            T *last = begin_;
            for(size_t ct = 0; ct < size_ - 1; ++ct)
                last = last->next();
            last->setNext(node);
        }
        else
            begin_ = node;
        ++size_;
    }
    T *begin() const {return begin_;}
    void removeEverySecondFrom(size_t startFrom) {
        if( startFrom >= size_ || size_ == 0)
            return;
        auto toDeleteNext = begin_;
        for(size_t ct = 1; ct < startFrom; ++ct)
            toDeleteNext = toDeleteNext->next();
        if( startFrom == size_ - 1 ) {
            toDeleteNext->setNext(nullptr);
            --size_;
        }
        else {
            T* toDelete = nullptr;
            if( startFrom == 0 ) {
                toDelete = begin_;
                begin_ = begin_->next();
            }
            else {
                toDelete = toDeleteNext->next();
                toDeleteNext->setNext(toDelete->next());
            }
            --size_;
            delete toDelete;
            removeEverySecondFrom(startFrom+1);
        }
    }
};

int main()
{
    using NodeType = Node<int>;
    using ListType = List<NodeType>;
    auto genList = [](){
        ListType list;
        for(size_t ct = 0; ct < 11; ++ct)
            list.push_back(new NodeType(ct));
        return list;
    };
    auto printList = [](const ListType& list){
        auto begin = list.begin();
        for(size_t ct = 0; ct < list.size(); ++ct) {
            std::cout << begin->data() << "\t";
            begin = begin->next();
        }
        std::cout << std::endl;
    };

    auto list = genList();
    printList(list);

    list = genList();
    list.removeEverySecondFrom(0);
    printList(list);

    list = genList();
    list.removeEverySecondFrom(1);
    printList(list);

    list = genList();
    list.removeEverySecondFrom(9);
    printList(list);

    list = genList();
    list.removeEverySecondFrom(10);
    printList(list);

    list = genList();
    list.removeEverySecondFrom(11);
    printList(list);
}

输出

0   1   2   3   4   5   6   7   8   9   10  
1   3   5   7   9   
0   2   4   6   8   10  
0   1   2   3   4   5   6   7   8   10  
0   1   2   3   4   5   6   7   8   9   
0   1   2   3   4   5   6   7   8   9   10

在这里测试http://cpp.sh/2egub