Abilvap

structers one linked list

Apr 21st, 2021
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1.  laba spiski
  2.  
  3. #include<string>
  4. #include <iostream>
  5. #include <iomanip>
  6. using namespace std;
  7. struct info
  8. {
  9.    int year=0; string name="" ; int  diameter=0, frequency=0;
  10.    info():year(0), name(""), diameter(0), frequency(0) { }
  11.   /* info(info* data) :year(data->year), name(data->name),
  12.        diameter(data->diameter), frequency(data->frequency)*/
  13.  info(info* data) {
  14.        year = data->year;
  15.        name = data->name;
  16.        diameter = data->diameter;
  17.        frequency = data->frequency;  
  18.    }
  19.    info(int year, string name, int  diameter,int frequency) {
  20.        this->year =year;
  21.        this->name = name;
  22.        this->diameter = diameter;
  23.        this->frequency = frequency;
  24.     }
  25. };
  26. struct List {
  27.    info listInfo;
  28.    List* next;
  29.    List* head;
  30.    int size;
  31.    List(): head(nullptr), size(0), next(0){}
  32.    List(info data, List* next = nullptr){
  33.        this->listInfo.year = data.year;
  34.        this->listInfo.name = data.name;
  35.        this->listInfo.diameter = data.diameter;
  36.        this->listInfo.frequency = data.frequency;
  37.        this->next = next;
  38.    }//?
  39.    info& operator [](const int index) {
  40.        List* current(head);
  41.        int counter = 0;
  42.        while (current != nullptr)
  43.        {
  44.            if (counter == index)
  45.            {
  46.                return current->listInfo;
  47.            }
  48.            current = current->next;
  49.            counter++;
  50.        }
  51.    }
  52.   void operator =(const List &list){
  53.       this->listInfo = (list.listInfo);
  54.       // return *this;
  55.    }
  56.    void PushBack(info data){
  57.        if (head == nullptr) {
  58.            head = new List;
  59.            head->listInfo = new info(data);
  60.        }
  61.        else {
  62.            List* temp = this->head;
  63.            while (next) {
  64.                next = temp->next;
  65.            }
  66.            temp->next = new List(data);
  67.        }
  68.        size++;
  69.    }
  70.    List(List* data) {
  71.       List* temp = data->head;
  72.       while (temp != nullptr) {
  73.           this->PushBack(data->listInfo);
  74.           temp = temp->next;
  75.       }
  76.    }
  77.  /*  void Sethead(int year, string name, int diameter, int frequency) {
  78.        this->head->listInfo.year = year;
  79.        this->head->listInfo.name = name;
  80.        this->head->listInfo.diameter = diameter;
  81.        this->head->listInfo.frequency = frequency;
  82.        this->head->next = NULL;
  83.    }*/
  84.   /* List* Sortby(int parameter, List* list) { НУЖЕН ИТЕРАТОР
  85.        if (parameter == 1){
  86.                for (int i = n - 1; i >= 0; i--)
  87.                    for (int j = 0; j < i; j++)
  88.                        if (list->listInfo.name >list->listInfo.name)
  89.                            swap(Tables[j], Tables[j + 1]);
  90.                cout << endl;
  91.                print(list);
  92.            }
  93.        }
  94.    }*/
  95.    List* AddList(int year, string name, int diameter, int frequency, List* previous) {
  96.        List* input = new List;
  97.        if (previous==NULL) {
  98.           // Sethead(year, name, diameter, frequency);
  99.       }
  100.        input->listInfo.year = year;
  101.        input->listInfo.name = name;
  102.        input->listInfo.diameter = diameter;
  103.        input->listInfo.frequency = frequency;
  104.        input->next = previous->next;
  105.        previous->next = input;
  106.        return input;
  107.    }
  108. };
  109. void print(List* list);
  110. void AddLast(List **list);
  111. int main()
  112. {
  113.    List* list = new List;
  114.    info data;
  115.    data.year = 1955;
  116.    data.name = "Michael Anatolievych";
  117.    data.diameter = 50;
  118.    data.frequency = 12;
  119.    list->PushBack(data);
  120.    info data2(1999, "Storch ", 70, 13);
  121.    list->PushBack(data2);
  122.    List* list2(list);
  123.    print(list2);
  124.    list[1].listInfo.year = 554455;
  125.    print(list);
  126.        return 0;
  127. }
  128. void print(List* list) {
  129.    List* Print = list->head;
  130.    cout << "|  Year |        Name               |  Diameter | Frequency |\n";
  131.    if (Print)
  132.        while (Print) {
  133.            cout << "|" << setw(7) << Print->listInfo.year << "|" << setw(27) << Print->listInfo.name << "|"
  134.                << setw(11) << Print->listInfo.diameter << "|" << setw(11) << Print->listInfo.frequency << "|\n";
  135.            Print = Print->next;
  136.        }
  137.    else {
Add Comment
Please, Sign In to add comment