添加方法不返回任何值

时间:2014-04-22 01:55:56

标签: c++ linked-list add

我正在为C ++中的Linked List类编写一个add方法。编写add方法是为了处理基于方法输入可能发生的各种情况。我添加了" cout"语句,以尝试跟踪方法进入哪些循环/ if语句,但没有从该方法打印任何内容。该方法的输入是节点的索引和节点中保存的数据。

void linkedList::add(int pos, string item)
{
    Node *temp = new Node;
    Node *nextTemp = new Node;

    if(pos == 0)
    {
        cout<< "hey";
        if(head != NULL)
        {
            cout<< "hey1";
            temp->setData((head->getPosition()+1), 
                    head->getItem(), head->getNextNode());
            head->setData(pos, item, temp);
            currentNode = head;
            size++;
        }
        else
        {
            cout<< "hey2";
            head->setData(pos, item, temp);
            currentNode = head;
            size++;
        }
    }
    else if(pos == size)
    {
        cout<< "hey3";
        while(currentNode != NULL)
        {
            cout<< "hey4";
            currentNode = currentNode->nextNode;
        }
        temp->setData(pos, item, nextTemp);
        currentNode->nextNode = temp;
        size++;
    }

    else
    {
        cout<< "hey5";
        for(int i = 0; i<size; i++)
        {
            cout<< "hey6";
            if(i == currentNode->getPosition())
            {
                cout<< "hey7";
                temp->setData(currentNode->getPosition(), 
                        currentNode->getItem(), currentNode->getNextNode());
                currentNode->setData(pos, item, temp);
                size++;
            }

            currentNode = currentNode->nextNode;
            while(currentNode->getPosition() < (size+1))
            {
                cout<< "hey8";
                currentNode->setPosition(i+1);
                currentNode = currentNode->nextNode;
            }
        }
    }
}

任何帮助将不胜感激。谢谢。

0 个答案:

没有答案