双向链表C ++逻辑错误

时间:2015-04-27 15:43:04

标签: c++

刚刚学会了双向链表的概念。我实现了它,幸运的是它成功了。但我的问题是当我测试一些测试用例时,这是我的测试用例,

  

(第1列是命令。第2列是应用命令的索引。第3列是插入的字。命令1是插入,命令2是删除,命令4是设置,命令6是清除,命令7是打印,命令8是退出。)

1 0 this
1 1 would
1 2 be
1 3 cleared
6
1 0 hello
1 1 world
1 1 beautiful
1 2 world
4 2 wonderful
2 1
1 0 what
4 1 a
7
8

似乎是

[what, a, beautiful, wonderful, world]

应该打印

[what, a, wonderful, world].

有人可以帮我解决这个问题吗?

#include<iostream>
#include<cstdlib>
using namespace std;
template <class T>
class Node {

    private:
    T data;
    Node<T> *next;
    Node<T> *prev;

    public:
    Node(T data) {
        this->data = data;
        this->next = NULL;
        this->prev = NULL;
    };
    void setData(T data) {
        this->data = data;
    };
    T getData() {
        return this->data;
    };
    void setNext(Node *next) {
        this->next = next;
    };
    Node *getNext() {
        return this->next;
    };
    void setPrev(Node *prev) {
        this->prev = prev;
    };
    Node *getPrev() {
        return this->prev;
    };

};

template <typename T>
class List {

    private:
    Node<T> *first; // or start or head or something
    Node<T> *last; // or end or tail or something
    int _size;

    public:
    List() {
        this->first = NULL;
        this->last = NULL;
        this->_size = 0;
    };
    ~List();
    void insert(int index, T data);
    T remove(int index);
    T get(int index);
    void set(int index, T data);
    int size();
    bool isEmpty();
    void clear();
    friend ostream &operator <<(ostream &os, const List &list) {
        if (list._size == 0) os << "*** List is empty. ***" << endl;
        else {
            os << "[";
            for (Node<T> *i = list.first; i != NULL; i = i->getNext()) {
                os << i->getData();
                if (i->getNext() != NULL) os << ", ";
                else os << "]" << endl;
            }
        }
        return os;
    };
};
template <class T>
void List<T>::insert(int index, T data)
{
    Node<T> *newNode;
    newNode = new Node<T>(data);
    Node<T> *temp;
    temp = first;
    int i = 0;
    if (index > size())
    {
        cout << "Sorry, can't perform this.";
        return;
    }
    if (temp == NULL)
    {
        this->first = newNode;
        this->last = newNode;
        this->_size++;
        return;
    }
    if(index == size())
    {
        while (i < index-1)
        {
            i++;
            temp = temp->getNext();
        }
        newNode->setData(data);
        temp->setNext(newNode);
        newNode->setPrev(temp);
        newNode->setNext(NULL);
        this->last = newNode;
        this->_size++;
    }
    else if (index == 0)
    {
        this->first = newNode;
        newNode->setData(data);
        newNode->setPrev(NULL);
        newNode->setNext(temp);
        temp->setPrev(newNode);
        this->_size++;
    }
    else if (index > 0 && index < size())
    {
        while (i < index-1)
        {
            i++;
            temp = temp->getNext();
        }
        newNode->setData(data);
        newNode->setPrev(temp);
        newNode->setNext(temp->getNext());
        temp->setNext(newNode);
        temp = temp->getNext();
        temp->setPrev(newNode);
        this->_size++;
    }
}
template <class T>
int List<T>::size()
{
    return this->_size;
}
template <class T>
T List<T>::remove(int index)
{
    Node<T> *temp = first;
    int i = 0;
    T value;
    if (temp == NULL)
    {
        cout << "Couldn't perform this.";
        return 0;
    }
    if (temp->getNext() == NULL)
    {
        value = temp->getData();
        delete temp;
        this->first = NULL;
        this->last = NULL;
        _size--;
        return value;
    }
    else if (index == _size - 1)
    {
        temp = this->last;
        this->last = this->last->getPrev();
        this->last->setNext(NULL);
        value = temp->getData();
        delete temp;
        _size--;
        return value;
    }
    else if (index == 0)
    {
        value = this->first->getData();
        this->first = this->first->getNext();
        this->first->setPrev(NULL);
        delete temp;
        _size--;
        return value;
    }
    else if (index > 0 && index < size() - 1)
    {
        Node<T> *temp2 = this->first;
        Node<T> *temp3 = this->first;
        while (i < index)
        {
            temp3 = temp3->getNext();
            temp2 = temp2->getNext();
            temp = temp->getNext();
            i++;
        }
        temp3 = temp3->getNext();
        temp2 = temp2->getPrev();
        temp2->setNext(temp3);
        temp3->setPrev(temp2);
        value = temp->getData();
        _size--;
        return value;
    }
}
template <class T>
T List<T>::get(int index)
{
    int i = 0;
    Node<T> *temp = first;
    if(temp == NULL) return "";
    if (index > _size) return "";
    while (i < index )
    {
        temp = temp->getNext();
        i++;
    }
    return temp->getData();
}
template <class T>
bool List<T>::isEmpty()
{
    if (size() <= 0)
        return true;
    return false;
}
template <class T>
void List<T>::clear()
{
    Node<T> *temp = first;
    while (this->first != NULL)
    {
        _size--;
        this->first = this->first->getNext();
        delete temp;
        temp = first;
    }
    this->last = NULL;
}
template <class T>
void List<T>::set(int index, T data)
{
    Node<T> *temp = first;

    if (temp == NULL)
    {
        cout << "Couldn't perform this.\n";
        return;
    }
        int i = 0;
        while (i < index)
        {
            temp = temp->getNext();
            i++;
        }
        temp->setData(data);
        return;
}
template <class T>
List<T>::~List()
{
    cout << "Bye";
}
int main()
{
    List <string> lists;
    int index, prompt;
    string data;
    while (prompt != 8)
    {
        cout << "\n(1) Insert (index, data)\n(2) Remove (index)\n(3) Get (index)\n(4) Set (index, data)\n(5) Size\n(6) Clear\n(7) Show\n(8) Exit";
        cout << "\nEnter number: ";
        cin >> prompt;
        if (prompt == 1)
        {
            cout << "Insert (index, data): ";
            cin >> index;
            cin >> data;
            lists.insert(index, data);
        }
        else if (prompt == 2)
        {
            cout << "Enter index: ";
            cin >> index;
            lists.remove(index);
        }
        else if (prompt == 3)
        {
            cout << "Enter index: ";
            cin >> index;
            if (lists.get(index) == "")
            {
                cout << "Very impossible.";
            }
            else cout << lists.get(index) << endl;
        }
        else if (prompt == 4)
        {
            cout << "Set (index, data): ";
            cin >> index;
            cin >> data;
            lists.set(index, data);
        }
        else if (prompt == 5)
        {
            cout << lists.size();
        }
        else if (prompt == 6)
        {
            lists.clear();
        }
        else if (prompt == 7)
        {
            cout << lists;
        }
        else
        {
            exit(1);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

List::remove()

中存在逻辑错误

如果我更改了行:

    Node<T> *temp2 = this->first;
    Node<T> *temp3 = this->first;

由:

    Node<T> *temp2 = this->first;
    Node<T> *temp3 = temp2->getNext();

一切都对我有用。