c++ - 修改链表主函数代码,用户将在其中输入节点的索引和数据,并带有适当的消息
问题描述
我有一个链接列表,用户将在其中输入带有适当消息的节点的索引和数据....
我知道这是一项非常简单的任务,但我很困惑如何去做。
我想要的是您编辑我的代码,以便用户可以输入他/她想要输入数据的索引,并且他/她能够输入他/她的数据..
如果有人回复,我会非常满意。
#include <iostream>
using namespace std;
class Node {
public:
double data; // data
Node* next; // pointer to next
};
class List {
public:
List(void) { head = NULL; } // constructor
~List(void); // destructor
bool IsEmpty() { return head == NULL; }
Node* InsertNode(int index, double x);
int FindNode(double x);
int DeleteNode(double x);
void DisplayList(void);
private:
Node* head;
};
Node* List::InsertNode(int index, double x)
{
if (index < 0)
return NULL;
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
//Try to locate index'th node. If it doesn't exist, return NULL
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL)
return NULL;
Node* newNode = new Node;
newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
int List::FindNode(double x)
{
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
currNode = currNode->next;
currIndex++;
}
if (currNode)
return currIndex;
return 0;
}
int List::DeleteNode(double x)
{
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
void List::DisplayList()
{
int num = 0;
Node* currNode = head;
while (currNode != NULL) {
cout << currNode->data << endl;
currNode = currNode->next;
num++;
}
cout << "Number of nodes in the list: " << num << endl;
}
List::~List(void)
{
Node* currNode = head;
Node* nextNode = NULL;
while (currNode != NULL) {
nextNode = currNode->next;
delete currNode; // destroy the current node
currNode = nextNode;
}
}
int main(void)
{
List list;
list.InsertNode(0, 7.0); // successful
list.InsertNode(1, 5.0); // successful
list.InsertNode(-1, 5.0); // unsuccessful
list.InsertNode(0, 6.0); // successful
list.InsertNode(8, 4.0); // unsuccessful
// print all the elements
list.DisplayList();
if (list.FindNode(5.0) > 0)
cout << "5.0 found" << endl;
else
cout << "5.0 not found" << endl;
if (list.FindNode(4.5) > 0)
cout << "4.5 found" << endl;
else
cout << "4.5 not found" << endl;
list.DeleteNode(7.0);
list.DisplayList();
return 0;
}
解决方案
只需读入输入并调用列表对象上的方法。你对它有什么困惑?另外,请尝试改掉使用命名空间 std 的习惯!将此代码附加到主函数。
int index;
double data;
std::cout << "Enter the index: ";
std::cin >> index;
std::cout << "Enter data: ";
std::cin >> data;
list.InsertNode(index, data);
list.DisplayList();
声明临时变量index
并data
保存来自控制台的输入。打印到控制台,要求用户使用 输入索引和数据std::cout
,它位于要输入节点的位置。std::cin
可用于从控制台读取输入并将其存储在变量中。链表的节点包含一个类型的数据字段, double
它是链表的实际数据。使用您的list
对象,您可以InsertNode()
连同此索引和数据一起调用该方法。
推荐阅读
- angular - 将输入设置为空值的角度问题
- javascript - stencil js,将数组作为自定义元素中的属性值传递
- html - 有条件地将 HTML 列表一分为二
- c# - 从表单 asp.net core 获取数据
- wordpress - wordpress 搜索阿拉伯语的同义词
- google-calendar-api - 我可以提高谷歌日历 api 使用限制以通过一个服务帐户创建大量日历吗
- c++ - 如何使用 glVertexAttribPointer 将两个单独的数组附加到着色器?
- c# - 在 Unity 中有一个刚体跟随(匹配)游戏对象的位置
- python - 无需从 Python 应用程序编译即可从 C/C++ 代码获取 AST
- swift - 用户登录应用程序后如何在视图上使用导航控制器?