Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename T>
- struct Node
- {
- T info;
- Node *next, *prev;
- };
- template<typename T>
- class Queue
- {
- private:
- Node<T> *begin, *end;
- int queue_depth;
- public:
- Queue() {
- begin = end = NULL;
- queue_depth = 0;
- }
- ~Queue() {
- delete[] begin;
- }
- Queue(const Queue &q) {
- Node<T>* cur = q.begin;
- this->begin = this->end = new Node<T>;
- this->begin->info = cur->info;
- this->queue_depth = 1;
- cur = cur->prev;
- while (cur != NULL) {
- this->push_back(cur->info);
- cur = cur->prev;
- }
- }
- Queue& operator =(const Queue &right) {
- if (this == &right)
- return *this;
- Node<T>* cur = right.begin;
- this->end = this->begin;
- this->begin->prev = NULL;
- this->begin->info = cur->info;
- cur = cur->prev;
- while (cur != NULL) {
- this->push_back(cur->info);
- cur = cur->prev;
- }
- return *this;
- }
- void push_front(T a) {
- Node<T>* cur = new Node<T>;
- if (!queue_depth) {
- begin = end = cur;
- cur->next = cur->prev = NULL;
- cur->info = a;
- queue_depth = 1;
- return;
- }
- begin->next = cur;
- cur->prev = begin;
- begin = cur;
- cur->next = NULL;
- cur->info = a;
- queue_depth++;
- }
- void push_back(T a) {
- Node<T>* cur = new Node<T>;
- if (!queue_depth) {
- begin = end = cur;
- cur->next = cur->prev = NULL;
- cur->info = a;
- queue_depth = 1;
- return;
- }
- end->prev = cur;
- cur->next = end;
- end = cur;
- cur->prev = NULL;
- cur->info = a;
- queue_depth++;
- }
- void print_begin() {
- Node<T>* cur = begin;
- while (cur != NULL) {
- cout << cur->info << endl;
- cur = cur->prev;
- }
- }
- void print_end() {
- Node<T>* cur = end;
- while (cur != NULL) {
- cout << cur->info << endl;
- cur = cur->next;
- }
- }
- void var5() {
- Node<T>* cur;
- while (begin->info % 5 == 0 && begin->info % 2 != 0) {
- begin = begin->prev;
- begin->next = NULL;
- }
- cur = begin;
- while (cur->prev->prev != NULL) {
- if (cur->prev->info % 5 == 0 && cur->prev->info % 2 != 0) {
- cur->prev = cur->prev->prev;
- cur->prev->next = cur;
- }
- else
- cur = cur->prev;
- }
- if (cur->prev->info % 5 == 0 && cur->prev->info % 2 != 0) {
- end = end->next;
- cur->prev = cur->prev->prev;
- }
- }
- void var11() {
- Node<T>* cur = begin;
- T sum = 0;
- while (cur != NULL) {
- sum += cur->info;
- cur = cur->prev;
- }
- begin->info = T(sum / queue_depth);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement