首页 > 解决方案 > 修改链表主函数代码,用户将在其中输入节点的索引和数据,并带有适当的消息

问题描述

我有一个链接列表,用户将在其中输入带有适当消息的节点的索引和数据....

我知道这是一项非常简单的任务,但我很困惑如何去做。

我想要的是您编辑我的代码,以便用户可以输入他/她想要输入数据的索引,并且他/她能够输入他/她的数据..

如果有人回复,我会非常满意。

#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;
}

标签: c++

解决方案


只需读入输入并调用列表对象上的方法。你对它有什么困惑?另外,请尝试改掉使用命名空间 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();

声明临时变量indexdata保存来自控制台的输入。打印到控制台,要求用户使用 输入索引和数据std::cout,它位于要输入节点的位置。std::cin可用于从控制台读取输入并将其存储在变量中。链表的节点包含一个类型的数据字段, double它是链表的实际数据。使用您的list对象,您可以InsertNode()连同此索引和数据一起调用该方法。


推荐阅读