繁体   English   中英

特定行后未输出Visual Studio 2017代码

[英]Visual studio 2017 code not outputting after a certain line

我正在编写一个程序,该程序需要8个用户输入的整数并从中生成一个链接列表。 我让程序打印链接列表,然后删除最后一个节点,然后反向打印列表。 在测试过程中,我一直在进行编程以确保每个部分都工作正常,并且可以完成打印原始链接列表的工作。

当我完成修改的代码编写并打印部分时,我遇到了一个问题-在打印出原始列表后,该程序将不会输出任何内容。 例如,如果我输入1,2,3,4,5,6,7,8,它将输出:1 2 3 4 5 6 7 8就是这样。 我已经尝试过将cout <<“ testing”; 在不同的位置查看我的代码停止输出的位置,以及成功输出的最新位置就在while循环之前。

我不确定为什么while循环会导致程序停止输出任何东西,甚至是与while循环本身无关的任意cout语句,所以我想在这里问。 如果有帮助,我正在使用Visual Studio 2017。 感谢您提供的所有帮助!

#include <iostream>
using namespace std;

void getdata(int & info); //function that assigns a user inputted value to each node
const int nil = 0;
class node_type         // declaration of class
{
public:
    int info;
    node_type *next;
};

int main()
{
    node_type *first, *p, *q, *r, *newnode;
    first = new node_type;
    newnode = new node_type;
    int info;
    getdata(info); //first node
    (*first).info = info;
    (*first).next = nil;
    getdata(info); //second node
    (*newnode).info = info;
    (*first).next = newnode;
    (*newnode).next = nil;
    p = newnode;
    for (int i = 2; i < 8; i++) //nodes 3-8
    {
        newnode = new node_type;
        getdata(info);
        (*newnode).info = info;
        (*p).next = newnode;
        p = newnode;
        (*newnode).next = nil;
    }
    q = first;
    while (q != nil) // printing linked list
    {
        cout << (*q).info << "\n";
        q = (*q).next;
    }

    //deletes last node then reverses list
    p = first;
    q = (*p).next;
    r = (*q).next;

    if (first == nil) //if list is empty
        cout << "Empty list";
    else if ((*first).next == nil) //if list has one node
        first = nil;
    else if (r == nil) //if list has two nodes
        q = nil;
    else //general case
    {
        (*first).next = nil; //last line where when i put a cout << ""; it prints in the output window
        while ((*r).next != nil)
        {
            (*q).next = p;
            (*r).next = q;
            p = q;
            q = r;
            r = (*r).next;
        }
        (*q).next = p;
        first = q;
    }
    q = first;
    while (q != nil) // printing newly modified list.
    {
        cout << (*q).info << "\n";
        q = (*q).next;
    }
    return 0;
}
void getdata(int & info)
{
    cout << "Enter number: \n";
    cin >> info;
}
else //general case
{
    (*first).next = nil; //last line where when i put a cout << ""; it prints in the output window
    while ((*r).next != nil)
    {
        (*q).next = p;
        (*r).next = q;
        p = q;
        q = r;
        r = (*r).next;
    }
    (*q).next = p;
    first = q;
}

反转列表时,您会陷入无限循环。 这就是为什么它不输出任何东西的原因。
一旦将元素2链接到元素3,就在下一步中将元素3链接到元素2。
当在列表中进行反向迭代时,这将导致无限循环。

在一般情况下,将其反转的正确方法如下:

else //general case
{
    node_type* current = first;
    node_type* next = first->next;

    (*first).next = nil; //last line where when i put a cout << ""; it prints in the output window
    while (next)
    {
        node_type* temp = next->next;
        next->next = current;
        current = next;
        next = temp;
    }

    first = current;
}

在旁注中,应使用bla->foo取消引用,而不是(*bla).foo

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM