Guest User

Untitled

a guest
Jan 13th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. //
  2. // Created by Александр Сулин on 13/01/2018.
  3. //
  4.  
  5. #include <iostream>
  6. #include "LinkedList.h"
  7.  
  8. using namespace std;
  9.  
  10. void LinkedList::add(double value) {
  11.     Node *node = root;
  12.     if (node == nullptr) {
  13.         root = new Node(value);
  14.         return;
  15.     }
  16.     while (node->getNext() != nullptr)
  17.         node = node->getNext();
  18.     node->setNext(new Node(value));
  19. }
  20.  
  21. void LinkedList::task1() {
  22.     Node *node = root;
  23.     while (node != nullptr) {
  24.         double temp = node->getValue();
  25.         temp = (temp < 0 ? temp + 1 : temp - 1);
  26.         node->setValue(temp);
  27.         node = node->getNext();
  28.     }
  29. }
  30.  
  31. std::ostream &operator<<(std::ostream &os, const LinkedList &list) {
  32.     Node *node = list.root;
  33.     while (node != nullptr) {
  34.         os << node->getValue() << " ";
  35.         node = node->getNext();
  36.     }
  37.     os << endl;
  38.  
  39.     return os;
  40. }
  41.  
  42. std::istream &operator>>(std::istream &is, LinkedList &list) {
  43.     int n;
  44.     cout << "n = ";
  45.     is >> n;
  46.  
  47.     cout << "values = ";
  48.     double temp;
  49.     is >> temp;
  50.  
  51.     list.setRoot(new Node(temp));
  52.     Node* node = list.root;
  53.     for (int i = 1; i < n; ++i) {
  54.         is >> temp;
  55.         node->setNext(new Node(temp));
  56.         node = node->getNext();
  57.     }
  58.  
  59.     return is;
  60. }
  61.  
  62. void LinkedList::setRoot(Node *node) {
  63.     this->root = node;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment