Advertisement
dizzy94

JJ

May 22nd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node {
  6.     int data;
  7.     Node* next;
  8. };
  9. Node* arrayToList(const int arr[], size_t size) {
  10.     Node *pierwsza = new Node();
  11.     pierwsza->data = arr[0];
  12.     Node *current = pierwsza;
  13.     for (int i = 1; i < size; i++) {
  14.         Node *nowa = new Node();
  15.         nowa->data = arr[i];
  16.         current->next = nowa;
  17.         current = nowa;
  18.     }
  19.     return pierwsza;
  20. }
  21.  
  22.  
  23. void showList(const Node* head) {
  24.     if (head != NULL) {
  25.         while (head != NULL) {
  26.             cout << head->data << endl;
  27.             head = head->next;
  28.         }
  29.     }
  30.     else {
  31.         cout << "List is empty" << endl;
  32.     }
  33. }
  34. void deleteList(Node*& head) {
  35.  
  36.     Node *temp;
  37.     temp = head;
  38.     while (head != NULL)
  39.     {
  40.         head = head->next;
  41.         delete temp;
  42.         temp = head;
  43.         std::cout << "Spadam stad" << std::endl;
  44.     }
  45. }
  46.  
  47. Node* removeOdd(Node* head)
  48. {
  49.     Node *aktualny = head;
  50.     Node *poprzedni = NULL;
  51.     while (aktualny != NULL)
  52.     {
  53.         if (aktualny->data % 2 != 0)
  54.         {
  55.             if (aktualny == head)
  56.                 head = head->next;
  57.             else
  58.                 poprzedni->next = aktualny->next;
  59.             Node* tmp = aktualny;
  60.             aktualny = aktualny->next;
  61.             delete tmp;
  62.         }
  63.         else
  64.         {
  65.             poprzedni = aktualny;
  66.             aktualny = aktualny->next;
  67.         };
  68.     };
  69.  
  70.     return head;
  71. };
  72.  
  73. int main() {
  74.     int arr[] = { 1,2,3,4,5,6 };
  75.     size_t size = sizeof(arr) / sizeof(*arr);
  76.     Node* head = arrayToList(arr, size);
  77.     showList(head);
  78.     head = removeOdd(head);
  79.     showList(head);
  80.     deleteList(head);
  81.     showList(head);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement