Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct lista
  6. {
  7.     string imie;
  8.     int wiek;
  9.     lista* next;
  10. };
  11.  
  12. void DodajElement(string name, int age, lista*&glowa)
  13. {
  14.     lista *NowyElListy, *tmp = glowa;
  15.  
  16.     NowyElListy = new lista;
  17.     NowyElListy->imie = name;
  18.     NowyElListy->wiek = age;
  19.     NowyElListy->next = NULL;
  20.  
  21.     if(glowa==NULL)
  22.     {
  23.         glowa=NowyElListy;
  24.     }
  25.  
  26.  
  27.     else
  28.     {
  29.  
  30.         while(tmp->next!=NULL)
  31.         {
  32.             tmp=tmp->next;
  33.         }
  34.         tmp->next = NowyElListy;
  35.     }
  36.  
  37. }
  38.  
  39. void drukuj(lista* glowa)
  40. {
  41.     cout<<"LISTA:\n";
  42.     if(!glowa)
  43.     {
  44.         cout<<"BRAK ELEMENTÓW w LISCIE";
  45.     }
  46.     else
  47.     {
  48.         while(glowa)
  49.         {
  50.             cout<<glowa->wiek<<endl;
  51.             glowa=glowa->next;
  52.         }
  53.     }
  54. }
  55.  
  56. bool CzyPosortowana(lista* glowa)
  57. {
  58.     while(glowa->next)
  59.     {
  60.         if(glowa->next->wiek < glowa->wiek)
  61.             return 0;
  62.         glowa=glowa->next;
  63.     }
  64.     return 1;
  65. }
  66.  
  67. void DrugaTablica (lista* glowa, lista*&cel)
  68. {
  69.     lista *tmp;
  70.     while(glowa->next!=NULL)
  71.     {
  72.         if(glowa->next->wiek < glowa->wiek)
  73.         {
  74.             tmp = glowa->next;
  75.             DodajElement(tmp->imie, tmp->wiek, cel);
  76.             if(glowa->next->next==NULL)
  77.             {
  78.                 delete tmp;
  79.             }
  80.             else
  81.             {
  82.             glowa->next=glowa->next->next;
  83.             delete tmp;
  84.             }
  85.         }
  86.         glowa=glowa->next;
  87.     }
  88. }
  89.  
  90. int main()
  91. {
  92.     lista* glowa=NULL;
  93.     lista* druga=NULL;
  94.     DodajElement("Jan", 10, glowa);
  95.     DodajElement("Andrzej", 20, glowa);
  96.     DodajElement("Henryk", 5, glowa);
  97.     DodajElement("Józef", 50, glowa);
  98.     DodajElement("Dawid", 13, glowa);
  99.     drukuj(glowa);
  100.     DrugaTablica(glowa, druga);
  101.     drukuj(druga);
  102.     drukuj(glowa);
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement