Advertisement
qberik

Untitled

Sep 19th, 2022
1,127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Cell
  6. {
  7. public:
  8.     double val;
  9.     Cell* next;
  10.     Cell* prev;
  11.     Cell(double v)
  12.     {
  13.         val = v;
  14.         next = NULL;
  15.         prev = NULL;
  16.     }
  17. };
  18.  
  19. class List
  20. {
  21. private:
  22.     Cell* head = NULL;
  23.     Cell* tail = NULL;
  24. public:
  25.  
  26.     Cell* begin(){
  27.       return head;
  28.     }
  29.  
  30.     Cell* end(){
  31.       return tail;
  32.     }
  33.  
  34.     void push_front(double v)
  35.     {
  36.         if (head == NULL)
  37.         {
  38.             head = new Cell(v);
  39.             tail = head;
  40.         }
  41.         else
  42.         {
  43.             Cell* n = new Cell(v);
  44.             n->next = head;
  45.             head->prev = n;
  46.             head = n;
  47.         }
  48.     }
  49.  
  50.     void push_back( double v )
  51.     {
  52.         if( head == NULL ){
  53.             head = new Cell(v);
  54.             tail = head;
  55.         }
  56.         else
  57.         {
  58.             Cell* n = new Cell(v);
  59.             n->prev = tail;
  60.             tail->next = n;
  61.             tail = n;
  62.         }
  63.     }
  64.  
  65.    
  66.     void remove( int index ){
  67.         Cell* elem = head;
  68.         for( int i = 0; i < index; i++ ){
  69.             elem = elem->next;
  70.         }
  71.         elem->prev->next = elem->next;
  72.         elem->next->prev = elem->prev;
  73.     }
  74.  
  75.     int find( double elem ){
  76.         Cell* curr = head;
  77.         int i = 0;
  78.         while (curr != NULL)
  79.         {
  80.             if( curr->val == elem ){
  81.               return i;
  82.             }
  83.             curr = curr->next;
  84.             i++;
  85.         }
  86.         return -1;
  87.     }
  88.  
  89.     void print()
  90.     {
  91.         Cell* curr = head;
  92.         while (curr != NULL)
  93.         {
  94.             cout << curr->val << " ";
  95.             curr = curr->next;
  96.         }
  97.         cout << endl;
  98.     }
  99. };
  100.  
  101. int main()
  102. {
  103.    
  104.     int n;
  105.     cout << "Enert n: ";
  106.     cin >> n;
  107.  
  108.     if( n >= 2 ){
  109.  
  110.         List data;
  111.  
  112.         for( int i = 0; i < n; i++ ){
  113.  
  114.             double elem;
  115.             cout << "Enter " << i + 1 << " element: ";
  116.             cin >> elem;
  117.             data.push_back(elem);
  118.  
  119.         }
  120.  
  121.         double ans = 1;
  122.         Cell* start = data.begin();
  123.         Cell* end   = data.end();
  124.         for( int i = 0; i < n; i++ ){
  125.             double elem;
  126.             elem = start->val * end->val;
  127.             start = start->next;
  128.             end = end->prev;
  129.             ans = ans * elem;
  130.         }
  131.      
  132.         cout << "Answer is " << ans << endl;
  133.     }else{
  134.         cout << "ERROR: n is too small" << endl;
  135.     }
  136.  
  137.     return 0;
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement