首页 > 解决方案 > C++ 中的双向链表迭代器打印中的程序崩溃

问题描述

我正在尝试运行我的链表迭代器来打印我输入到列表中的数据。它确实设法打印所有条目,但一旦打印,程序就会崩溃。有谁看到可能是什么原因造成的?我在 CodeBlocks 上运行它,所以我不知道这是否是问题所在。

这是我在其他问题中一直在处理的同一个项目。在我尝试运行迭代器以打印列表类中函数的内容之前。但是,分配的规范不允许类触发报告中的任何错误或控制消息。理想的情况是用户可以完全访问数据。诸如控制台消息之类的任何事情都必须在 main 中完成。这是我调整后得到的。再次,它成功打印列表的内容,但之后,程序停止工作。

代码:

#include <iostream>
#include <string>

using namespace std;

typedef string dataType;
typedef class LISTEntry* listPtr;

class DATAClass
{
    dataType key;
public:
    DATAClass();
    DATAClass(dataType key);
    DATAClass(DATAClass &dc);

    void setKey(dataType keyVal);
    dataType getKey();
};

DATAClass::DATAClass() {
  key = key;
}

DATAClass::DATAClass(dataType key) {
  this->key = key;
}

DATAClass::DATAClass(DATAClass &dc) {
  key = dc.key;
}

void DATAClass::setKey(dataType keyVal) {
  key = keyVal;
}

dataType DATAClass::getKey() {
  return key;
}


class LISTEntry
{
  DATAClass data;
  listPtr prev;
  listPtr next;

public:
  LISTEntry() {
    this->data = data;
    this->prev = nullptr;
    this->next = nullptr;
    };

  LISTEntry(DATAClass l) {  
    this->data = l;
    this->prev = nullptr;
    this->next = nullptr;
    };

  LISTEntry(LISTEntry &le) {
    data = le.getData();
    prev = nullptr;
    next = nullptr;
    };

  ~LISTEntry() {
  };

  LISTEntry & operator=(const LISTEntry &le) {
    if (this != &le)
        {
            data = le.data;
            prev = nullptr;
            next = nullptr;
        }
    };

  LISTEntry * getNext() {
        return next;
    };

  void setNext(LISTEntry * entry) {
        next = entry;
    };

  LISTEntry * getPrev() {
        return prev;
    };

  void setPrev(LISTEntry * entry) {
        prev = entry;
    };

  DATAClass getData() {
        return data;
    };

  void setData(DATAClass d) {
        data = d;
    };
};


class LIST
{
private:
    listPtr curr;
    listPtr head;
    listPtr tail;

public:
    LIST();
    LIST(DATAClass d);
    LIST(LISTEntry l);
    LIST(LIST * l);
    ~LIST();

    void addList(dataType n);
    void iterateInitialize();
    bool iterate_hasNext();
    bool searchList(dataType key);

    LISTEntry * getHead();
    void setHead(LISTEntry * h);
    LISTEntry * getTail();
    void setTail(LISTEntry * t);
    LISTEntry * getCurr();
    void setCurr(LISTEntry * c);

    bool isEmpty();
    bool isFull();
};

LIST::LIST() {
    curr = nullptr;
    head = nullptr;
    tail = nullptr;
};

LIST::LIST(DATAClass d) {
    listPtr tmp;
    tmp->setData(d);
    tmp->getData();

    curr = tmp;
    head = nullptr;
    tail = nullptr;
};

LIST::LIST(LISTEntry l) {
    listPtr tmp;

    curr = tmp;
    head = tmp;
    tail = tmp;
};

LIST::LIST(LIST * l) {
    curr = nullptr;
    head = nullptr;
    tail = nullptr;
};

LIST::~LIST()
{}

void LIST::addList(dataType n) {
    DATAClass dc;
    dc.setKey(n);

    listPtr entry = new LISTEntry();
    entry->setData(dc);
    entry->setNext(nullptr);
    entry->setPrev(nullptr);

    if (!isEmpty())
    {
        tail->setNext(entry);
        entry->setPrev(tail);
        tail = entry;
        curr = tail;

        curr->setNext(nullptr);
        tail->setNext(nullptr);
    }

    else
    {
        head = tail = entry;
        head->setPrev(nullptr);
        tail->setNext(nullptr);

        curr = head;
    }
} //addList


void LIST::iterateInitialize() {
    setCurr(head);
}

bool LIST::iterate_hasNext() {
    return curr->getNext() != nullptr;
}

bool LIST::searchList(dataType key) {
    curr = head;
    while (curr != nullptr)
    {
        if (curr->getData().getKey() == key)
        {
            return true;
        }

        else
        {
            curr = curr->getNext();
        }
    }

    return false;
}

LISTEntry * LIST::getHead() {
    return head;
}

void LIST::setHead(LISTEntry * h) {
    head = h;
}

LISTEntry * LIST::getTail() {
    return tail;
}

void LIST::setTail(LISTEntry * t) {
    tail = t;
}

LISTEntry * LIST::getCurr() {
    return curr;
}

void LIST::setCurr(LISTEntry * c) {
    curr = c;
}

bool LIST::isEmpty() {
    return head == nullptr;
}

bool LIST::isFull() {
    return (new(LISTEntry) == nullptr);
}


int main()
{
    LIST * burger = new LIST();

    burger->addList("meat");
    burger->addList("pickles");
    burger->addList("onions");
    burger->addList("tomato");
    burger->addList("cheese");
    burger->addList("bun");

    burger->iterateInitialize();
    listPtr bgrIndex = burger->getCurr();

    while (burger->iterate_hasNext())
    {
        cout << bgrIndex->getData().getKey() << " ";
        bgrIndex = bgrIndex->getNext();

        if (!(burger->iterate_hasNext()))
            cout << endl;
    }

    dataType searchValue;
    while (searchValue != "end")
    {
        cout << "Enter an item to look up in the list: " << endl;
        cin >> searchValue;

        if (burger->searchList(searchValue))
            cout << "The item " << searchValue << " was found." << endl;

        else
            cout << "The item " << searchValue << " is not in the list." << endl;
    }

    delete burger;

    return 0;
}

标签: c++listiteratordoubly-linked-list

解决方案


推荐阅读