Facebook
From Milk, 1 Month ago, written in C++.
Embed
Download Paste or View Raw
Hits: 143
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node {
  6.     double value;
  7.     Node* next;
  8.  
  9.     Node();
  10.     Node(double value);
  11.     Node(const Node& n);
  12.     ~Node();
  13.  
  14.     //swap premješta sadržaj između dva čvora
  15.     void swap(Node& n);
  16.  
  17.     void print();
  18. };
  19.  
  20. Node::Node(){
  21.     next = nullptr;
  22. }
  23.  
  24. Node::Node(double value){
  25.     this->value=value;
  26.     next = nullptr;
  27. }
  28.  
  29. Node::Node(const Node& n){
  30.     value = n.value;
  31.     next = n.next;
  32. }
  33.  
  34. Node::~Node(){}
  35.  
  36. void Node::swap(Node &n) {
  37.     double temp = value;
  38.     value = n.value;
  39.     n.value = temp;
  40. }
  41.  
  42. void Node::print() {
  43.     cout << "Node at: " << this << "; value: " << value << "; next: " << next << endl;
  44. }
  45.  
  46. class CSLL {
  47. protected:
  48.     Node* head;
  49. public:
  50.     CSLL();
  51.     CSLL(const CSLL& c);
  52.     ~CSLL();
  53.  
  54.     bool empty();
  55.  
  56.     void prepend(double value);
  57.     void append(double value);
  58.  
  59.     double removeFromHead();
  60.  
  61.     void sort();
  62.  
  63.     void print();
  64. };
  65.  
  66. CSLL::CSLL() {
  67. head = nullptr;
  68. }
  69.  
  70. CSLL::CSLL(const CSLL &c) {
  71.     head = nullptr;
  72.  
  73.     Node *it = c.head; //iterator
  74.     do{
  75.         append(it->value);
  76.         it = it->next;
  77.     }while(it!=c.head);
  78.  
  79. }
  80.  
  81. CSLL::~CSLL() {
  82. while (!empty())
  83.     removeFromHead();
  84. }
  85.  
  86. bool CSLL::empty() {
  87.     return head == nullptr;
  88. }
  89.  
  90. void CSLL::prepend(double value) {
  91. Node* n = new Node{value};
  92.  
  93. if (empty()){
  94.     n->next = n;
  95.     head = n;
  96. }else{
  97. Node* it = head;
  98. while(it->next!=head)
  99.     it = it->next;
  100.  
  101. it->next = n;
  102. n->next = head;
  103. head = n;
  104. }}
  105.  
  106. void CSLL::append(double value) {
  107.     prepend(value);
  108.     head = head->next;
  109. }
  110.  
  111. double CSLL::removeFromHead() {
  112.     if (!empty()){
  113.         double to_return = head->value;
  114.         Node* to_delete = head;
  115.  
  116.         Node* it = head;
  117.         while(it->next!=head)
  118.             it = it->next;
  119.  
  120.         if (head==head->next){
  121.  
  122.         }else{
  123.         it->next = head->next;
  124.         head = head->next;
  125.     }
  126.         delete to_delete;
  127.         return to_return;
  128. }}
  129.  
  130. void CSLL::sort() {
  131. Node* last = head;
  132. while(last->next != head)
  133.     last = last->next;
  134.  
  135. while(last!=head){
  136.     Node* j= head;
  137.     Node* prev;
  138.  
  139.     while(j!=last){
  140.         if(j->value > (j->next->value))
  141.             j->swap(*j->next);
  142.  
  143.         prev = j;
  144.         j = j->next;
  145.     }
  146.  
  147.     last = prev;
  148. }
  149. }
  150.  
  151.  
  152. void CSLL::print() {
  153. cout << "CSLL at: " << this << "; head at: " <&lt; head &lt;&lt; endl;
  154.  
  155. if(!empty()){
  156.     Node* it = head;
  157.  
  158.     do {
  159.         it->print();
  160.         it = it->next;
  161.     }while(it!=head);
  162. }
  163.  
  164. cout << endl << endl;
  165.  
  166. }
  167.  
  168.  
  169. class DynamicQueue {
  170. protected:
  171.     CSLL container;
  172. public:
  173.     DynamicQueue();
  174.     DynamicQueue(const DynamicQueue& q);
  175.     ~DynamicQueue();
  176.  
  177.     bool empty();
  178.  
  179.     void enqueue(double x);
  180.     double dequeue();
  181.  
  182.     void print();
  183. };
  184.  
  185. int main(){
  186.  
  187.     double arr[] = {59.9, 13.7, 10.0, 98.44, 16.7, 20.269, 1.5};
  188.     CSLL *c = new CSLL{};
  189.     c->print();
  190.  
  191.     for (double el: arr)
  192.         c->append(el);
  193.     c->print();
  194.  
  195.     CSLL *c2 = new CSLL{*c};
  196.     c2->print();
  197.     c2->sort();
  198.     c2->print();
  199.     c2->removeFromHead();
  200.     c2->print();
  201.  
  202.     delete c; delete c2;
  203.  
  204.     return 0;
  205. }
  206.