Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. typedef struct m {
  8.     int val;
  9.     int index;
  10.     struct m* next;
  11.     struct m* pred;
  12. } *pointer;
  13.  
  14. pointer M;
  15.  
  16. void Creat() {
  17.     pointer tmp = NULL;
  18.     cout << "Enter the number: ";
  19.     int n; cin >> n;
  20.     int k = n * 0.9;
  21.     cout << "Enter the integers so that no more than " << k << " are 0:\n";
  22.     for (int i = 0; i < n; i++) {
  23.         if (tmp == NULL) {
  24.             tmp = new m();
  25.             cin >> tmp->val;
  26.             tmp->index = i;
  27.             M = tmp;
  28.         } else {
  29.             tmp->next = new m();
  30.             tmp->next->pred = tmp;
  31.             tmp = tmp->next;
  32.             cin >> tmp->val;
  33.             tmp->index = i;
  34.         }
  35.     }
  36. }
  37.  
  38. void Compression() {
  39.     pointer tmp = M;
  40.     while (tmp != NULL) {
  41.         if (tmp->val == 0) {
  42.             if (tmp->pred != NULL) {
  43.                 tmp->pred->next = tmp->next;
  44.                 if (tmp->next != NULL) tmp->next->pred = tmp->pred;
  45.                 tmp = tmp->next;
  46.             }
  47.             else {
  48.                 M = tmp->next;
  49.                 tmp->next->pred = NULL;
  50.                 tmp = M;
  51.             }
  52.         }
  53.         else {
  54.             tmp = tmp->next;
  55.         }
  56.     }
  57.  
  58.     tmp = M;
  59.     cout << "Compressed list:\n";
  60.     while (tmp != NULL) {
  61.         cout << tmp->index << " - " << tmp->val << endl;
  62.         tmp = tmp->next;
  63.     }
  64. }
  65.  
  66. int Suma() {
  67.     pointer tmp = M;
  68.     int S = 0;
  69.     while (tmp != NULL) {
  70.         S += tmp->val;
  71.         tmp = tmp->next;
  72.     }
  73.     return S;
  74. }
  75.  
  76. int main()
  77. {
  78.     Creat();
  79.     Compression();
  80.     cout << "The amount is equal: " << Suma();
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement