Advertisement
Archangelpl

Untitled

Jun 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. // 1 way list.cpp: Określa punkt wejścia dla aplikacji konsoli.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. typedef struct lista
  9. {
  10. int id;
  11. struct lista *nast;
  12. }list;
  13. // w mainie head=NULL; !!
  14. struct lista *head;
  15. struct lista *temp;
  16. struct lista *temp1;
  17. struct lista *temp2;
  18. struct lista *temp3;
  19. struct lista *temp4;
  20. int ilosc = 0;
  21.  
  22. void stworz(int x)
  23. {
  24. temp = (list*)malloc(sizeof(list));
  25. temp ->nast = NULL;
  26. temp->id = x;
  27. ilosc++;
  28. }
  29.  
  30. void wczytwaniePrzod(int x)
  31. {
  32. if (head == NULL)
  33. {
  34. stworz(x);
  35. head = temp;
  36. temp1 = head;
  37. }
  38. else
  39. {
  40. stworz(x);
  41. temp->nast = head;
  42. head = temp;
  43.  
  44. }
  45. }
  46. void wczytanieTyl(int x)
  47. {
  48. if (head == NULL)
  49. {
  50. stworz(x);
  51. head = temp;
  52. temp1 = head;
  53. }
  54. else
  55. {
  56. stworz(x);
  57. {
  58. temp1->nast = temp;
  59. temp1 = temp;
  60. }
  61. }
  62. }
  63. void wypistyl()
  64. {
  65. list *temp = head;
  66. while (temp != NULL)
  67. {
  68. printf("%d ->", temp->id);
  69. temp = temp->nast;
  70. }
  71. printf("null\n");
  72. }
  73. dodaj(int x,int z)
  74. {
  75. int i = 1;
  76. temp2 = head;
  77. if (z<1 || z>ilosc + 1)
  78. {
  79. printf("wykraczamy po za zakres ");
  80. return;
  81. }
  82. if (head == NULL && z == 1)
  83. {
  84. stworz(x);
  85. head = temp;
  86. temp1 = head;
  87.  
  88. }
  89. else
  90. {
  91. while (i < z)
  92. {
  93. temp3 = temp2;
  94. temp2 = temp2->nast;
  95. i++;
  96. }
  97. stworz(x);
  98. temp3->nast = temp;
  99. temp3 = temp;
  100. temp3->nast = temp2;
  101. }
  102. }
  103. void sort()
  104. {
  105. int i, j, x;
  106.  
  107. temp2 = head;
  108. temp4 = head;
  109.  
  110. if (temp2 == NULL)
  111. {
  112. printf("\n List empty to sort");
  113. return;
  114. }
  115.  
  116. for (temp2 = head; temp2 != NULL; temp2 = temp2->nast)
  117. {
  118. for (temp4 = temp2->nast; temp4 != NULL; temp4 = temp4->nast)
  119. {
  120. if (temp2->id > temp4->id) // malejąco znak w drugą strone
  121. {
  122. x = temp2->id;
  123. temp2->id = temp4->id;
  124. temp4->id = x;
  125. }
  126. }
  127. }
  128. wypistyl();
  129. }
  130. void wymiana()
  131. {
  132. int data, data1;
  133.  
  134. printf("\n Wpisz liczbe ktora chcesz wymienic : ");
  135. scanf("%d", &data);
  136. printf("\n Wpisz nowa liczbe : ");
  137. scanf("%d", &data1);
  138. temp2 = head;
  139. if (temp2 == NULL)
  140. {
  141. printf("\n Error : Lista jest pusta");
  142. return;
  143. }
  144. while (temp2 != NULL)
  145. {
  146. if (temp2->id == data)
  147. {
  148.  
  149. temp2->id = data1;
  150. wypistyl();
  151. return;
  152. }
  153. else
  154. temp2 = temp2->nast;
  155. }
  156.  
  157. printf("\n Error : %d nie znaleziono liczby do wymiany", data);
  158. }
  159. void szukaj()
  160. {
  161. int data, ilosc = 0;
  162. temp2 = head;
  163.  
  164. if (temp2 == NULL)
  165. {
  166. printf("\n Error : List empty to search for data");
  167. return;
  168. }
  169. printf("\n Wpisz liczbe do wyszukania : ");
  170. scanf("%d", &data);
  171. while (temp2 != NULL)
  172. {
  173. if (temp2->id == data)
  174. {
  175. printf("\n liczba znajduje sie na pozycji %d \n", ilosc + 1);
  176.  
  177. }
  178. temp2 = temp2->nast;
  179. ilosc++;
  180. }
  181. if(temp2==NULL)
  182. printf("\n Error : %d nie znaleziono liczby na liscie", data);
  183. }
  184. int main()
  185. {
  186. head =NULL;
  187. wczytwaniePrzod(10);
  188. wczytwaniePrzod(20);
  189. wczytwaniePrzod(30);
  190.  
  191. wczytanieTyl(40);
  192. dodaj(50,3);
  193. wypistyl();
  194. sort();
  195. wymiana();
  196. szukaj();
  197. return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement