Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "double_list.h"
- using namespace std;
- DoubleList::DoubleList()
- {
- first = nullptr;
- last = nullptr;
- size = 0;
- }
- DoubleList::DoubleList(const DoubleList &l2)
- {
- if (l2.first == nullptr)
- {
- first = nullptr;
- last = nullptr;
- size = 0;
- }
- else
- {
- size = 0;
- list_node *p = l2.first;
- while (p != nullptr)
- {
- add_last_node(p->info);
- p = p->next;
- }
- }
- }
- DoubleList::~DoubleList()
- {
- while (!empty())
- delete_first_node();
- }
- void DoubleList::add_first_node(int x)
- {
- if (empty())
- {
- first = new list_node;
- last = first;
- first->info = x;
- first->prev = nullptr;
- first->next = nullptr;
- size++;
- }
- else
- {
- list_node *p = new list_node;
- first->prev = p;
- p->next = first;
- p->info = x;
- p->prev = nullptr;
- first = p;
- size++;
- }
- }
- void DoubleList::add_last_node(int x)
- {
- if (empty())
- {
- first = new list_node;
- last = first;
- first->info = x;
- first->prev = nullptr;
- first->next = nullptr;
- size++;
- }
- else
- {
- list_node *p = new list_node;
- p->prev = last;
- p->info = x;
- p->next = nullptr;
- last->next = p;
- last = p;
- size++;
- }
- }
- void DoubleList::print_list()
- {
- list_node *p = first;
- while (p != nullptr)
- {
- cout << p->info << " ";
- p = p->next;
- }
- cout << endl;
- }
- int DoubleList::list_size()
- {
- return size;
- }
- bool DoubleList::empty()
- {
- return (size == 0);
- }
- DoubleList unite_lists(const DoubleList &l1, const DoubleList &l2)
- {
- if (!l1.first && !l2.first)
- {
- DoubleList l;
- return l;
- }
- if (!l1.first)
- {
- DoubleList l(l2);
- return l;
- }
- if (!l2.first)
- {
- DoubleList l(l1);
- return l;
- }
- DoubleList copy1(l1);
- DoubleList copy2(l2);
- copy1.last->next = copy2.first;
- copy2.first->prev = copy1.last;
- copy1.last = copy2.last;
- copy1.size += copy2.size;
- DoubleList l(copy1);
- copy2.first = nullptr;
- copy2.last = nullptr;
- copy2.size = 0;
- return l;
- }
- void DoubleList::delete_first_node()
- {
- if (first == last)
- {
- delete first;
- size--;
- }
- else
- {
- first = first->next;
- delete first->prev;
- first->prev = nullptr;
- size--;
- }
- }
- void DoubleList::delete_last_node()
- {
- if (last == first)
- {
- delete last;
- size--;
- }
- else
- {
- last = last->prev;
- delete last->next;
- last->next = nullptr;
- size--;
- }
- }
- bool DoubleList::element_exists(int x)
- {
- list_node *p = first;
- while (p != nullptr)
- {
- if (p->info == x)
- return true;
- p = p->next;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement