Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. // lista.cpp : Dyefines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include "conio.h"
  7. #include "stdlib.h"
  8.  
  9.  
  10.  
  11. struct Element
  12. {
  13. struct Element *next, *prev;
  14. int wartosc;
  15. };
  16.  
  17. struct Element * tail = NULL;
  18. struct Element * head = NULL;
  19.  
  20.  
  21.  
  22. Element * init(){
  23. struct Element *wsk;
  24. wsk = (struct Element *)malloc(sizeof(struct Element));
  25. wsk->wartosc = NULL;
  26. wsk->next = NULL;
  27. wsk->prev = NULL;
  28. head = wsk;
  29. return wsk;
  30. }
  31.  
  32.  
  33.  
  34. int raz = 1;
  35. bool dodaj(Element * glowa, int x){
  36. Element * prev = NULL;
  37. Element * wsk = glowa;
  38. Element * tmp;
  39.  
  40.  
  41. if (wsk->wartosc == 0)
  42. {
  43. // tmp = head;
  44. wsk->wartosc = x;
  45. wsk->next = NULL;
  46. wsk->prev = NULL;
  47. return true;
  48. }
  49.  
  50.  
  51. /*while (raz)
  52. {
  53. wsk->wartosc = x;
  54. wsk->next = wsk;
  55. raz--;
  56. return true;
  57.  
  58. }
  59.  
  60.  
  61. if (wsk->wartosc == x)
  62. {
  63. printf("Element o wartosci %d znajduje sie juz na liscie\n", wsk->wartosc);
  64. return false;
  65.  
  66. }*/
  67.  
  68.  
  69.  
  70.  
  71. while (wsk->next != NULL)
  72. {
  73. prev = wsk;
  74. wsk = wsk->next;
  75.  
  76. if (wsk->wartosc == x)
  77. {
  78. printf("Element o wartosci %d znajduje sie juz na liscie\n", wsk->wartosc);
  79. return false;
  80.  
  81. }
  82.  
  83.  
  84. if (x < wsk->wartosc)
  85. {
  86.  
  87. tmp = wsk;
  88. wsk->wartosc = x;
  89. tmp->prev = NULL;
  90. wsk = tmp;
  91. return true;
  92. }
  93.  
  94. if (wsk->wartosc > x)
  95. {
  96. while (wsk != NULL) {
  97. tmp = wsk->prev;
  98. wsk->prev = (struct Element *)malloc(sizeof(struct Element));
  99. wsk->prev->next = wsk;
  100. wsk->prev->prev = tmp;
  101. wsk->prev->wartosc = x;
  102. wsk->prev->prev->next = wsk->prev;
  103.  
  104. return true;
  105. }
  106.  
  107. }
  108. else
  109. return true;
  110. if (wsk->next == NULL) break;
  111. }
  112.  
  113. if (wsk->next == NULL)
  114. {
  115. wsk->next = (struct Element *) malloc(sizeof(struct Element));
  116. wsk->next->prev = wsk;
  117. wsk = wsk->next;
  118. wsk->wartosc = x;
  119. wsk->next = NULL;
  120. tail = wsk;
  121. return true;
  122. }
  123. }
  124.  
  125. void wypisz(Element * glowa)
  126. {
  127. while (glowa != NULL)
  128. {
  129. printf("%d ", glowa->wartosc);
  130. glowa = glowa->next;
  131. }
  132. }
  133.  
  134. int _tmain(int argc, _TCHAR* argv[])
  135. {
  136.  
  137. Element *glowa;
  138. glowa = init();
  139.  
  140. dodaj(glowa, 99);
  141. wypisz(glowa);
  142. system("PAUSE");
  143. dodaj(glowa, 1167);
  144. wypisz(glowa);
  145. system("PAUSE");
  146. dodaj(glowa, 5);
  147. wypisz(glowa);
  148. system("PAUSE");
  149. dodaj(glowa, 1000000);
  150. wypisz(glowa);
  151. system("PAUSE");
  152. dodaj(glowa, 67);
  153. wypisz(glowa);
  154. system("PAUSE");
  155. dodaj(glowa, 66);
  156. wypisz(glowa);
  157. system("PAUSE");
  158. dodaj(glowa, 22);
  159. wypisz(glowa);
  160. system("PAUSE");
  161. dodaj(glowa, 39183);
  162. wypisz(glowa);
  163. system("PAUSE");
  164. dodaj(glowa, 9);
  165. wypisz(glowa);
  166. system("PAUSE");
  167. dodaj(glowa, 83721);
  168. wypisz(glowa);
  169. system("PAUSE");
  170. return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement