【问题标题】:Linked List Print() Function Printing Ascii number of CharLinked List Print() 函数打印字符的ASCII码
【发布时间】:2020-06-04 12:42:57
【问题描述】:

我创建类型链表并从用户那里获取字符。 这是类定义。

    template <class nodeT>
struct nodeSLL
{
    int data;                                               //Data inside the node
    nodeSLL* link;                                          //Address next node
};

    char itemC;

这是一个 main()

singlyLinkedList<char> listCSLL; //Creating linked list
cout << "Create Char Single Linked List: "
    << "\nCTRL+Z for exit!" << endl;
cin >> itemC;

    while (!cin.eof())
{
    listCSLL.insertLast(itemC);
    cin >> itemC;
}
listCSLL.printSLL();

但我的打印函数打印字符的 ASCII 十进制表示

    template <class nodeT>
void singlyLinkedList<nodeT>::printSLL()
{
    nodeSLL<nodeT>* move;
    move = head;
    while (move != NULL)
    {
        cout << move->data << " ";
        move = move->link;
    }
}

我的程序可以创建 int 或 char 列表,用于打印 int 没问题,但 char 列表打印 ascii 十进制格式

【问题讨论】:

  • 请包括您正在使用的类的定义 - nodeSLL&lt;nodeT&gt;::data 的类型可能与此处相关。通常,您应该将问题简化为mcve 以使其更清晰,这样您通常会自己找到解决方案。
  • 现在可以了吗? @hnefatl
  • 你应该使用nodeT data;模板化而不是int data;
  • 感谢您帮助它现在工作。 @Wander3r 我无法回答标记解决问题

标签: c++ linked-list


【解决方案1】:

而不是 int data; 您应该模板化 nodeSLL 通过将其更改为 nodeT data; 如果不是,无论是传递 int 还是 char 或任何其他数据类型仍会导致打印 int

如果没有,传递模板根本不会影响struct nodeSLL

【讨论】:

    【解决方案2】:
    char itemC;  // This is a char
    

    您将一些输入读入char,然后将其传输到int

    int data;   
    

    您在打印功能中打印的内容

     cout << move->data << " ";
    

    因为data的数据类型是int,所以会打印成数字。

    如果您想要char,请在两个地方都使用char。如果您想要int,请在两个地方都使用int

    假设您想要char,只需将itemC 更改为char

    char itemC;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-25
      • 2017-08-11
      • 2016-06-21
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多