Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Структура данных - стек.
- Напишите программу для моделирования Т-образного сортировочного узла на железной дороге,
- позволяющего разделять на два направления состав, состоящий из вагонов двух типов (на каждое направление
- формируется состав из вагонов одного типа
- */
- #include <iostream>
- #include <string.h>
- #include <cstdlib>
- using namespace std;
- class Stack
- {
- private:
- struct Element
- {
- int type;
- string number;
- struct Element *next;
- } * pstack;
- public:
- Stack ()//конструктор
- {
- pstack = NULL;
- }
- ~Stack ()//деструктор
- {
- Element * temp;
- while (pstack)
- {
- temp = pstack;
- pstack = pstack->next;
- delete temp;
- }
- }
- int Empty()//проверка на наполненость стека
- {
- return pstack == NULL;
- }
- int TopT()//влзврат типа последнего элемента
- {
- return pstack->type;
- }
- string TopN()//возврат номера последнего вагона
- {
- return pstack->number;
- }
- void Pop()//разрушающее чтение
- {
- Element *del = pstack;
- pstack = pstack->next;
- delete del;
- }
- void Push (int x,string y) //добавить элемент в стек
- {
- Element *ins = new Element;
- ins->type=x;
- ins->number=y;
- ins->next = pstack;
- pstack = ins;
- }
- };
- int proverka()
- {
- int x;
- do
- {
- cout<<"Enter train type(0 or 1) ";
- cin>>x;
- }while((x<0)||(x>1));
- return x;
- }
- int main()
- {
- int menu,temptype;
- string tempnumb;
- Stack trainlist;
- do
- {
- system("cls");
- cout<<"1)Make train 2) Watch last train 3)Send trains 4)Exit"<<endl;
- cin>>menu;
- switch(menu)
- {
- case 1:
- {
- temptype=proverka();
- cout<<"Enter train number ";
- cin>>tempnumb;
- trainlist.Push(temptype,tempnumb);
- cout<<"train was added in stack"<<endl;
- system("pause");
- break;
- }
- case 2:
- {
- if(!trainlist.Empty())
- {
- cout<<trainlist.TopT()<<" "<<trainlist.TopN()<<endl;
- system("pause");
- }
- else
- {
- cout<<"No trains"<<endl;
- system("pause");
- }
- break;
- }
- case 3:
- {
- cout<<"1 way\t\t\t2 way"<<endl;
- while(!(trainlist.Empty()))
- {
- if(!(trainlist.TopT()))
- cout<<trainlist.TopN()<<endl;
- else
- cout<<"\t\t\t"<<trainlist.TopN()<<endl;
- trainlist.Pop();
- }
- system("pause");
- break;
- }
- }
- }while(menu!=4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement