如何允许用户输入链表c ++中的数据点
How to allow user input for a data point in a linked list c++
我相信我在这里有一个相当业余的问题,过去几天我一直在为链表绞尽脑汁(最终目标是建立一个多列表),但现在我正在使用我的"addNode" 函数,我试图弄清楚如何让用户输入数据点而不是将点硬编码到程序中。我的 addNode 函数如下所示:
void List::AddNode(int addData) {
nodePtr n = new node;
n->classpointer = NULL;
n->class_number = addData;
cout << "What value would you like to add?" << endl;
//I believe that right here is where I need to figure out
//how to allow the user to add what they want the data point to be.
if(head != NULL) {
curr = head;
while (curr->classpointer != NULL) {
curr = curr->classpointer;
}
curr->classpointer = n;
}
else {
head = n;
}
}
总而言之,我正在寻找有关如何允许用户输入此数据点的指导。如果您需要查看我提供的更多代码,请告诉我,在此先感谢大家。
您想为这样的事情使用标准输入。就像您使用 "cout" 将数据发送到控制台一样,您使用 "cin" 从控制台读取数据。像这样:
int x;
cin >> x;
程序将在这里等待,直到您输入内容。
关于你的方法的注释,如果你使用尾指针来跟踪链表的末尾,你可以执行恒定时间 addNode 而不是像你现在做的那样 O(N)。这意味着将私人成员 "tail" 添加到您的 class.
我相信我在这里有一个相当业余的问题,过去几天我一直在为链表绞尽脑汁(最终目标是建立一个多列表),但现在我正在使用我的"addNode" 函数,我试图弄清楚如何让用户输入数据点而不是将点硬编码到程序中。我的 addNode 函数如下所示:
void List::AddNode(int addData) {
nodePtr n = new node;
n->classpointer = NULL;
n->class_number = addData;
cout << "What value would you like to add?" << endl;
//I believe that right here is where I need to figure out
//how to allow the user to add what they want the data point to be.
if(head != NULL) {
curr = head;
while (curr->classpointer != NULL) {
curr = curr->classpointer;
}
curr->classpointer = n;
}
else {
head = n;
}
}
总而言之,我正在寻找有关如何允许用户输入此数据点的指导。如果您需要查看我提供的更多代码,请告诉我,在此先感谢大家。
您想为这样的事情使用标准输入。就像您使用 "cout" 将数据发送到控制台一样,您使用 "cin" 从控制台读取数据。像这样:
int x;
cin >> x;
程序将在这里等待,直到您输入内容。
关于你的方法的注释,如果你使用尾指针来跟踪链表的末尾,你可以执行恒定时间 addNode 而不是像你现在做的那样 O(N)。这意味着将私人成员 "tail" 添加到您的 class.