如何使用链表输出字符?
How To Output Characters Using Linked Lists?
#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);
}
INSTRUCTIONS : 将上述带追加操作的链表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!
如果你想让它适用于字符串类型或字符类型,你必须将你的链接列表转换为模板化 class。看你对节点的定义:
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');
}
由于您的列表现在是模板化的 class,您必须指定什么是模板化类型:
FloatList<float> list;
解决方案非常简单,您只需更改 class 定义中的两点:
a) 告诉编译器这个 class 是模板化的
template <typename T>
class FloatList
b) 将所有浮点数交换为 T(我们想要的类型):
float value;
T value;
void appendNode(float num)
void appendNode(T num)
#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);
}
INSTRUCTIONS : 将上述带追加操作的链表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!
如果你想让它适用于字符串类型或字符类型,你必须将你的链接列表转换为模板化 class。看你对节点的定义:
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');
}
由于您的列表现在是模板化的 class,您必须指定什么是模板化类型:
FloatList<float> list;
解决方案非常简单,您只需更改 class 定义中的两点: a) 告诉编译器这个 class 是模板化的
template <typename T>
class FloatList
b) 将所有浮点数交换为 T(我们想要的类型):
float value;
T value;
void appendNode(float num)
void appendNode(T num)