c++ - 如何使用链表输出字符?
问题描述
#include <iostream>
using namespace std;
class FloatList
{
private:
// Declare a structure for the list
struct ListNode
{
float value;
struct ListNode *next;
};
ListNode *head; // List head pointer
public:
FloatList(); // Constructor
void appendNode(float num);
};
FloatList::FloatList()
{
head = NULL;
}
void FloatList::appendNode(float num)
{
ListNode *newNode, *nodePtr;
// Allocate a new node & store num
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
// If there are no nodes in the list
// make newNode the first node
if (!head)
head = newNode;
else // Otherwise, insert newNode at end
{
// Initialize nodePtr to head of list
nodePtr = head;
// Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node
nodePtr->next = newNode;
} cout << num << " has been APPENDED!" << endl;
}
int main()
{
FloatList list;
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
}
说明:将上述带有附加操作的链表 ADT 转换为链表模板,使其能够处理不同数据类型的数据。程序完成后,将主程序更改为以下内容:
int main()
{
FloatList list;
list.appendNode('a');
list.appendNode('b');
list.appendNode('c');
cout << "Successful Append!" << endl;
}
我能够打印浮点数 (2.5) (7.9) 和 (12.6),但我无法编辑程序以打印“a”、“b”和“c”
这是预期的输出:
a has been APPENDED!
b has been APPENDED!
c has been APPENDED!
Successful Append!
解决方案
如果您想让它适用于字符串类型或字符类型,则必须将链接列表转换为模板类。查看您对节点的定义:
struct ListNode
{
float value;
struct ListNode *next;
};
您将 float 定义为值,那么您如何期望它包含 char 类型?我更改了您的链表,所以现在它可以采用您想要的任何类型:
#include <iostream>
using namespace std;
template <typename T>
class FloatList
{
private:
// Declare a structure for the list
struct ListNode
{
T value;
struct ListNode* next;
};
ListNode* head; // List head pointer
public:
FloatList()
{
head = NULL;
}// Constructor
void appendNode(T num)
{
ListNode* newNode, * nodePtr;
// Allocate a new node & store num
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
// If there are no nodes in the list
// make newNode the first node
if (!head)
head = newNode;
else // Otherwise, insert newNode at end
{
// Initialize nodePtr to head of list
nodePtr = head;
// Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node
nodePtr->next = newNode;
} cout << num << " has been APPENDED!" << endl;
}
};
int main()
{
FloatList<float> list;
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
FloatList<char> char_list;
char_list.appendNode('c');
char_list.appendNode('d');
char_list.appendNode('e');
}
由于您的列表现在是模板类,因此您必须指定模板类型是什么:
FloatList<float> list;
解决方案非常简单,您所要做的就是更改类定义中的两件事:a)告诉编译器这个类是模板化的
template <typename T>
class FloatList
b)将所有浮点数交换为 T (我们想要的类型):
float value;
T value;
void appendNode(float num)
void appendNode(T num)
推荐阅读
- lotus-notes - 使用 Lotusscript 填充列表框选项
- python - Pandas:如何使用新的可空字符串和 int 类型构造具有指定类型的 DataFrame?
- r - Databricks 将 Rdata 文件保存到 AWS S3 存储桶
- c# - 如何将参数传递给在 Xamarin.Forms XAML 绑定上下文元素中声明的视图模型构造函数?
- android - 活动 A 没有移动到活动 B
- excel - 循环VBA内的索引匹配函数错误
- ansible - 如何在 Ansible 剧本中运行 AWS `amplify init`?
- javascript - 选择表格中的附加信息
- kubernetes-helm - 如何解决错误:验证:使用 microk8s 在 Mac 上需要 chart.metadata
- c# - 如何将 DbSet.Find() 与具有抽象类作为键的实体一起使用?