Advertisement
frentzy

problema lab liste cu class (Irina #7)

Apr 16th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. class Lista {
  8. private:
  9.     struct nod {
  10.         int info;
  11.         nod *next;
  12.     };
  13.     nod *head, *last;
  14. public:
  15.     Lista();
  16.     void Append(int nr = 0);
  17.     void Remove(int poz);
  18. //  Lista& Remove(int poz);
  19.     void DisplayList();
  20.     ~Lista();
  21. };
  22. Lista::~Lista() {
  23.     cout << "\nDestructor\n";
  24. }
  25.  
  26. Lista::Lista() {
  27.     this->head = NULL;
  28.     //this->last = head;
  29. }
  30.  
  31. void Lista::Append(int nr) {
  32.     nod *temp = new nod;
  33.     temp->info = nr;
  34.     if (head) {
  35.         last->next = temp;
  36.         last = temp;
  37.         last->next = NULL;
  38.     }
  39.     else {
  40.         head = temp;
  41.         head->next = NULL;
  42.         last = head;
  43.     }
  44. }
  45.  
  46.  
  47. void Lista::Remove(int nr) {
  48.     for (;;)
  49.         for (nod *temp = head; temp; temp = temp->next) {
  50.        
  51.             if (head->info == nr) {
  52.  
  53.                 if (head->next != NULL) {
  54.                     nod * _temp = head;
  55.                     head = head->next;
  56.                     delete _temp;
  57.                     break;
  58.                 }
  59.                 else {
  60.                     nod *_temp = head;
  61.                     last = head = NULL;
  62.                     delete _temp;
  63.                     goto exit;
  64.                 }
  65.             }
  66.        
  67.             if (temp->info == nr) {
  68.                 nod* _temp = head;
  69.                 while (_temp->next != temp) {
  70.                     _temp = _temp->next;
  71.                 }
  72.                 _temp->next = temp->next;
  73.  
  74.                 if (temp == last) {
  75.                     _temp->next = last->next;
  76.                     last = _temp;
  77.                 }
  78.  
  79.                 delete temp;
  80.                 break;
  81.             }
  82.        
  83.             if (temp->info != nr && temp == last) {
  84.                 goto exit;
  85.             }
  86.            
  87.         }
  88. exit:;
  89.  
  90. }
  91.  
  92.  
  93.  
  94. void Lista::DisplayList() {
  95.     cout << endl;
  96.     if (head) {
  97.         for (nod *temp = head; temp; temp = temp->next) {
  98.             cout << temp->info << " ,";
  99.         }
  100.         printf("%c%c%c", 8, 32, 8);
  101.     }
  102.     else {
  103.         cout << "Lista este vida";
  104.     }
  105.    
  106. }
  107.  
  108.  
  109. void main() {
  110.     Lista ceva;
  111.     ceva.Append(10);
  112.     //ceva.Append(5);
  113.     ceva.Append(10);
  114.     //ceva.Append(-1);
  115.     ceva.Append(10);
  116.     ceva.Append(10);
  117.     //ceva.Append(11);
  118.     ceva.Append(10);
  119.     ceva.DisplayList();
  120.     ceva.Remove(10);
  121.     ceva.DisplayList();
  122.  
  123.  
  124.     _getch();
  125. }
  126.  
  127.  
  128. // din 2,2,3,3,2 , scapi de "2" si ramane doar "3" adica 3,3
  129.  
  130. //##############################################
  131. #include <iostream>
  132. #include <conio.h>
  133. #include <stdio.h>
  134.  
  135. using namespace std;
  136.  
  137. class Lista {
  138. private:
  139.     struct nod {
  140.         int info;
  141.         nod *next;
  142.     };
  143.     nod *head, *last;
  144. public:
  145.     Lista();
  146.     void Append(int nr = 0);
  147.     void Remove(int poz);
  148.     void Insert();
  149.     void DisplayList();
  150.     ~Lista();
  151. };
  152. Lista::~Lista() {
  153.  
  154.     cout << "\nDestructor\n";
  155.    
  156. }
  157.  
  158. Lista::Lista() {
  159.     this->head = NULL;
  160.     //this->last = head;
  161. }
  162.  
  163. void Lista::Append(int nr) {
  164.     nod *temp = new nod;
  165.     temp->info = nr;
  166.     if (head) {
  167.         last->next = temp;
  168.         last = temp;
  169.         last->next = NULL;
  170.     }
  171.     else {
  172.         head = temp;
  173.         head->next = NULL;
  174.         last = head;
  175.     }
  176. }
  177.  
  178.  
  179. void Lista::Remove(int nr) {
  180.     for (;;)
  181.         for (nod *temp = head; temp; temp = temp->next) {
  182.        
  183.             if (head->info == nr) {
  184.  
  185.                 if (head->next != NULL) {
  186.                     nod * _temp = head;
  187.                     head = head->next;
  188.                     delete _temp;
  189.                     break;
  190.                 }
  191.                 else {
  192.                     nod *_temp = head;
  193.                     last = head = NULL;
  194.                     delete _temp;
  195.                     goto exit;
  196.                 }
  197.             }
  198.        
  199.             if (temp->info == nr) {
  200.                 nod* _temp = head;
  201.                 while (_temp->next != temp) {
  202.                     _temp = _temp->next;
  203.                 }
  204.                 _temp->next = temp->next;
  205.  
  206.                 if (temp == last) {
  207.                     _temp->next = last->next;
  208.                     last = _temp;
  209.                 }
  210.  
  211.                 delete temp;
  212.                 break;
  213.             }
  214.        
  215.             if (temp->info != nr && temp == last) {
  216.                 goto exit;
  217.             }
  218.            
  219.         }
  220. exit:;
  221.  
  222. }
  223.  
  224.  
  225.  
  226. void Lista::DisplayList() {
  227.     cout << endl;
  228.     if (head) {
  229.         for (nod *temp = head; temp; temp = temp->next) {
  230.             cout << temp->info << " ,";
  231.         }
  232.         printf("%c%c%c", 8, 32, 8);
  233.     }
  234.     else {
  235.         cout << "Lista este vida";
  236.     }
  237.    
  238. }
  239.  
  240. void Lista::Insert() {
  241.    
  242.    
  243.     if (head && head->next) {
  244.         for (nod * temp = head; temp; temp = temp->next) {
  245.             nod *_temp = head;
  246.            
  247.         }
  248.     }
  249. }
  250.  
  251. void main() {
  252.     Lista cevapar,cevaimpar;
  253.     int n;
  254.     cout << "n = ";
  255.     cin >> n;
  256.     for (int i = 0; i < n; i++) {
  257.         int nr;
  258.         cin >> nr;
  259.         if (nr % 2 == 0) {
  260.             cevapar.Append(nr);
  261.         }
  262.         else {
  263.             cevaimpar.Append(nr);
  264.         }
  265.     }
  266.     cevapar.DisplayList();
  267.     cevaimpar.DisplayList();
  268.     _getch();
  269. }
  270.  
  271.  
  272. // din 2,2,3,3,2 , scapi de "2" si ramane doar "3" adica 3,3
  273.  
  274.  
  275. //#############################################################
  276. // adauga intre nod-uri un nou nod, si acel nou nod este suma intre nodul din urma lui si nodul urmator , ex: 1,2,3 => 1,3,5,3
  277. #include <iostream>
  278. #include <conio.h>
  279. #include <stdio.h>
  280.  
  281. using namespace std;
  282.  
  283. class Lista {
  284. private:
  285.     struct nod {
  286.         int info;
  287.         nod *next;
  288.     };
  289.     nod *head, *last;
  290. public:
  291.     Lista();
  292.     void Append(int nr = 0);
  293.     void Remove(int poz);
  294.     void Insert();
  295.     void DisplayList();
  296.     ~Lista();
  297. };
  298. Lista::~Lista() {
  299.  
  300.     cout << "\nDestructor\n";
  301.    
  302. }
  303.  
  304. Lista::Lista() {
  305.     this->head = NULL;
  306.     //this->last = head;
  307. }
  308.  
  309. void Lista::Append(int nr) {
  310.     nod *temp = new nod;
  311.     temp->info = nr;
  312.     if (head) {
  313.         last->next = temp;
  314.         last = temp;
  315.         last->next = NULL;
  316.     }
  317.     else {
  318.         head = temp;
  319.         head->next = NULL;
  320.         last = head;
  321.     }
  322. }
  323.  
  324.  
  325. void Lista::Remove(int nr) {
  326.     for (;;)
  327.         for (nod *temp = head; temp; temp = temp->next) {
  328.        
  329.             if (head->info == nr) {
  330.  
  331.                 if (head->next != NULL) {
  332.                     nod * _temp = head;
  333.                     head = head->next;
  334.                     delete _temp;
  335.                     break;
  336.                 }
  337.                 else {
  338.                     nod *_temp = head;
  339.                     last = head = NULL;
  340.                     delete _temp;
  341.                     goto exit;
  342.                 }
  343.             }
  344.        
  345.             if (temp->info == nr) {
  346.                 nod* _temp = head;
  347.                 while (_temp->next != temp) {
  348.                     _temp = _temp->next;
  349.                 }
  350.                 _temp->next = temp->next;
  351.  
  352.                 if (temp == last) {
  353.                     _temp->next = last->next;
  354.                     last = _temp;
  355.                 }
  356.  
  357.                 delete temp;
  358.                 break;
  359.             }
  360.        
  361.             if (temp->info != nr && temp == last) {
  362.                 goto exit;
  363.             }
  364.            
  365.         }
  366. exit:;
  367.  
  368. }
  369.  
  370.  
  371.  
  372. void Lista::DisplayList() {
  373.     cout << endl;
  374.     if (head) {
  375.         for (nod *temp = head; temp; temp = temp->next) {
  376.             cout << temp->info << " ,";
  377.         }
  378.         printf("%c%c%c", 8, 32, 8);
  379.     }
  380.     else {
  381.         cout << "Lista este vida";
  382.     }
  383.    
  384. }
  385.  
  386. void Lista::Insert() {
  387.    
  388.    
  389.     if (head && head->next) {
  390.         for (nod * temp = head; temp->next; temp = temp->next) {
  391.             nod *noduletz = new nod;
  392.             nod *_temp = temp->next;
  393.             noduletz->info = temp->info + _temp->info;
  394.             noduletz->next = temp->next;
  395.             temp->next = noduletz;
  396.             temp = temp->next;
  397.         }
  398.     }
  399. }
  400.  
  401. void main() {
  402.     Lista cevapar,cevaimpar,ceva;
  403.     int n;
  404. /*  cout << "n = ";
  405.     cin >> n;
  406.     for (int i = 0; i < n; i++) {
  407.         int nr;
  408.         cin >> nr;
  409.         if (nr % 2 == 0) {
  410.             cevapar.Append(nr);
  411.         }
  412.         else {
  413.             cevaimpar.Append(nr);
  414.         }
  415.     }
  416.     cevapar.DisplayList();
  417.     cevaimpar.DisplayList();*/
  418.     ceva.Append(1);
  419.     ceva.Append(2);
  420.     ceva.Append(3);
  421.     ceva.Append(4);
  422.     ceva.DisplayList();
  423.     ceva.Insert();
  424.     ceva.DisplayList();
  425.     _getch();
  426. }
  427.  
  428.  
  429. // din 2,2,3,3,2 , scapi de "2" si ramane doar "3" adica 3,3
  430.  
  431. //##################################################################
  432. //scoate toate nr pare din lista
  433.  
  434. #include <iostream>
  435. #include <conio.h>
  436. #include <stdio.h>
  437.  
  438. using namespace std;
  439.  
  440. class Lista {
  441. private:
  442.     struct nod {
  443.         int info;
  444.         nod *next;
  445.     };
  446.     nod *head, *last;
  447. public:
  448.     Lista();
  449.     void Append(int nr = 0);
  450.     void Remove(int poz);
  451.     void RemovePar();
  452.     void Insert();
  453.     void DisplayList();
  454.     ~Lista();
  455. };
  456. Lista::~Lista() {
  457.  
  458.     cout << "\nDestructor\n";
  459.  
  460. }
  461.  
  462. Lista::Lista() {
  463.     this->head = NULL;
  464.     //this->last = head;
  465. }
  466.  
  467. void Lista::Append(int nr) {
  468.     nod *temp = new nod;
  469.     temp->info = nr;
  470.     if (head) {
  471.         last->next = temp;
  472.         last = temp;
  473.         last->next = NULL;
  474.     }
  475.     else {
  476.         head = temp;
  477.         head->next = NULL;
  478.         last = head;
  479.     }
  480. }
  481.  
  482.  
  483. void Lista::Remove(int nr) {
  484.     for (;;)
  485.         for (nod *temp = head; temp; temp = temp->next) {
  486.  
  487.             if (head->info == nr) {
  488.  
  489.                 if (head->next != NULL) {
  490.                     nod * _temp = head;
  491.                     head = head->next;
  492.                     delete _temp;
  493.                     break;
  494.                 }
  495.                 else {
  496.                     nod *_temp = head;
  497.                     last = head = NULL;
  498.                     delete _temp;
  499.                     goto exit;
  500.                 }
  501.             }
  502.  
  503.             if (temp->info == nr) {
  504.                 nod* _temp = head;
  505.                 while (_temp->next != temp) {
  506.                     _temp = _temp->next;
  507.                 }
  508.                 _temp->next = temp->next;
  509.  
  510.                 if (temp == last) {
  511.                     _temp->next = last->next;
  512.                     last = _temp;
  513.                 }
  514.  
  515.                 delete temp;
  516.                 break;
  517.             }
  518.  
  519.             if (temp->info != nr && temp == last) {
  520.                 goto exit;
  521.             }
  522.  
  523.         }
  524. exit:;
  525.  
  526. }
  527.  
  528.  
  529.  
  530. void Lista::DisplayList() {
  531.     cout << endl;
  532.     if (head) {
  533.         for (nod *temp = head; temp; temp = temp->next) {
  534.             cout << temp->info << " ,";
  535.         }
  536.         printf("%c%c%c", 8, 32, 8);
  537.     }
  538.     else {
  539.         cout << "Lista este vida";
  540.     }
  541.  
  542. }
  543.  
  544. void Lista::Insert() {
  545.  
  546.  
  547.     if (head && head->next) {
  548.         for (nod * temp = head; temp->next; temp = temp->next) {
  549.             nod *noduletz = new nod;
  550.             nod *_temp = temp->next;
  551.             noduletz->info = temp->info + _temp->info;
  552.             noduletz->next = temp->next;
  553.             temp->next = noduletz;
  554.             temp = temp->next;
  555.         }
  556.     }
  557. }
  558.  
  559. void Lista::RemovePar() {
  560.     for (;;)
  561.         for (nod *temp = head; temp; temp = temp->next) {
  562.  
  563.             if (head->info %2== 0) {
  564.  
  565.                 if (head->next != NULL) {
  566.                     nod * _temp = head;
  567.                     head = head->next;
  568.                     delete _temp;
  569.                     break;
  570.                 }
  571.                 else {
  572.                     nod *_temp = head;
  573.                     last = head = NULL;
  574.                     delete _temp;
  575.                     goto exit;
  576.                 }
  577.             }
  578.  
  579.             if (temp->info %2 == 0) {
  580.                 nod* _temp = head;
  581.                 while (_temp->next != temp) {
  582.                     _temp = _temp->next;
  583.                 }
  584.                 _temp->next = temp->next;
  585.  
  586.                 if (temp == last) {
  587.                     _temp->next = last->next;
  588.                     last = _temp;
  589.                 }
  590.  
  591.                 delete temp;
  592.                 break;
  593.             }
  594.  
  595.             if (temp->info%2 != 0  && temp == last) {
  596.                 goto exit;
  597.             }
  598.  
  599.         }
  600. exit:;
  601. }
  602.  
  603. void main() {
  604.     Lista cevapar, cevaimpar, ceva;
  605.     int n;
  606.     /*  cout << "n = ";
  607.     cin >> n;
  608.     for (int i = 0; i < n; i++) {
  609.     int nr;
  610.     cin >> nr;
  611.     if (nr % 2 == 0) {
  612.     cevapar.Append(nr);
  613.     }
  614.     else {
  615.     cevaimpar.Append(nr);
  616.     }
  617.     }
  618.     cevapar.DisplayList();
  619.     cevaimpar.DisplayList();*/
  620.     ceva.Append(1);
  621.     ceva.Append(2);
  622.     ceva.Append(3);
  623.     ceva.Append(4);
  624.     ceva.Append(6);
  625.     ceva.Append(12);
  626.     //ceva.Append(7);
  627.     ceva.DisplayList();
  628.     ceva.Insert();
  629.     ceva.DisplayList();
  630.     ceva.RemovePar();
  631.     ceva.DisplayList();
  632.     _getch();
  633. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement