Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- class Stack
- {
- public:
- std::string name;
- int number;
- Stack *next = nullptr;
- void s_zapolnenie(Stack *&s, int i);
- void s_vivod (Stack *s);
- void s_udalenie (Stack *&s);
- void s_vstavka (Stack *&s);
- };
- void Stack::s_zapolnenie (Stack *&first, int i)
- {
- for (;i>0;i--) {
- Stack *new_elem = new Stack;
- std::cout << "Введите название института:" << std::endl;
- std::cin >> new_elem->name;
- std::cout << "Введите количество студентов в институте:" << std::endl;
- std::cin >> new_elem->number;
- if (first) {
- new_elem->next = first;
- first = new_elem;
- }
- else {
- first = new_elem;
- }
- }
- }
- void Stack::s_vivod (Stack *first)
- {
- std::cout << std::endl;
- Stack *current = first;
- while (current) {
- std::cout << "Название института:" << std::endl;
- std::cout << current->name << std::endl;
- std::cout << "Количество студентов в институте:" << std::endl;
- std::cout << current->number << std::endl;
- current = current->next;
- }
- std::cout << std::endl;
- }
- void Stack::s_udalenie (Stack *&first) {
- if (first->next) {
- Stack *tmp = first->next;
- delete first;
- first = tmp;
- }
- else {
- delete first;
- first = nullptr;
- }
- std::cout << "Первый элемент стэка удален" << std::endl;
- }
- void Stack::s_vstavka (Stack *&first)
- {
- Stack *new_elem = new Stack;
- std::cout << "Введите название института:" << std::endl;
- std::cin >> new_elem->name;
- std::cout << "Введите количество студентов в институте:" << std::endl;
- std::cin >> new_elem->number;
- Stack *last = first; // указатель на последний элемент
- Stack *current = first; //указатель на предпоследний элемент
- while (last->next) {
- last = last->next;
- }
- while (current->next != last) {
- current = current->next;
- }
- new_elem->next = last;
- current->next = new_elem;
- }
- int main()
- {
- Stack *first = nullptr;
- Stack institutes;
- std::cout << "Введите количество институтов для ввода" << std::endl;
- int k;
- std::cin >> k;
- institutes.s_zapolnenie(first, k); //добавить K
- institutes.s_vivod(first);
- institutes.s_vstavka(first);
- institutes.s_udalenie(first);
- institutes.s_vivod(first);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement