ВО
Виктория Окшина

Составить программу на C++.

Составить программу, которая формирует список L включив в него по одному разу элементы, которые входят в один из списков L1 и L2, но в то же время не входит в другой из них.

Наталья Пахомова
Наталья Пахомова

#include
#include
#include

template
class list {

struct node {
T data;
node * next;

node(T const& d = T(), node * n = 0) : data(d), next(n) {}
};

public:
typedef T value_type;

struct iterator : public std::iterator {

T & operator *() const {
return ptr->data;
}

iterator & operator ++() {
increment();
return *this;
}

iterator operator ++(int) {
iterator copy(*this);
increment();
return copy;
}

bool operator ==(iterator const& rhs) const {
return ptr == rhs.ptr;
}

bool operator !=(iterator const& rhs) const {
return !(*this == rhs);
}

iterator(node * n = 0) : ptr(n) {}

private:
void increment() {
ptr = ptr->next;
}

node * ptr;
};

iterator begin() {
return iterator(head);
}

iterator end() {
return iterator(0);
}

void push_front(T const& data) {
if (head) {
node * newNode = new node(data, head);
head = newNode;
} else {
head = new node(data);
}
}

void unique() {
if (head) {
node * p = head;
while (p->next) {
if (p->data == p->next->data) {
node * tmp = p->next->next;
delete p->next;
p->next = tmp;
} else {
p = p->next;
}
}
}
}

void clear() {
while (head) {
node * tmp = head;
head = head->next;
delete tmp;
}
head = 0;
}

void sort() {
if (head && head->next) {
for (node * p = head; p; p = p->next) {
for (node * q = p->next; q; q = q->next) {
if (q->data < p->data) {
std::swap(q->data, p->data);
}
}
}
}
}

list() : head(0) {}

~list() {
clear();
}

private:
node * head;
};

template
std::ostream & operator <<(std::ostream & stream, list /*const*/ & l) {
for (auto itr = l.begin(); itr != l.end(); ++itr) {
stream << *itr << "->";
}
stream << "x";
return stream;
}

template
std::istream & operator >>(std::istream & stream, list & l) {
T t;
while (stream >> t) {
l.push_front(t);
}
return stream;
}

int main() {
std::ofstream fout("out.txt", std::ios::out);

list l1;
std::ifstream fin1("list1.txt", std::ios::in);
fin1 >> l1;
fout << "list number 1:\t" << l1 << std::endl;

list l2;
std::ifstream fin2("list2.txt", std::ios::in);
fin2 >> l2;
fout << "list number 2:\t" << l2 << std::endl;

l1.sort();
l1.unique();
l2.sort();
l2.unique();

list l;
std::set_difference(l1.begin(), l1.end(), l2.begin(), l2.end(), std::front_inserter(l));
fout << "unique l1/l2:\t" << l << std::endl;

l.clear();
std::set_difference(l2.begin(), l2.end(), l1.begin(), l1.end(), std::front_inserter(l));
fout << "unique l2/l1:\t" << l << std::endl;
}

Похожие вопросы
Помогите, пожалуйста, составить программу в Паскале
Помогите составить программу на c++
Помогите программу составить на C++
Программа в C или C++
Помогите составить программу Pascal для вычесления ->
Составить программу на Ассемблере для МП i8080
Составить Pascal программу
как в C++ составить программу на задачу
Помогите написать код программы на C++, пожалуйста :)
c++ Составить программу и трассировку программы вычисления суммы элементов последовательности.