Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // method_prog_lr1
- //
- // Created by Nikita Korvyakov on 28.03.2021.
- // Copyright © 2021 Nikita Korvyakov. All rights reserved.
- //
- #include <iostream>
- #include <string>
- using namespace std;
- template<typename T>
- class List{
- public:
- List();
- ~List();
- void push_back(T data);
- int GetSize(){return Size;}
- T& operator[](const int index);
- private:
- template<typename T1>
- class Node{
- public:
- Node *pNext;
- T1 data;
- Node(T1 data=T1() , Node *pNext = nullptr){
- this->data = data;
- this->pNext = pNext;
- }
- };
- int Size;
- Node<T> *head;
- };
- template<typename T>
- List<T>::List(){
- Size = 0;
- head = nullptr;
- }
- template<typename T>
- List<T>::~List(){
- }
- template<typename T>
- void List<T>::push_back(T data){
- if(head == nullptr){
- head = new Node<T>(data);
- }
- else{
- Node<T> *current = this-> head;
- while (current->pNext != nullptr) {
- current = current->pNext;
- }
- current->pNext = new Node<T>(data);
- }
- Size++;
- }
- template<typename T>
- T& List<T>::operator[](const int index)
- {
- int counter = 0;
- T c;
- Node<T> *current = this->head;
- while (current != nullptr){
- if (counter == index){
- c = current->data;
- }
- current = current->pNext;
- counter++;
- }
- return c;
- }
- int main() {
- srand(time(NULL));
- setlocale(LC_ALL, "rus");
- List<int> lst;
- lst.push_back(1);
- int n;
- cin >> n;
- for(int i=0;i<n;i++){
- lst.push_back(rand()%100);
- }
- for(int i=0;i<lst.GetSize();i++){
- cout << lst[i] << " ";
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement