双链表列表代码修订

时间:2018-06-11 10:31:26

标签: c++ linked-list

从我在代码审查中发布的帖子中进行适当更改时遇到了一些问题。这是我发布的原始帖子here。具体来说,我正在修改我的insertPosition函数。

以前我的功能是这样的:

template <class T>
void DoubleLinkedLists<T>::insertPosition(int pos, const T& theData) {
    Node* prev = new Node;
    Node* current = head;
    Node* newNode = new Node;

    for(int i = 1; i < pos; i++) {
        prev = current;
        current = current->next;
    }
    newNode->data = theData;
    prev->next = newNode;
    newNode->next = current;
}

但是现在,从我被告知的事情来看,它更像是这样:

Node* cur_node = head;
int i = 0;
while (cur_node) {
    if (i++ == pos) {
        // do the deed
    }
    cur_node = cur_node->next;
}

我不确定在//做行动部分写什么。我试过的是:

template <class T>
void DoubleLinkedLists<T>::insertPosition(int pos, const T& theData) {
    Node* current = head;
    int i = 0;
    while(current) {
        if(i++ == pos) {
            Node* newNode = new Node;
            newNode->data = theData;
            newNode->next = newNode->previous;
        }
        current = current->next;
    }
}

但是当我测试它时,我根本没有得到代码。还有其他一些我需要改变的东西我确定但是我希望得到我的代码到目前为止的第二个意见。

这是头文件:

#ifndef DoubleLinkedLists_h
#define DoubleLinkedLists_h

template <class T>
class DoubleLinkedLists {
private:

    struct Node {
        T data;
        Node* next;
        Node* previous;
    };

    Node* head;
    Node* tail;

public:
    // Constructors
    DoubleLinkedLists() : head(nullptr), tail(nullptr) {}                  // empty constructor
    DoubleLinkedLists(DoubleLinkedLists const& value);                     // copy constructor
    DoubleLinkedLists<T>(DoubleLinkedLists<T>&& move) noexcept;            // move constuctor
    DoubleLinkedLists<T>& operator=(DoubleLinkedLists&& move) noexcept;    // move assignment operator
    ~DoubleLinkedLists();                                                  // destructor

    // Overload operators
    DoubleLinkedLists& operator=(DoubleLinkedLists const& rhs);
    friend std::ostream& operator<<(std::ostream& str, DoubleLinkedLists<T> const& data) {
        data.display(str);
        return str;
    }

    // Member functions
    void swap(DoubleLinkedLists& other) noexcept;
    void createNode(const T& theData);
    void createNode(T&& theData);
    void display(std::ostream& str) const;
    void insertHead(const T& theData);
    void insertTail(const T& theData);
    void insertPosition(int pos, const T& theData);
    void deleteHead();
    void deleteTail();
    void deletePosition(int pos);
    bool search(const T& x);
};

template <class T>
DoubleLinkedLists<T>::DoubleLinkedLists(DoubleLinkedLists const& value) : head(nullptr), tail(nullptr) {
    for(Node* loop = value->head; loop != nullptr; loop = loop->next) {
        createNode(loop->data);
    }
}

template <class T>
DoubleLinkedLists<T>::DoubleLinkedLists(DoubleLinkedLists<T>&& move) noexcept : head(nullptr), tail(nullptr) {
    move.swap(*this);
}

template <class T>
DoubleLinkedLists<T>& DoubleLinkedLists<T>::operator=(DoubleLinkedLists<T> &&move) noexcept {
    move.swap(*this);
    return *this;
}

template <class T>
DoubleLinkedLists<T>::~DoubleLinkedLists() {
    while(head != nullptr) {
        deleteHead();
    }
}

template <class T>
DoubleLinkedLists<T>& DoubleLinkedLists<T>::operator=(DoubleLinkedLists const& rhs) {
    DoubleLinkedLists copy(rhs);
    swap(copy);
    return *this;
}

template <class T>
void DoubleLinkedLists<T>::swap(DoubleLinkedLists<T>& other) noexcept {
    using std::swap;
    swap(head, other.head);
    swap(tail, other.tail);
}

template <class T>
void DoubleLinkedLists<T>::createNode(const T& theData) {
    Node* newData = new Node;
    newData->data = theData;
    newData->next = nullptr;

    if(head == nullptr) {
        newData->previous = nullptr;
        head = newData;
        tail = newData;
    }
    else {
        newData->previous = tail;
        tail->next = newData;
        tail = newData;
    }
}

template <class T>
void DoubleLinkedLists<T>::createNode(T&& theData) {
    Node* newData = new Node;
    newData->data = std::move(theData);
    newData->next = nullptr;

    if(head == nullptr) {
        newData->previous = nullptr;
        head = newData;
        tail = newData;
    }
    else {
        newData->previous = tail;
        tail->next = newData;
        tail = newData;
    }
}

template <class T>
void DoubleLinkedLists<T>::insertHead(const T& theData) {
    Node* newNode = new Node;
    newNode->data = theData;
    newNode->next = head;
    head->previous = newNode;
    head = newNode;
}

template <class T>
void DoubleLinkedLists<T>::insertTail(const T& theData) {
    Node* newNode = new Node;
    newNode->data = theData;
    newNode->previous = tail;
    tail->next = newNode;
    tail = newNode;
}

//template <class T>
//void DoubleLinkedLists<T>::insertPosition(int pos, const T& theData) {
//    Node* prev = new Node;
//    Node* current = head;
//    Node* newNode = new Node;
//
//    for(int i = 1; i < pos; i++) {
//        prev = current;
//        current = current->next;
//    }
//    newNode->data = theData;
//    prev->next = newNode;
//    newNode->next = current;
//}

template <class T>
void DoubleLinkedLists<T>::insertPosition(int pos, const T& theData) {
    Node* current = head;
    int i = 0;
    while(current) {
        if(i++ == pos) {
            Node* newNode = new Node;
            newNode->data = theData;
            newNode->next = newNode->previous;
        }
        current = current->next;
    }
}

template <class T>
void DoubleLinkedLists<T>::display(std::ostream &str) const {
    for(Node* loop = head; loop != nullptr; loop = loop->next) {
        str << loop->data << "\t";
    }
    str << "\n";
}

template <class T>
void DoubleLinkedLists<T>::deleteHead() {
    Node* old = head;
    head = head->next;
    delete old;
}

template <class T>
void DoubleLinkedLists<T>::deleteTail() {
    Node* prev = nullptr;
    Node* current = head;

    while(current->next != nullptr) {
        prev = current;
        current = current->next;
    }
    tail = prev;
    prev->next = nullptr;
    delete current;
}

template <class T>
void DoubleLinkedLists<T>::deletePosition(int pos) {
    Node* prev = new Node;
    Node* current = head;

    for(int i = 1; i < pos; i++) {
        prev = current;
        current = current->next;
    }
    prev->next = current->next;
}

template <class T>
bool DoubleLinkedLists<T>::search(const T &x) {
    Node* current = head;
    while(current != nullptr) {
        if(current->data == x) {
            return true;
        }
        current = current->next;
    }
    return false;
}



#endif /* DoubleLinkedLists_h */

以下是测试上述实现的main.cpp文件:

#include <iostream>
#include "DoubleLinkedLists.h"


int main(int argc, const char * argv[]) {

///////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////// Double Linked List //////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////
    DoubleLinkedLists<int> obj;
    obj.createNode(2);
    obj.createNode(4);
    obj.createNode(6);
    obj.createNode(8);
    obj.createNode(10);
    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"---------------Displaying All nodes---------------";
    std::cout<<"\n--------------------------------------------------\n";
    std::cout << obj << std::endl;

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"----------------Inserting At Start----------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.insertHead(50);
    std::cout << obj << std::endl;

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"-----------------Inserting At End-----------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.insertTail(20);
    std::cout << obj << std::endl;

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"-------------Inserting At Particular--------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.insertPosition(5,60);
    std::cout << obj << std::endl;

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"----------------Deleting At Start-----------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.deleteHead();
    std::cout << obj << std::endl;

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"----------------Deleting At End-----------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.deleteTail();
    std::cout << obj << std::endl;

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"--------------Deleting At Particular--------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.deletePosition(4);
    std::cout << obj << std::endl;
    std::cout << std::endl;

    obj.search(8) ? printf("Yes"):printf("No");






    return 0;
}

更新

这是我到目前为止所做的,我将数据传递到一个新节点,然后将上一个节点与下一个节点链接起来。虽然代码运行但实际上没有插入到某个位置,所以我相信它甚至不会通过while循环。我不确定是什么问题。

template <class T>
void DoubleLinkedLists<T>::insertPosition(int pos, const T& theData) {
    Node* current = head;
    int i = 0;
    while(current) {
        if(i++ == pos) {
            Node* newNode = new Node;
            newNode->data = theData;
            newNode->previous = newNode->next;
        }
        current = current->next;
    }
}

1 个答案:

答案 0 :(得分:1)

因此...

    template <class T>
void DoubleLinkedLists<T>::insertPosition(int pos, const T& theData) {
    Node* current = head;
    int i = 0;
    while (current != nullptr) {
        if (i++ == pos) {
            Node* newNode = new Node;
            newNode->data = theData;
            // Let's do the wiring
            newNode->previous = current->previous;
            newNode->next = current;
            if (newNode->previous != nullptr) {  // If the node is inserted at the end
                newNode->previous->next = newNode;
            }
            current->previous = newNode;
            return;
        }
        current = current->next;
    }
}