Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Node {
- int value;
- Node *next;
- Node(): value(0), next(NULL) {};
- Node(int _value, Node *_next): value(_value), next(_next) {};
- };
- class List {
- Node *m_head;
- int m_size;
- public:
- // конструктор по-умолчанию
- List(): m_head(NULL), m_size(0) {};
- // конструктор копирования
- List(const List &list);
- // с параметрами
- List(int size);
- // деструктор
- ~List();
- // Очистка списка
- void clear();
- // Получить элемент списка
- Node *get(int index);
- // Скопировать элементы списка в другой список
- bool assign_to(List* list) const;
- // Вставка до/после выбранного элемента
- bool insert_after(int index, int value);
- bool insert_before(int index, int value);
- // Замена элемента
- bool replace(int index, int value);
- // Вставка в конец/начало
- bool push_back(int value);
- bool push_front(int value);
- // Вывод списка на экран
- void display();
- // Размер списка
- int size() const { return m_size; }
- };
- List::List(const List &list): List() {
- list.assign_to(this);
- }
- List::List(int size): List() {
- while (size-- > 0) {
- this->push_back(0);
- }
- }
- List::~List() {
- clear();
- }
- Node *List::get(int index) {
- if (index < 0 || index >= m_size)
- return NULL;
- Node *node = m_head;
- while (index-- > 0) {
- if (!node)
- return NULL;
- node = node->next;
- }
- return node;
- }
- void List::clear() {
- m_size = 0;
- while (m_head) {
- Node *node = m_head;
- m_head = m_head->next;
- delete node;
- }
- }
- bool List::assign_to(List* list) const {
- if (!list)
- return false;
- list->clear();
- for(Node *node = m_head; node; node = node->next) {
- list->push_back(node->value);
- }
- return true;
- }
- bool List::insert_after(int index, int value) {
- if (index == -1) {
- m_head = new Node(value, NULL);
- ++m_size;
- return true;
- }
- Node *node = this->get(index);
- if (!node)
- return false;
- Node *newNode = new Node(value, node->next);
- node->next = newNode;
- ++m_size;
- return true;
- }
- bool List::insert_before(int index, int value) {
- if (index == 0) {
- Node *newNode = new Node(value, m_head);
- m_head = newNode;
- ++m_size;
- return true;
- }
- return this->insert_after(index - 1, value);
- }
- bool List::push_back(int value) {
- return this->insert_after(m_size - 1, value);
- }
- bool List::push_front(int value) {
- return this->insert_before(0, value);
- }
- bool List::replace(int index, int value) {
- Node *node = this->get(index);
- if (!node)
- return false;
- node->value = value;
- return true;
- }
- void List::display() {
- Node *node = m_head;
- while (node) {
- std::cout << node->value << " ";
- node = node->next;
- }
- std::cout << std::endl;
- }
- int main() {
- // Создаем с четырьмя нулями
- List list(4);
- // Добавляем 10 цифр
- for (int i = 0; i < 10; ++i)
- list.push_back(i);
- list.display();
- // Тест конструктора копирования:
- List list2(list);
- list2.display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement