Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链表显示垃圾字符_C++_Visual Studio_Linked List_Visual Studio 2017_C++17 - Fatal编程技术网

C++ 链表显示垃圾字符

C++ 链表显示垃圾字符,c++,visual-studio,linked-list,visual-studio-2017,c++17,C++,Visual Studio,Linked List,Visual Studio 2017,C++17,不知道为什么,但每次我显示链接列表时,它只显示垃圾字符。当我将\u getche添加到第31行,并使用\u putch(当前->c)显示第53行的值时,出现了此问题如果有人能帮我描述一下我的问题是什么,并提供一个非常感谢的解决方案 #include <iostream> #include <string> #include <conio.h> using namespace std; class ListNode { public: char c;

不知道为什么,但每次我显示链接列表时,它只显示垃圾字符。当我将
\u getche
添加到第31行,并使用
\u putch(当前->c)显示第53行的值时,出现了此问题如果有人能帮我描述一下我的问题是什么,并提供一个非常感谢的解决方案

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

class ListNode
{
public:
    char c;
    ListNode *next;
};

int main()
{
    ofstream outputFile;
    ListNode *current;
    ListNode *start;
    ListNode *newNode = new ListNode();

    current = nullptr;
    start = newNode;
    newNode->next = nullptr;;

    cout << "Hit 'esc' when you are done.\n";
    while (newNode->c = _getche() != 27)
    {
        //If start is empty, create node
        if (current == nullptr)
        {
            current = newNode;
        }
        else //If start is not empty, create new node, set next to the new node
        {
            current->next = newNode;
            current = newNode;
        }

        newNode = new ListNode();
        newNode->next = nullptr;
    }

    //Display linked list
    cout << "Here is what you have typed so far:\n";
    current = start;
    while (current != nullptr)
    {
        _putch(current->c);
        current = current->next;
    }
    cout << endl;

    outputFile.close();
    system("pause");
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
类ListNode
{
公众:
字符c;
ListNode*下一步;
};
int main()
{
流输出文件;
ListNode*当前;
ListNode*开始;
ListNode*newNode=新ListNode();
电流=零PTR;
开始=新节点;
newNode->next=nullptr;;
cout c=_getche()!=27)
{
//如果start为空,则创建节点
如果(当前==nullptr)
{
当前=新节点;
}
else//如果start不为空,则创建新节点,并在新节点旁边设置
{
当前->下一步=新节点;
当前=新节点;
}
newNode=newlistnode();
newNode->next=nullptr;
}
//显示链表
cout(c);
当前=当前->下一步;
}
coutIn:

=
的值低于
=,因此它分配
\u getche()!=27
新节点->c

修正:


通过维护指向最后一个节点的
next
指针的
ptail
指针(用
head
初始化),可以更轻松地添加单链接列表:

ListNode *head = nullptr, **ptail = &head;

cout << "Hit 'esc' when you are done.\n";
for(char c; (c = _getche()) != 27;) {
    auto node = new ListNode{c, nullptr}; // allocate and initialize a new node
    *ptail = node; // append to the end of the list
    ptail = &node->next; // move the end of list to the new node
}

//Display linked list
cout << "Here is what you have typed so far:\n";
for(auto next = head; next; next = next->next)
    _putch(next->c);
ListNode*head=nullptr,**ptail=&head;
无法下一步;//将列表末尾移动到新节点
}
//显示链表
(下一步)
_putch(下一步->c);
while((newNode->c = _getche()) != 27)
ListNode *head = nullptr, **ptail = &head;

cout << "Hit 'esc' when you are done.\n";
for(char c; (c = _getche()) != 27;) {
    auto node = new ListNode{c, nullptr}; // allocate and initialize a new node
    *ptail = node; // append to the end of the list
    ptail = &node->next; // move the end of list to the new node
}

//Display linked list
cout << "Here is what you have typed so far:\n";
for(auto next = head; next; next = next->next)
    _putch(next->c);