首页 > 解决方案 > Linked List Print() 函数打印字符的ASCII码

问题描述

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

    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 十进制格式

标签: c++linked-list

解决方案


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

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


推荐阅读