Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Stack
  5. {
  6. public:
  7.         std::string name;
  8.         int number;
  9.         Stack *next = nullptr;
  10.  
  11.         void s_zapolnenie(Stack *&s, int i);
  12.         void s_vivod (Stack *s);
  13.         void s_udalenie (Stack *&s);
  14.         void s_vstavka (Stack *&s);
  15. };
  16.  
  17. void Stack::s_zapolnenie (Stack *&first, int i)
  18. {
  19.     for (;i>0;i--) {
  20.         Stack *new_elem = new Stack;
  21.         std::cout << "Введите название института:" << std::endl;
  22.         std::cin >> new_elem->name;
  23.         std::cout << "Введите количество студентов в институте:" << std::endl;
  24.         std::cin >> new_elem->number;
  25.  
  26.         if (first) {
  27.             new_elem->next = first;
  28.             first = new_elem;
  29.  
  30.         }
  31.         else {
  32.             first = new_elem;
  33.         }
  34.     }
  35. }
  36.  
  37. void Stack::s_vivod (Stack *first)
  38. {
  39.     std::cout << std::endl;
  40.     Stack *current = first;
  41.     while (current) {
  42.         std::cout << "Название института:" << std::endl;
  43.         std::cout << current->name << std::endl;
  44.         std::cout << "Количество студентов в институте:" << std::endl;
  45.         std::cout << current->number << std::endl;
  46.  
  47.         current = current->next;
  48.     }
  49.     std::cout << std::endl;
  50. }
  51.  
  52. void Stack::s_udalenie (Stack *&first) {
  53.     if (first->next) {
  54.         Stack *tmp = first->next;
  55.         delete first;
  56.         first = tmp;
  57.     }
  58.     else {
  59.         delete first;
  60.         first = nullptr;
  61.     }
  62.     std::cout << "Первый элемент стэка удален" << std::endl;
  63. }
  64.  
  65.  
  66. void Stack::s_vstavka (Stack *&first)
  67. {
  68.     Stack *new_elem = new Stack;
  69.     std::cout << "Введите название института:" << std::endl;
  70.     std::cin >> new_elem->name;
  71.     std::cout << "Введите количество студентов в институте:" << std::endl;
  72.     std::cin >> new_elem->number;
  73.  
  74.     Stack *last = first; // указатель на последний элемент
  75.     Stack *current = first; //указатель на предпоследний элемент
  76.  
  77.     while (last->next) {
  78.         last = last->next;
  79.     }
  80.  
  81.     while (current->next != last) {
  82.         current = current->next;
  83.     }
  84.  
  85.     new_elem->next = last;
  86.     current->next = new_elem;
  87. }
  88.  
  89. int main()
  90. {
  91.     Stack *first = nullptr;
  92.     Stack institutes;
  93.     std::cout << "Введите количество институтов для ввода" << std::endl;
  94.     int k;
  95.     std::cin >> k;
  96.     institutes.s_zapolnenie(first, k); //добавить K
  97.     institutes.s_vivod(first);
  98.     institutes.s_vstavka(first);
  99.     institutes.s_udalenie(first);
  100.     institutes.s_vivod(first);
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement