Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 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;
  18. struct Element * head;
  19. Element * init(){
  20. Element * nowa_lista = (struct Element *) malloc(sizeof(struct Element));
  21. nowa_lista->wartosc = NULL;
  22. nowa_lista->next = NULL;
  23. nowa_lista->prev = NULL;
  24. head = nowa_lista;
  25. return nowa_lista;
  26. }
  27.  
  28.  
  29.  
  30. //void dodajlos(lista * l)
  31.  
  32.  
  33. bool dodaj(Element * glowa, int x){
  34. Element * prev = NULL;
  35. Element * wsk = glowa;
  36. Element * tmp;
  37.  
  38.  
  39.  
  40. /*if(wsk == NULL)
  41. {
  42. *glowa = nowy_element;
  43. wsk = nowy_element;
  44. //*glowa->next = NULL;
  45. //*glowa->prev = NULL;
  46. return 0;
  47. }*/
  48.  
  49. while (wsk->next != NULL)
  50. {
  51. prev = wsk;
  52. wsk = wsk->next;
  53.  
  54. if (wsk->wartosc == x)
  55. {
  56. printf("Element o wartosci %d znajduje sie juz na liscie", wsk->wartosc);
  57. return false;
  58.  
  59. }
  60.  
  61. if (wsk->wartosc > x)
  62. {
  63. tmp = wsk->prev;
  64. wsk->prev = (struct Element *)malloc(sizeof(struct Element));
  65. wsk->prev->next = wsk;
  66. wsk->prev->prev = tmp;
  67. wsk->prev->wartosc = x;
  68. wsk->prev->prev->next = wsk->prev;
  69. return true;
  70. }
  71.  
  72. if (wsk->next == NULL) break;
  73. }
  74.  
  75. if (wsk->next == NULL)
  76. {
  77. wsk->next = (struct Element *)malloc(sizeof(struct Element));
  78. wsk->next->prev = wsk;
  79. wsk = wsk->next;
  80. wsk->wartosc = x;
  81. wsk->next = NULL;
  82. tail = wsk;
  83. return true;
  84. }
  85. }
  86.  
  87. /*i
  88. else
  89. {
  90.  
  91. if (wsk->wartosc < x && wsk->prev == NULL && wsk->next = NULL)
  92. {
  93. wsk->next = nowy_element;
  94. nowy_element->prev = wsk;
  95. return 0;
  96. }
  97.  
  98. else
  99.  
  100. {
  101. if (!prev)
  102. {
  103. wsk->prev = nowy_element;
  104. nowy_element->next = wsk;
  105. wsk = nowy_element;
  106. nowy_element->prev = NULL;
  107. return 0;
  108. }
  109. else
  110. {
  111. prev->next = nowy_element;
  112. nowy_element->next = *glowa;
  113. return 0;
  114. }
  115. wsk = wsk->next;
  116. }
  117.  
  118.  
  119.  
  120. wsk->next = nowy_element;
  121. nowy_element->next = NULL;
  122. return 0;
  123. }
  124.  
  125. }*/
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. /*for (i = 0; i <= licz; i++)
  139. {
  140. if (l[i].wartosc == x)
  141. {
  142. czy = 1;
  143. }
  144. // else
  145. // l = l->next;
  146. }
  147. l = l;
  148. if (l->next != NULL && czy != 1)
  149. {
  150. //l = l->next;
  151. l->next = nowy_element;
  152. }
  153. else
  154. cout << "Taka wartosc juz istnieje" << endl;
  155.  
  156.  
  157. }*/
  158.  
  159.  
  160. void wypisz(Element * glowa)
  161. {
  162. while (glowa != NULL)
  163. {
  164. printf("%d ", glowa->wartosc);
  165. glowa = glowa->next;
  166. }
  167. }
  168.  
  169. int _tmain(int argc, _TCHAR* argv[])
  170. {
  171.  
  172. Element *glowa;
  173. glowa = init();
  174. //Element *glowa = (l);
  175.  
  176. //lista * l = new lista;
  177.  
  178. dodaj(glowa, 11);
  179. wypisz(glowa);
  180. system("PAUSE");
  181. dodaj(glowa, 115);
  182. wypisz(glowa);
  183. system("PAUSE");
  184. dodaj(glowa, 56);
  185. wypisz(glowa);
  186. system("PAUSE");
  187. dodaj(glowa, 117);
  188. wypisz(glowa);
  189. system("PAUSE");
  190. return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement