删除链表功能不匹配所有测试用例

时间:2019-01-31 07:33:48

标签: c++ linked-list

有人能启发我为什么我的大小调整功能不删除链接列表末尾的节点吗?抱歉,我的代码过长。

对于这一系列代码,我应该调整列表的大小以包含n元素。如果n小于当前大小,则仅保留前n个元素,然后销毁其余元素。如果n大于当前大小,则新元素将初始化为val

我的输出: -1 -1 -1 -1 -1 -2 -2(7个节点)

所需的输出: -1 -1 -1 -1 -1 -2(6个节点)

void list::resize(int n, int val) {

    int count = 0;

    int count1 = n;
    if (the_list == nullptr) {
       // handling of case when list is empty
        }
    } else {
        node *p = the_list;
        node *pc = the_list;
        while (p->next != nullptr) {
            p = p->next;
            count++;
        }
        if (n < count) {    
            if (n== 0) {
               //if n==0, delete the all of the nodes in the linked list
                }
            } else {
                while (count1!= 1) {
                    pc=pc->next;
                    count1--;
                }
                if (pc->next->next == nullptr) {
                    delete pc;
                    pc->next = nullptr;
                } else {
                    for (int i = 0; i < count-n+1; i++) {
                        node *nxt = pc->next->next; 
                        delete pc->next;
                        pc->next = nxt;
                    }
                }
            }
        } else {
            // handling of case where code is required to add nodes with a special value
            }
        }
    }
}

int main(void)
{

std::string funcName[6] = 
                        {"Push_Front          ",
                         "Erase One Element   ",
                         "Erase Some Elements ",
                         "Resize w New Val    ",
                         "Resize w Default Val",
                         "Merge               "};

int choice, listID = 0, pos1, pos2, newSize, newVal;
CS170::list list1, list2;
CS170::list *chosenList;

do
{
    std::cin >> choice;
    if (choice != 0 && choice != 6)
    {
        std::cin >> listID;
    }

    if (choice != 0)
    {
        std::cout << funcName[choice-1] << ": ";

        if (listID == 1)
            chosenList = &list1;
        else
            chosenList = &list2;

        switch (choice)
        {
            case 1:
                std::cin >> newVal;
                chosenList->push_front(newVal);
                break;

            case 2:
                std::cin >> pos1;
                chosenList->erase(pos1);
                break;

            case 3:
                std::cin >> pos1 >> pos2;
                chosenList->erase(pos1, pos2);
                break;

            case 4:
                std::cin >> newSize >> newVal;
                chosenList->resize(newSize, newVal);
                break;

            case 5:
                std::cin >> newSize;
                chosenList->resize(newSize);
                break;

            case 6:
                list1.sort();
                list2.sort();
                list1.merge(list2);
                break;
        }

        if( is_circular( list1 ) || 
            is_circular( list2 ) )
        {
          std::cin.sync();
          std::cin.ignore(std::numeric_limits<std::streamsize>::max());
          std::cout << "List is invalid (circular), test exiting" << std::endl;
          return 0;
        }

        switch (choice)
        {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                std::cout << "List" << listID << " (" << std::setw(2) << 
                    chosenList->size() << " nodes): ";
                chosenList->print_list();
                break;

            case 6:
                std::cout << "\n";
                std::cout << "List1" << " (" << std::setw(2) << 
                    list1.size() << " nodes): ";
                list1.print_list();

                std::cout << "List2" << " (" << std::setw(2) << 
                    list2.size() << " nodes): ";
                list2.print_list();
                break;
        }
    }
}
while (choice != 0);

return 0;
}

0 个答案:

没有答案