Advertisement
Guest User

Untitled

a guest
May 24th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Node {
  4.   int value;
  5.   Node *next;
  6.  
  7.   Node(): value(0), next(NULL) {};
  8.   Node(int _value, Node *_next): value(_value), next(_next) {};
  9. };
  10.  
  11. class List {
  12.   Node *m_head;
  13.   int m_size;
  14.  
  15. public:
  16.   // конструктор по-умолчанию
  17.   List(): m_head(NULL), m_size(0) {};
  18.   // конструктор копирования
  19.   List(const List &list);
  20.   // с параметрами
  21.   List(int size);
  22.  
  23.   // деструктор
  24.   ~List();
  25.  
  26.   // Очистка списка
  27.   void clear();
  28.  
  29.   // Получить элемент списка
  30.   Node *get(int index);
  31.  
  32.   // Скопировать элементы списка в другой список
  33.   bool assign_to(List* list) const;
  34.  
  35.   // Вставка до/после выбранного элемента
  36.   bool insert_after(int index, int value);
  37.   bool insert_before(int index, int value);
  38.  
  39.   // Замена элемента
  40.   bool replace(int index, int value);
  41.  
  42.   // Вставка в конец/начало
  43.   bool push_back(int value);
  44.   bool push_front(int value);
  45.  
  46.   // Вывод списка на экран
  47.   void display();
  48.  
  49.   // Размер списка
  50.   int size() const { return m_size; }
  51. };
  52.  
  53. List::List(const List &list): List() {
  54.   list.assign_to(this);
  55. }
  56.  
  57. List::List(int size): List() {
  58.   while (size-- > 0) {
  59.     this->push_back(0);
  60.   }
  61. }
  62.  
  63. List::~List() {
  64.   clear();
  65. }
  66.  
  67. Node *List::get(int index) {
  68.   if (index < 0 || index >= m_size)
  69.     return NULL;
  70.  
  71.   Node *node = m_head;
  72.   while (index-- > 0) {
  73.     if (!node)
  74.       return NULL;
  75.     node = node->next;
  76.   }
  77.  
  78.   return node;
  79. }
  80.  
  81. void List::clear() {
  82.   m_size = 0;
  83.   while (m_head) {
  84.     Node *node = m_head;
  85.     m_head = m_head->next;
  86.     delete node;
  87.   }
  88. }
  89.  
  90. bool List::assign_to(List* list) const {
  91.   if (!list)
  92.     return false;
  93.  
  94.   list->clear();
  95.   for(Node *node = m_head; node; node = node->next) {
  96.     list->push_back(node->value);
  97.   }
  98.   return true;
  99. }
  100.  
  101. bool List::insert_after(int index, int value) {
  102.   if (index == -1) {
  103.     m_head = new Node(value, NULL);
  104.     ++m_size;
  105.     return true;
  106.   }
  107.  
  108.   Node *node = this->get(index);
  109.   if (!node)
  110.     return false;
  111.  
  112.   Node *newNode = new Node(value, node->next);
  113.   node->next = newNode;
  114.  
  115.   ++m_size;
  116.   return true;
  117. }
  118.  
  119. bool List::insert_before(int index, int value) {
  120.   if (index == 0) {
  121.     Node *newNode = new Node(value, m_head);
  122.     m_head = newNode;
  123.     ++m_size;
  124.     return true;
  125.   }
  126.  
  127.   return this->insert_after(index - 1, value);
  128. }
  129.  
  130. bool List::push_back(int value) {
  131.   return this->insert_after(m_size - 1, value);
  132. }
  133.  
  134. bool List::push_front(int value) {
  135.   return this->insert_before(0, value);
  136. }
  137.  
  138. bool List::replace(int index, int value) {
  139.   Node *node = this->get(index);
  140.   if (!node)
  141.     return false;
  142.  
  143.   node->value = value;
  144.   return true;
  145. }
  146.  
  147. void List::display() {
  148.   Node *node = m_head;
  149.   while (node) {
  150.     std::cout << node->value << " ";
  151.     node = node->next;
  152.   }
  153.   std::cout << std::endl;
  154. }
  155.  
  156. int main() {
  157.   // Создаем с четырьмя нулями
  158.   List list(4);
  159.   // Добавляем 10 цифр
  160.   for (int i = 0; i < 10; ++i)
  161.     list.push_back(i);
  162.   list.display();
  163.  
  164.   // Тест конструктора копирования:
  165.   List list2(list);
  166.   list2.display();
  167.   return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement