С клавиатуры вводятся строки, содержащие целые числа, записанные через пробел. Организовать из вводимых чисел однонаправленный список, упорядоченный по абсолютной величине числа. В программе предусмотреть возможность добавления в список нового элемента и удаления элемента, равного введенному с клавиатуры. Вывести на печать список и проверить функции добавления и удаления элементов списка.
Помогите пожалуйста!!!
C/C++
Программирование на C++
#include <iostream>
#include <cstdlib>
using namespace std;
struct Node {
int data;
Node* next;
};
class List {
private:
Node* head;
public:
List() {
head = NULL;
}
void add(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
return;
}
if (abs(head->data) > abs(value)) {
newNode->next = head;
head = newNode;
return;
}
Node* current = head;
while (current->next != NULL && abs(current->next->data) < abs(value)) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
void remove(int value) {
if (head == NULL) {
return;
}
if (head->data == value) {
Node* oldHead = head;
head = head->next;
delete oldHead;
return;
}
Node* current = head;
while (current->next != NULL && current->next->data != value) {
current = current->next;
}
if (current->next != NULL) {
Node* oldNode = current->next;
current->next = current->next->next;
delete oldNode;
}
}
void print() {
Node* current = head;
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
};
int main() {
List list;
int value, operation;
do {
cout << "1 - add, 2 - remove, 3 - print, 0 - exit: ";
cin >> operation;
switch (operation) {
case 1:
cout << "Enter value: ";
cin >> value;
list.add(value);
break;
case 2:
cout << "Enter value: ";
cin >> value;
list.remove(value);
break;
case 3:
list.print();
break;
case 0:
break;
default:
cout << "Invalid operation" << endl;
}
} while (operation != 0);
return 0;
}
#include <cstdlib>
using namespace std;
struct Node {
int data;
Node* next;
};
class List {
private:
Node* head;
public:
List() {
head = NULL;
}
void add(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
return;
}
if (abs(head->data) > abs(value)) {
newNode->next = head;
head = newNode;
return;
}
Node* current = head;
while (current->next != NULL && abs(current->next->data) < abs(value)) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
void remove(int value) {
if (head == NULL) {
return;
}
if (head->data == value) {
Node* oldHead = head;
head = head->next;
delete oldHead;
return;
}
Node* current = head;
while (current->next != NULL && current->next->data != value) {
current = current->next;
}
if (current->next != NULL) {
Node* oldNode = current->next;
current->next = current->next->next;
delete oldNode;
}
}
void print() {
Node* current = head;
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
};
int main() {
List list;
int value, operation;
do {
cout << "1 - add, 2 - remove, 3 - print, 0 - exit: ";
cin >> operation;
switch (operation) {
case 1:
cout << "Enter value: ";
cin >> value;
list.add(value);
break;
case 2:
cout << "Enter value: ";
cin >> value;
list.remove(value);
break;
case 3:
list.print();
break;
case 0:
break;
default:
cout << "Invalid operation" << endl;
}
} while (operation != 0);
return 0;
}
#include
#include
#include
#include
#include
#include
using namespace std;
class List {
public:
List(const string& sequence) {
istringstream iss(sequence);
int value;
while (iss >> value) list.push_front(value);
}
void add(const int value) {
list.push_front(value);
}
void erase(const int value) {
list.remove(value);
}
void abs_sort() {
auto compare = [](int a, int b) { return abs(a) < abs(b); };
list.sort(compare);
}
void show()const {
for (auto item : list) cout
Николай Дмитрив

Похожие вопросы
- Программирование на C++
- Программирование на C++
- Помоги написать лабу по программированию на c++
- Помогите решить задачу по программированию на C++
- Программирование на C. Помогите бездарю
- На каком языке программирования написан C (Си)?
- Программирование на c++
- Программирование на C++
- Помогите с программированием на C++
- Задание по программированию язык C/СИ