Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename T>//шаблонный класс параметризуемый типом Т
  5. class List // явл-ся шаблонным и парамитризуется типом Т
  6. {
  7.     struct Element
  8.     {
  9.         T data;
  10.         Element* next;
  11.     };
  12.     Element *head;
  13.  
  14.     // Копирующий конструктор
  15.     List(const List& other)
  16.     {
  17.     }
  18.  
  19.     // Оператор копирующего присваивания
  20.     List& operator=(const List& other)
  21.     {
  22.     }
  23.  
  24. public:
  25.     List () // конструктор (по умолчанию списоок пустой)
  26.     {
  27.         head=NULL;
  28.     }
  29.     ~List () // Деструктор
  30.     {
  31.         while (head!=NULL)
  32.         {
  33.             delHead();
  34.         }
  35.     }
  36.     void add (T newel) // добавить в начало элемент
  37.     {
  38.         Element *e;
  39.         e=new Element();//создали новый !пустой! элемент
  40.         e->data=newel;
  41.         e->next=head;
  42.         head=e;
  43.     }
  44.  
  45.     void delHead()//удаление первого
  46.     {
  47.         if (head!=NULL)
  48.         {
  49.             Element* e=head;
  50.             head=head->next;
  51.             delete e;
  52.         }
  53.     }
  54.  
  55.     void addTail (T newel)
  56.     {
  57.         if(head==NULL)
  58.         {
  59.             add(newel);
  60.         }
  61.         else
  62.         {
  63.             Element *e;
  64.             e=new Element();
  65.             e->data=newel;
  66.             e->next=NULL;
  67.             Element *k=head;
  68.             while (k->next!=NULL)
  69.             {
  70.                 k=k->next;
  71.             }
  72.             k->next=e;
  73.         }      
  74.     }
  75.  
  76.     void delTail()
  77.     {
  78.         if (head!=NULL)
  79.         {
  80.             if (head->next==NULL)
  81.             {
  82.                 delHead();
  83.             }
  84.             else
  85.             {
  86.                 Element *pred=head;
  87.                 Element *k=head->next;
  88.                 while (k->next!=NULL)
  89.                 {
  90.                     pred=k;
  91.                     k=k->next;
  92.                 }
  93.                 pred->next=NULL;
  94.                 delete k;
  95.             }
  96.         }
  97.     }
  98.  
  99.  
  100.     void print ()//для печати
  101.     {
  102.         Element* k=head;
  103.         cout << '[';
  104.         while (k!=NULL)
  105.         {
  106.             cout << k->data <<' ';
  107.             k=k->next;
  108.         }
  109.         cout << ']' << endl;
  110.     }
  111.  
  112. // осталось добавить итератор и проверить его работу
  113.     class Iterator
  114.     {
  115.         friend List<T>; // даем классу списка использовать приватный конструктор
  116.  
  117.         Element *cur;
  118.  
  119.         Iterator(Element *start)
  120.         {
  121.             cur = start;
  122.         }
  123.  
  124.     public:
  125.         T operator*() { return cur -> data; }
  126.         T* operator->() { return &(cur -> data); }
  127.         Iterator& operator++() {
  128.             cur = cur -> next;
  129.             return *this;
  130.         }
  131.         bool operator!=(const Iterator& other) {
  132.             return cur != other.cur;
  133.         }
  134.         bool hasNext() { return cur != NULL ? (cur -> next != NULL) : NULL; }
  135.     };
  136.  
  137.     Iterator start() const
  138.     {
  139.         return Iterator(head);
  140.     }
  141.  
  142.     Iterator end() const
  143.     {
  144.         return Iterator(NULL);
  145.     }
  146.  
  147. };
  148. int main()
  149. {
  150.     List<int> listik;
  151.     listik.print();
  152.     listik.add(123);
  153.     listik.add(101);
  154.     listik.print();
  155.     listik.delHead();
  156.     listik.print();
  157.     listik.addTail(432);
  158.     listik.addTail(10);
  159.     listik.print();
  160.     listik.delTail();
  161.     listik.print();
  162.  
  163.     //List<int> a;
  164.     //List<int> b(a); //копирующий-конструктор
  165.     //a = b; // копирующее присваивание
  166.    
  167.     cout << "[";
  168.     for(List<int>::Iterator it = listik.start(); it != listik.end(); ++it) // использование итератора
  169.     {
  170.         cout << *it;
  171.         if (it.hasNext()) cout << ", ";
  172.     }
  173.     cout << "]" << endl;
  174.  
  175. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement