Advertisement
Guest User

Untitled

a guest
May 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. /*
  6. listy dwukierunkowe
  7. 1. dodawanie elementow
  8. a) na koniec
  9. b) na poczatek
  10. c) po elemencie o podanej wartosci data
  11.  
  12. 2. usuwanie elementow
  13. a) z konca
  14. b) z poczatku
  15. c) o podanej wartosci
  16.  
  17. 3. wyswietlanie
  18. a) od konca
  19. b) od poczatku
  20.  
  21. 4. wyszukiwanie elementu o podanym numerze
  22.  
  23. 5. menu w mainie
  24. */
  25.  
  26. struct element
  27. {
  28. int data;
  29. struct element *next;
  30. struct element *prev;
  31. };
  32.  
  33. struct lista
  34. {
  35. struct element *head;
  36. struct element *tail;
  37. int count;
  38. };
  39.  
  40. void pushBack(int newdata, struct lista* list);
  41. void pushFront (int newdata, struct lista* list);
  42. void pushAfter (int newdata, struct lista* list);
  43. void popBack(struct lista* list);
  44. void popFront(struct lista* list);
  45. void popAfter(struct lista* list);
  46. void wyswietlFront(struct lista* list);
  47. void wyswietlBack(struct lista* list);
  48. struct element *szukaj2(struct lista* list);
  49. int szukaj (struct lista* list);
  50.  
  51. int main()
  52. {
  53. int wybor1,wartosc;
  54. char wybor2;
  55. struct lista* list = (struct element*)malloc(sizeof(struct element));
  56. list->head = NULL;
  57. list->tail = NULL;
  58. list->count = 0;
  59. do
  60. {
  61. printf("count %d\n\n", list->count);
  62. printf("LISTY DWUKIERUNKOWE:\n1.Dodanie elementu\n2.Usuniecie elementu\n3.Wyswietlenie\n4.Wyszukiwanie elementu o podanym numerze\n5.Zakonczenie dzialania programu\nWybor: ");
  63. scanf("%d", &wybor1);
  64. if (wybor1 == 1)
  65. {
  66. printf("\na. na koniec listy\nb. na poczatek listy\nc. po elemencie o podanej wartosci\nWybor (a/b/c):");
  67. getchar();
  68. scanf("%c", &wybor2);
  69. switch (wybor2)
  70. {
  71. case 'a':
  72. printf("Podaj wartosc dodawanego elementu: ");
  73. scanf("%d", &wartosc);
  74. pushBack(wartosc, list);
  75. break;
  76.  
  77. case 'b':
  78. printf("Podaj wartosc dodawanego elementu: ");
  79. scanf("%d", &wartosc);
  80. pushFront(wartosc, list);
  81. break;
  82.  
  83. case 'c':
  84. printf("Podaj wartosc dodawanego elementu: ");
  85. scanf("%d", &wartosc);
  86. pushAfter(wartosc, list);
  87.  
  88. break;
  89.  
  90. default:
  91. printf("Podano wartosc spoza zakresu");
  92. break;
  93. }
  94. }
  95.  
  96. if (wybor1 == 2)
  97. {
  98. printf("\na. z konca listy\nb. z poczatku listy\nc. elementu o podanej wartosci\nWybor (a/b/c):");
  99. getchar();
  100. scanf("%c", &wybor2);
  101. switch (wybor2)
  102. {
  103. case 'a':
  104. popBack(list);
  105. break;
  106.  
  107. case 'b':
  108. popFront(list);
  109. break;
  110.  
  111. case 'c':
  112. popAfter(list);
  113. break;
  114.  
  115.  
  116. default:
  117. printf("Podano wartosc spoza zakresu");
  118. break;
  119. }
  120. }
  121.  
  122. if (wybor1 == 3)
  123. {
  124. printf("\na. od konca\nb. od poczatku\nWybor (a/b/c):");
  125. getchar();
  126. scanf("%c", &wybor2);
  127. switch (wybor2)
  128. {
  129. case 'a':
  130. wyswietlBack(list);
  131. break;
  132.  
  133. case 'b':
  134. wyswietlFront(list);
  135. break;
  136.  
  137. default:
  138. printf("Podano wartosc spoza zakresu");
  139. break;
  140. }
  141. }
  142.  
  143. if (wybor1 == 4)
  144. {
  145. int indeks=szukaj(list);
  146. if (indeks == 0) printf("Szukany element nie znajduje sie na liscie.\n");
  147. else printf("Szukany element jest elementem nr %d.\n", indeks);
  148. }
  149.  
  150. if (wybor1 == 5) printf("Koncze dzialanie programu");
  151.  
  152.  
  153. getchar();
  154. getchar();
  155. system("cls");
  156. } while (wybor1 != 5);
  157. return 0;
  158. }
  159.  
  160.  
  161. void pushBack(int newdata, struct lista* list)
  162. {
  163. struct element* newEl = (struct element*)malloc(sizeof(struct element));
  164. newEl->data = newdata;
  165. newEl->next = NULL;
  166. if (list->count == 0)
  167. {
  168. list->head=newEl;
  169. list->tail=newEl;
  170. newEl->prev=NULL;
  171. }
  172. else
  173. {
  174. newEl->prev=list->tail;
  175. list->tail->next=newEl;
  176. list->tail=newEl;
  177. }
  178. list->count++;
  179. }
  180.  
  181. void pushFront (int newdata, struct lista* list)
  182. {
  183. struct element* newEl = (struct element*)malloc(sizeof(struct element));
  184. newEl->data = newdata;
  185. newEl->prev = NULL;
  186. if (list->count == 0)
  187. {
  188. list->head=newEl;
  189. list->tail=newEl;
  190. newEl->next = NULL;
  191. }
  192. else
  193. {
  194. newEl->next=list->head;
  195. list->head->prev=newEl;
  196. list->head=newEl;
  197. }
  198. list->count++;
  199. }
  200.  
  201. void pushAfter(int newdata, struct lista* list)
  202. {
  203. struct element* newEl = (struct element*)malloc(sizeof(struct element));
  204. struct element* szukany = (struct element*)malloc(sizeof(struct element));
  205. szukany=szukaj2(list);
  206. if (szukany == NULL) pushBack(newdata, list);
  207. else
  208. {
  209. if (szukany == list->tail) pushBack(newdata, list);
  210. else
  211. {
  212. newEl->data = newdata;
  213. newEl->prev = szukany;
  214. newEl->next = szukany->next;
  215. szukany->next->prev = newEl;
  216. szukany->next = newEl;
  217. list->count++;
  218. }
  219. }
  220.  
  221. }
  222.  
  223. void popBack(struct lista* list)
  224. {
  225. if (list->count == 0) printf("Lista jest pusta\n");
  226. if (list->count == 1)
  227. {
  228. free(list->tail);
  229. free(list->head);
  230. list->count=0;
  231. return;
  232. }
  233. if (list->count > 1)
  234. {
  235. list->tail=list->tail->prev;
  236. free(list->tail->next);
  237. list->tail->next=NULL;
  238. list->count--;
  239. }
  240.  
  241. }
  242.  
  243. void popFront(struct lista* list)
  244. {
  245. if (list->count == 0) printf("Lista jest pusta\n");
  246. if (list->count == 1)
  247. {
  248. free(list->head);
  249. free(list->tail);
  250. list->count=0;
  251. return;
  252. }
  253. if (list->count > 1)
  254. {
  255. list->head=list->head->next;
  256. free(list->head->prev);
  257. list->head->prev=NULL;
  258. list->count--;
  259. }
  260.  
  261. }
  262.  
  263. void popAfter(struct lista* list)
  264. {
  265. struct element* usuwany = (struct element*)malloc(sizeof(struct element));
  266. usuwany=szukaj2(list);
  267. if (usuwany == NULL) return;
  268.  
  269. else
  270. {
  271.  
  272. if (usuwany->prev == NULL)
  273. {
  274. popFront(list);
  275. return;
  276. }
  277. if (usuwany->next == NULL)
  278. {
  279. popBack(list);
  280. return;
  281. }
  282. if (usuwany->prev && usuwany->next)
  283. {
  284. usuwany->next->prev = usuwany->prev;
  285. usuwany->prev->next = usuwany->next;
  286. free(usuwany);
  287. list->count--;
  288. }
  289. }
  290. }
  291.  
  292. void wyswietlFront(struct lista* list)
  293. {
  294. if (list->count == 0) printf("Lista jest pusta\n");
  295. else
  296. {
  297. struct element* el = (struct element*)malloc(sizeof(struct element));
  298. el = list->head;
  299. while (el)
  300. {
  301. printf("%d, ", el->data);
  302. el = el->next;
  303. }
  304. }
  305. }
  306.  
  307. void wyswietlBack(struct lista* list)
  308. {
  309. if (list->count == 0) printf("Lista jest pusta\n");
  310. else
  311. {
  312. struct element* el = (struct element*)malloc(sizeof(struct element));
  313. el = list->tail;
  314. while (el)
  315. {
  316. printf("%d, ", el->data);
  317. el = el->prev;
  318. }
  319. }
  320. }
  321.  
  322. struct element *szukaj2(struct lista* list)
  323. {
  324. int szukana;
  325. printf("Podaj wartosc elementu o ktory Ci chodzi: ");
  326. scanf("%d", &szukana);
  327.  
  328. struct element* el = (struct element*)malloc(sizeof(struct element));
  329. el = list->head;
  330. if (list->count == 0) return NULL;
  331. else
  332. {
  333. while (1)
  334. {
  335. if (el->data == szukana) break;
  336. if (el->next) el = el->next;
  337. else return NULL;
  338. }
  339. return el;
  340. }
  341. }
  342.  
  343. int szukaj (struct lista* list)
  344. {
  345. int szukana,indeks=1;
  346. printf("Podaj wartosc elementu ktorego szukasz: ");
  347. scanf("%d", &szukana);
  348.  
  349. if (list->count == 0) return 0;
  350. else
  351. {
  352. struct element* el = (struct element*)malloc(sizeof(struct element));
  353. el = list->head;
  354. while (1)
  355. {
  356. if (el->data == szukana) break;
  357.  
  358. if (el->next)
  359. {
  360. el = el->next;
  361. indeks++;
  362. }
  363. else return 0;
  364. }
  365. return indeks;
  366. }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement