【问题标题】:Add integer to each item of unordered linked list向无序列表的每一项添加整数
【发布时间】:2021-03-21 01:35:03
【问题描述】:

我想编写一个函数,将整数(作为参数传递给函数)添加到无序列表中的每个项目。这是完整的程序。

#include <iostream>
using namespace std;
//creates a node class
class Node {
    //defines data, and next as a pointer.
private:
    int data; //data in the beginning node
    Node *next; //pointer to the next node

public:
Node(int initdata) {
    data = initdata; //the initialized data is set as the head
    next = NULL; //the next node is set as NULL, as there is no next node yet.
}

int getData() { //function that return data of a given node.
    return data;
}

Node *getNext() { // pointer that gets the next node
    return next;
}

void setData(int newData) { // sets data in node
    data = newData;
}

void setNext(Node *newnext) {
    next = newnext;
}
};

// creates unorderedlist that points to the head of the linked list
class UnorderedList {
public:
Node *head;

UnorderedList() { // makes the head node equal to null
    head = NULL;
}

bool isEmpty() { // the head node is empty if it is null
    return head == NULL;
}

void add(int item) { //cerates a "temp" pointer that adds the new node to the head of the list
    Node *temp = new Node(item);
    temp->setNext(head);
    head = temp;
}

int size() { //cereates a "current" pointer that iterates through the list until it reaches null
    Node *current = head;
    int count = 0;
    while (current != NULL) {
        count++;
        current = current->getNext();
    }

    return count;
}

// creates "current" pointer that iterates through the list
// untli it finds the item being searched for, and returns a boolean value

bool search(int item) {
    Node *current = head;
    while (current != NULL) {
        if (current->getData() == item) {
            return true;
        } else {
            current = current->getNext();
        }
    }
    return false;
}

void addInteger(int item){
    Node *current = head;
    while (current != NULL) {
        current->getData() = current->getData() + item;
    }
}

// uses current and previous pointer to iterate through the lists
// finds the items that is searched for, and removes it

void remove(int item) {
    Node *current = head;
    Node *previous = NULL;
    bool found = false;
    while (!found) {
        if (current->getData() == item) {
            found = true;
        } else {
            previous = current;
            current = current->getNext();
        }
    }
    if (previous == NULL) {
        head = current->getNext();
    } else {
        previous->setNext(current->getNext());
    }
}

friend ostream& operator<<(ostream& os, const UnorderedList& ol);
};

ostream& operator<<(ostream& os, const UnorderedList& ol) {
Node *current = ol.head;
while (current != NULL) {
    os<<current->getData()<<endl;
    current = current->getNext();
}
return os;
}

int main() {
UnorderedList mylist;
mylist.add(1);
mylist.add(2);
mylist.add(3);
mylist.add(4);
mylist.add(5);
mylist.add(6);

cout<<"MY LIST: "<<endl<<mylist;

mylist.addInteger(5);
cout<<"=========================================================\n";
cout<<"After adding 5 to each element, the list now is\n";
cout<<"MY LIST: "<<endl<<mylist;
return 0;
}

现在程序在上面程序的以下函数中显示了一个关于赋值操作的错误。

void addInteger(int item){
    Node *current = head;
    while (current != NULL) {
        current->getData() = current->getData() + item;
    }
}

如何为链表的每个元素添加一个数字? 任何帮助表示赞赏。

【问题讨论】:

  • 如果你想让current-&gt;getData() = current-&gt;getData() + item;工作int getData()需要int&amp; getData()

标签: c++ linked-list operator-overloading


【解决方案1】:

您可能想要以下内容:

current->setData(current->getData() + item);

请注意,现在您在左侧检索返回值,然后尝试分配给它。大概这就是你的编译器告诉你的。

【讨论】:

  • 感谢您的更正,但我发现程序正在以相反的顺序打印列表。我该如何纠正?
  • 您可以对operator&lt;&lt; 进行简单的更正,但这确实是一个单独的问题,与您最初提出的问题没有真正的关系。如果你打开一个,我相信人们会帮助你。如果需要,请随时在此处的评论中链接到它。
  • 你能告诉我如何更改 add() 函数,以便它在列表末尾插入一个元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
  • 2022-08-11
  • 1970-01-01
  • 2020-02-27
  • 1970-01-01
相关资源
最近更新 更多