创建NodeQueues

时间:2018-04-23 04:10:37

标签: c++

void NodeQueue::push(const DataType & value){
m_back = m_back->m_next = new Node(value, NULL);  }

你好,我正在使用Node Queues,我收到一个seg fault 11,我已经缩小到我的推送功能。我已经调整了一段时间,无法弄清楚出了什么问题。有什么建议吗?

这是驱动程序类:

NodeQueue nQueue;
cout << "Default Constructor: " << nQueue << endl;

DataType nParameter(1, 0);
NodeQueue nparameter(6, nParameter);
cout << "Paramaterized Constructor: " << nparameter << endl;

NodeQueue nCopier(nparameter);
cout << "Copy Constructor:          " << nCopier << endl;

nCopier.pop();
cout << "Pop Function:              " << nCopier << endl;

DataType nPushable(2,1);
nCopier.push(nPushable);
cout << "Push Function:             " << nCopier << endl;

nQueue = nCopier;
cout << "Assignment Operator:       " << nQueue << endl;

下面的NodeQueue构造函数:

    NodeQueue::NodeQueue() {
    m_front = NULL;
    m_back = NULL;
}

NodeQueue::NodeQueue(size_t size, const DataType & value) {
    if(size <= 0) {
        m_front = NULL;
    } else {
        m_front = new Node(value, NULL);
        Node *temp = m_front;
        for(size_t i = 0; i < (size); i++) {
            temp->m_next = new Node(value, NULL);
            temp = temp->m_next;
        }
        temp->m_next = NULL;
    }
}

NodeQueue::NodeQueue(const NodeQueue & other) {
    if(other.m_front != NULL) {
        m_front = new Node(other.m_front->data(), NULL);

        Node *newN = m_front;
        Node *tempN = other.m_front;

        while(tempN->m_next != NULL) {
            tempN = tempN->m_next;
            newN->m_next = new Node(tempN->data(), NULL);
            newN = newN->m_next;
        }
        newN->m_next = NULL;
    } else {
        m_front = NULL;
    }
}

0 个答案:

没有答案