Advertisement
evcamels

spisok

Mar 28th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  method_prog_lr1
  4. //
  5. //  Created by Nikita Korvyakov on 28.03.2021.
  6. //  Copyright © 2021 Nikita Korvyakov. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <string>
  11. using namespace std;
  12. template<typename T>
  13. class List{
  14. public:
  15.     List();
  16.     ~List();
  17.     void push_back(T data);
  18.     int GetSize(){return Size;}
  19.     T& operator[](const int index);
  20.    
  21. private:
  22.     template<typename T1>
  23.     class Node{
  24.         public:
  25.         Node *pNext;
  26.         T1 data;
  27.        
  28.         Node(T1 data=T1() , Node *pNext = nullptr){
  29.             this->data = data;
  30.             this->pNext = pNext;
  31.         }
  32.     };
  33.     int Size;
  34.     Node<T> *head;
  35. };
  36.  
  37.  
  38. template<typename T>
  39. List<T>::List(){
  40.     Size = 0;
  41.     head = nullptr;
  42. }
  43.  
  44.  
  45. template<typename T>
  46. List<T>::~List(){
  47.    
  48. }
  49.  
  50.  
  51. template<typename T>
  52. void List<T>::push_back(T data){
  53.     if(head == nullptr){
  54.         head = new Node<T>(data);
  55.     }
  56.     else{
  57.         Node<T> *current = this-> head;
  58.         while (current->pNext != nullptr) {
  59.             current = current->pNext;
  60.         }
  61.         current->pNext = new Node<T>(data);
  62.     }
  63.     Size++;
  64. }
  65.  
  66.  
  67. template<typename T>
  68. T& List<T>::operator[](const int index)
  69. {
  70.     int counter = 0;
  71.     T c;
  72.     Node<T> *current = this->head;
  73.     while (current != nullptr){
  74.         if (counter == index){
  75.             c = current->data;
  76.         }
  77.         current = current->pNext;
  78.         counter++;
  79.     }
  80.     return c;
  81. }
  82.  
  83.  
  84. int main() {
  85.     srand(time(NULL));
  86.     setlocale(LC_ALL, "rus");
  87.     List<int> lst;
  88.     lst.push_back(1);
  89.     int n;
  90.     cin >> n;
  91.     for(int i=0;i<n;i++){
  92.         lst.push_back(rand()%100);
  93.     }
  94.     for(int i=0;i<lst.GetSize();i++){
  95.         cout << lst[i] << " ";
  96.     }
  97.     cout << endl;
  98.     return 0;
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement