Advertisement
Lempek

List.cpp

May 24th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. /*
  2.  * List.cpp
  3.  *
  4.  */
  5.  
  6. #include "List.h"
  7. List::Node::~Node(){
  8.     // TO DO
  9.    
  10. }
  11. List::~List() {
  12. }
  13.  
  14. void List::push_front(int object){
  15.     Node* new_node = new Node(object);
  16.     head->previous = new_node;
  17.     new_node->next = head;
  18.     head = new_node;
  19.     ++n;
  20. }
  21. void List::pop_front(){
  22.     Node* next = head->next;
  23.     delete head;
  24.     next->previous = nullptr;
  25.     head = next;
  26.     --n;
  27. }
  28. void List::push_back(int object) {
  29.     Node* new_node = new Node(object);
  30.     if (n == 0) {
  31.         head = new_node;
  32.         tail = new_node;
  33.     }
  34.     else {
  35.         tail->next = new_node;
  36.         new_node->previous = tail;
  37.         tail = new_node;
  38.     }
  39.     ++n;
  40. }
  41. void List::pop_back(){
  42.     Node* previous = tail->previous;
  43.     delete tail;
  44.     previous->next = nullptr;
  45.     tail = previous;
  46.     --n;
  47. }
  48. void List::insert(int index, int object){
  49.     if (index == 0){
  50.         push_front(object);
  51.     }
  52.     else if (index == (n - 1)){
  53.         push_back(object);
  54.     }
  55.     else{
  56.         try{
  57.             Node* new_node = new Node(object);
  58.             Node* previous = get_node_ptr(index - 1);
  59.             Node* next = get_node_ptr(index + 1);
  60.  
  61.             next->previous = new_node;
  62.             new_node->next = next;
  63.  
  64.             previous->next = new_node;
  65.             new_node->previous = previous;
  66.             ++n;
  67.         }
  68.         catch (const char* &s) {
  69.             std::cerr << s;
  70.             system("PAUSE");
  71.             exit(1);
  72.         }
  73.     }
  74. }
  75. void List::erase(int index){
  76.     if (index == 0){
  77.         pop_front();
  78.     }
  79.     else if (index == (n - 1)){
  80.         pop_back();
  81.     }
  82.     else
  83.         try{
  84.         Node* current = get_node_ptr(index);
  85.         Node* previous = get_node_ptr(index - 1);
  86.         Node* next = get_node_ptr(index + 1);
  87.  
  88.         next->previous = current->previous;
  89.         previous->next = current->next;
  90.         delete current;
  91.         --n;
  92.     }
  93.     catch (const char* &s) {
  94.         std::cerr << s;
  95.         system("PAUSE");
  96.         exit(1);
  97.     }
  98. }
  99. void List::swap(int index1, int index2){
  100.     //try{
  101.     //  Node* first = get_node_ptr(index1);
  102.     //  Node* second = get_node_ptr(index2);
  103.     //  Node* tmp = new Node(first);
  104.  
  105.     //  first->data = second->data;
  106.     //  first->next = second->next;
  107.     //  first->previous = second->previous;
  108.  
  109.     //  second->data = tmp->data;
  110.     //  second->next = tmp->next;
  111.     //  second->previous = tmp->previous;
  112.  
  113.     //  delete tmp;
  114.     //}
  115.     //catch (const char* &s) {
  116.     //  std::cerr << s;
  117.     //  system("PAUSE");
  118.     //  exit(1);
  119.     //}
  120.  
  121. }
  122. int List::size(){
  123.     return n;
  124. }
  125. int List::get_index_of(const int object){
  126.     return 0;
  127. }
  128. List::Node& List::get_node_ref(int index){
  129.     Node* node = head;
  130.     if (index >= n || index < 0)
  131.         throw "Przekroczono zakres!\n";
  132.     for (int i = 0; i < index; ++i) {
  133.         if (node == nullptr)
  134.             throw "Blad! Dziura w liscie!\n";
  135.         node = node->next;
  136.     }
  137.     return *node;
  138. }
  139. List::Node* List::get_node_ptr(int index) {
  140.     Node* node = head;
  141.     if (index >= n || index < 0)
  142.         throw "Przekroczono zakres!\n";
  143.     for (int i = 0; i < index; ++i) {
  144.         if (node == nullptr)
  145.             throw "Blad! Dziura w liscie!\n";
  146.         node = node->next;
  147.     }
  148.     return node;
  149. }
  150. void List::save_to_file(std::string file_name){
  151.  
  152. }
  153. void List::load_from_file(std::string file_name){
  154.  
  155. }
  156.  
  157. //bool List::operator==(const List &L){
  158. //
  159. //}
  160. //List& List::operator=(const List &L){
  161. //
  162. //}
  163. int& List::operator[](int index) {
  164.     try{
  165.         return get_node_ref(index).data;
  166.     }
  167.     catch (const char* &s) {
  168.         std::cerr << s;
  169.         system("PAUSE");
  170.         exit(1);
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement