Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <string.h>
- using namespace std;
- typedef struct m {
- int val;
- int index;
- struct m* next;
- struct m* pred;
- } *pointer;
- pointer M;
- void Creat() {
- pointer tmp = NULL;
- cout << "Enter the number: ";
- int n; cin >> n;
- int k = n * 0.9;
- cout << "Enter the integers so that no more than " << k << " are 0:\n";
- for (int i = 0; i < n; i++) {
- if (tmp == NULL) {
- tmp = new m();
- cin >> tmp->val;
- tmp->index = i;
- M = tmp;
- } else {
- tmp->next = new m();
- tmp->next->pred = tmp;
- tmp = tmp->next;
- cin >> tmp->val;
- tmp->index = i;
- }
- }
- }
- void Compression() {
- pointer tmp = M;
- while (tmp != NULL) {
- if (tmp->val == 0) {
- if (tmp->pred != NULL) {
- tmp->pred->next = tmp->next;
- if (tmp->next != NULL) tmp->next->pred = tmp->pred;
- tmp = tmp->next;
- }
- else {
- M = tmp->next;
- tmp->next->pred = NULL;
- tmp = M;
- }
- }
- else {
- tmp = tmp->next;
- }
- }
- tmp = M;
- cout << "Compressed list:\n";
- while (tmp != NULL) {
- cout << tmp->index << " - " << tmp->val << endl;
- tmp = tmp->next;
- }
- }
- int Suma() {
- pointer tmp = M;
- int S = 0;
- while (tmp != NULL) {
- S += tmp->val;
- tmp = tmp->next;
- }
- return S;
- }
- int main()
- {
- Creat();
- Compression();
- cout << "The amount is equal: " << Suma();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement