Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdarg.h>
- #include <string>
- using namespace std;
- struct Node
- {
- int x;
- Node* next;
- Node* prev;
- Node(int x)
- {
- this->x = x;
- prev = next = NULL;
- }
- };
- class DualList
- {
- private:
- Node* begin, * end;
- int count;
- public:
- DualList()
- {
- begin = end = NULL;
- count = 0;
- }
- DualList(const DualList& a)
- {
- }
- DualList(DualList& list)
- {
- begin = end = NULL;
- count = 0;
- Node* temp = list.begin;
- while (temp)
- {
- Insert(count, temp->x);
- count++;
- temp = temp->next;
- }
- }
- void Insert(int pos, int x);
- int Size();
- void Print();
- void reversePrint();
- void Del(int x);
- DualList operator+(DualList& a)
- {
- return *this;
- }
- DualList& operator =(DualList& a)
- {
- Node* newTemp = a.begin;
- int cnt = a.count;
- for (int i = 0; i < count; i++)
- {
- this->Insert(cnt, newTemp->x);
- newTemp = newTemp->next;
- cnt++;
- }
- return *this;
- }
- bool operator !=(DualList& a)
- {
- Node* newTmp = a.begin;
- Node* newTmp1 = begin;
- if (count != a.count)
- {
- return true;
- }
- for (int i = 0; i < count; i++)
- {
- if (newTmp1->x != newTmp->x)
- {
- return true;
- }
- newTmp = newTmp->next;
- newTmp1 = newTmp1->next;
- }
- return false;
- }
- bool operator ==(DualList& a)
- {
- Node* newTmp = a.begin;
- Node* newTmp1 = begin;
- /*if (count == a.count)
- {
- return true;
- }*/
- for (int i = 0; i < count; i++)
- {
- if (newTmp1->x != newTmp->x)
- {
- return false;
- }
- newTmp = newTmp->next;
- newTmp1 = newTmp1->next;
- }
- return true;
- }
- bool operator >(DualList& a)
- {
- Node* newTmp = a.begin;
- Node* newTmp1 = begin;
- if (*this == a) return false;
- if (count < a.count)
- {
- return false;
- }
- for (int i = 0; i < count; i++)
- {
- if (newTmp1->x < newTmp->x)
- {
- return false;
- }
- newTmp = newTmp->next;
- newTmp1 = newTmp1->next;
- }
- return true;
- }
- bool operator <(DualList& a)
- {
- Node* newTmp = a.begin;
- Node* newTmp1 = begin;
- if (*this == a) return false;
- if (count > a.count)
- {
- return false;
- }
- for (int i = 0; i < count; i++)
- {
- if (newTmp1->x > newTmp->x)
- {
- return false;
- }
- newTmp = newTmp->next;
- newTmp1 = newTmp1->next;
- }
- return true;
- }
- bool operator >=(DualList& a)
- {
- Node* newTmp = a.begin;
- Node* newTmp1 = begin;
- if (count < a.count)
- {
- return false;
- }
- for (int i = 0; i < count; i++)
- {
- if (newTmp1->x < newTmp->x)
- {
- return false;
- }
- newTmp = newTmp->next;
- newTmp1 = newTmp1->next;
- }
- return true;
- }
- bool operator <=(DualList& a)
- {
- Node* newTmp = a.begin;
- Node* newTmp1 = begin;
- if (count > a.count)
- {
- return false;
- }
- for (int i = 0; i < count; i++)
- {
- if (newTmp1->x > newTmp->x)
- {
- return false;
- }
- newTmp = newTmp->next;
- newTmp1 = newTmp1->next;
- }
- return true;
- }
- };
- void DualList::Insert(int pos, int x)
- {
- if (pos < 0 || pos > count)
- {
- cout << "Incorrect position" << endl;
- return;
- }
- Node* newNode = new Node(x);
- if (pos == 0)
- {
- if (begin == NULL)
- {
- begin = end = newNode;
- }
- else
- {
- newNode->next = begin;
- begin->prev = newNode;
- begin = newNode;
- }
- }
- else if (pos == count)
- {
- end->next = newNode;
- newNode->prev = end;
- end = newNode;
- }
- else
- {
- Node* prevInsert = begin;
- Node* postInsert;
- Node* temp = begin;
- for (int i = 0; i < pos; i++)
- {
- prevInsert = prevInsert->next;
- }
- postInsert = prevInsert->next;
- prevInsert->next = newNode;
- prevInsert->prev = newNode;
- }
- count++;
- }
- int DualList::Size()
- {
- return count;
- }
- void DualList::Print()
- {
- Node* temp = begin;
- for (int i = 0; i < count; i++)
- {
- cout << temp->x << " ";
- temp = temp->next;
- }
- }
- void DualList::reversePrint()
- {
- Node* temp = end;
- for (int i = 0; i < count; i++)
- {
- cout << temp->x << " ";
- temp = temp->prev;
- }
- }
- void DualList::Del(int x)
- {
- Node* newNode = new Node(x);
- if (x == 0)
- {
- newNode = newNode->next;
- newNode->prev = NULL;
- delete newNode;
- }
- }
- int main()
- {
- DualList a;
- DualList b;
- a.Insert(0, 4);
- a.Insert(1, 5);
- a.Insert(2, 6);
- a.Insert(3, 9);
- cout << "a: ";
- a.Print();
- cout << endl;
- b.Insert(0, 4);
- b.Insert(1, 5);
- b.Insert(2, 6);
- b.Insert(3, 7);
- cout << "b: ";
- b.Print();
- //a = b;
- cout << endl;
- cout << (a < b) << endl;
- cout << (a > b) << endl;
- cout << (a <= b) << endl;
- cout << (a >= b) << endl;
- cout << (a == b) << endl;
- cout << (a != b) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement