Я хочу написать функцию, которая добавляет целое число (переданное в качестве аргумента функции) к каждому элементу в неупорядоченном связанном списке. Вот полная программа.
#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->setData(current->getData() + item);
Обратите внимание, что теперь вы получаете возвращаемое значение в левой части, а затем пытаетесь присвоить ему значение. Это то, что, по-видимому, говорит вам ваш компилятор.
Спасибо за поправку, но я обнаружил, что программа печатает список в обратном порядке. Как я могу это исправить?
Есть простое исправление, которое вы можете внести в свой operator<<
, но это действительно отдельный вопрос, на самом деле не связанный с тем, который вы изначально задали. Если вы откроете один, я уверен, что люди помогут вам. Не стесняйтесь ссылаться на него в комментарии здесь, если хотите.
Не подскажете, как внести изменения в функцию add(), чтобы она вставляла элемент в конец списка.
Если вы хотите
current->getData() = current->getData() + item;
работатьint getData()
нужно бытьint& getData()