Guest User

Untitled

a guest
May 27th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <class T>
  6. int compare(T zm1, T zm2)
  7. {
  8. if(zm1 > zm2)
  9. {
  10. return 1;
  11. }
  12. else if( zm1 < zm2)
  13. {
  14. return -1;
  15. }
  16. else
  17. {
  18. return 0;
  19. }
  20.  
  21. }
  22.  
  23. template <class A>
  24. class Lista
  25. {
  26. struct Element
  27. {
  28. A value;
  29. Element *next;
  30. };
  31. public:
  32. Element *lista;
  33. Lista(){lista=new Element;};
  34. Lista(const Lista&wzorzec);
  35. ~Lista(){};
  36. void dodajElement(A elm)
  37. {
  38. Element *nowy=new Element;
  39. nowy->value=elm;
  40. nowy->next=NULL;
  41. if(lista==NULL)
  42. {
  43. lista=nowy;
  44. }
  45. else
  46. {
  47. Element *poczatek=lista;
  48. while(poczatek->next!=NULL)
  49. {
  50. poczatek=poczatek->next;
  51. }
  52. poczatek->next=nowy;
  53. }
  54. }
  55.  
  56. void wyswietl()
  57. {
  58. Element *pocz=lista;
  59. pocz=pocz->next;
  60. while(pocz)
  61. {
  62. cout<<pocz->value<<endl;
  63. pocz=pocz->next;
  64. }
  65.  
  66. }
  67.  
  68. void sortuj()
  69. {
  70. Element *wyn=lista;
  71. Element *sort=lista;
  72. Element *sort2=lista;
  73. wyn=wyn->next;
  74. sort=sort->next;
  75. sort2=sort2->next->next;
  76. while(wyn)
  77. {
  78. if(compare(sort,sort2)==1)
  79. {
  80. wyn->next=sort2;
  81. sort=sort->next;
  82. sort2=sort2->next;
  83. }
  84. else if(compare(sort,sort2)==-1)
  85. {
  86. wyn->next=sort;
  87. sort=sort->next;
  88. sort2=sort2->next;
  89.  
  90. }
  91. else
  92. {
  93. wyn->next=sort;
  94. sort=sort->next;
  95. sort2=sort2->next;
  96.  
  97. }
  98. }
  99.  
  100. }
  101.  
  102. };
  103.  
  104. int main()
  105. {
  106. int ilosc,x;
  107. cout<<"Podaj ilosc liczb ktore chcesz umiescic na liscie: ";
  108. cin>>ilosc;
  109. Lista<int> a;
  110. while(ilosc!=0)
  111. {
  112. cout<<"Podaj wartosc do wpisania na liste: ";
  113. cin>>x;
  114. a.dodajElement(x);
  115. ilosc--;
  116. }
  117. a.sortuj();
  118. a.wyswietl();
  119.  
  120. }
Add Comment
Please, Sign In to add comment