Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Queue.h
- #ifndef _________18__Queue__
- #define _________18__Queue__
- #include <iostream>
- #endif /* defined(_________18__Queue__) */
- using namespace std;
- class Queue
- {
- public:
- Queue();
- ~Queue();
- struct Element
- {
- int data;
- Element *next;
- };
- Element *head;
- Element *tail;
- bool Empty();
- void Dobavka(int);
- void Add(int);
- void Print();
- };
- Queue.cpp
- #include "Queue.h"
- Queue::Queue()
- {
- head = NULL;
- tail = NULL;
- }
- Queue::~Queue()
- {
- }
- bool Queue::Empty()
- {
- if (this->head == NULL)
- return true;
- return false;
- }
- void Queue::Add(int str)
- {
- Element *temp = new Element;
- temp->data = str;
- temp->next = NULL;
- if (Empty())
- head = temp;
- else
- tail->next = temp;
- tail = temp;
- }
- void Queue::Dobavka(int str)
- {
- Element *Vangog = head;
- while (Vangog)
- {
- if (Vangog->data < 0)
- {
- Element *temp = new Element;
- temp->data = str;
- temp->next = Vangog->next;
- Vangog->next = temp;
- }
- Vangog = Vangog->next;
- }
- }
- void Queue::Print()
- {
- Element *temp = head;
- while (temp)
- {
- cout << temp->data << endl;
- temp = temp->next;
- }
- }
- main.cpp
- #include <iostream>
- #include <fstream>
- #include <time.h>
- #include "Queue.h"
- using namespace std;
- int main()
- {
- setlocale(0, "");
- srand(time(0));
- Queue queue;
- for (int i = 0; i < 20; i++)
- queue.Add((int)rand() % 61 - 31);
- cout << "Начальная очередь\n";
- queue.Print();
- cout << endl;
- cout << "Измененная очередь";
- queue.Dobavka(100);
- queue.Print();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement