seergiomv

2

May 6th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "types.h"
  3. #include "center_list.h"
  4. #include "party_list.h"
  5.  
  6. #define CODE_LENGTH 2
  7.  
  8. void C(tCenterName NombreCentro, char param2[NAME_LENGTH_LIMIT+1], tListC *ListC, tList *Lista){
  9. tItemC tempItemC;
  10. strcpy(tempItemC.centerName,NombreCentro);
  11. tNumVotes TempNumVotes;
  12. TempNumVotes = atoi(param2);
  13. tempItemC.totalvoters = TempNumVotes;
  14.  
  15. tempItemC.partyList = LNULL;
  16. tempItemC.validVotes = 0;
  17. tempItemC.nullVotes = 0;
  18.  
  19. if(findItemC(NombreCentro,*ListC) != NULLC){
  20. printf("+ Error: Create not possible\n");
  21. } else {
  22. if (insertItemC(tempItemC, ListC) == 1) {
  23. printf("* Create: center %s totalvoters %d\n", tempItemC.centerName, tempItemC.totalvoters);
  24. } else {
  25. printf("+ Error: Create not possible\n");
  26. }
  27. }
  28. }
  29.  
  30. void N(tCenterName NombreCentro, tPartyName NombrePartido, tListC *ListC, tList *Lista){
  31. tItemC tempItemC;
  32. tempItemC = getItemC(findItemC(NombreCentro,*ListC),*ListC);
  33. if(findItemC(NombreCentro,*ListC) != NULLC){
  34. if (findItem(NombrePartido,tempItemC.partyList) == LNULL){
  35. tItemL tempItemL;
  36. strcpy(tempItemL.partyName,NombrePartido);
  37. tempItemL.numVotes = 0;
  38. if(insertItem(tempItemL,&tempItemC.partyList) == 1){
  39. updateListC(tempItemC.partyList,findItemC(NombreCentro,*ListC),ListC);
  40. printf("* New: center %s party %s\n",NombreCentro,NombrePartido);
  41. } else{
  42. printf("+ Error: New not possible\n");
  43. }
  44. } else{
  45. printf("+ Error: New not possible\n");
  46. }
  47. } else{
  48. printf("+ Error: New not possible\n");
  49. }
  50. }
  51.  
  52. void V(tCenterName NombreCentro, tPartyName NombrePartido, tListC *ListC, tList *Lista){
  53. tNumVotes tempNullVotes, tempValidVotes;
  54. if(findItemC(NombreCentro,*ListC) != NULLC){
  55. tItemC tempItemC;
  56. tempItemC = getItemC(findItemC(NombreCentro,*ListC),*ListC);
  57. tempNullVotes = tempItemC.nullVotes;
  58. tempValidVotes = tempItemC.validVotes;
  59. if(findItem(NombrePartido,tempItemC.partyList) != LNULL){
  60. tItemL partido;
  61. partido = getItem(findItem(NombrePartido,tempItemC.partyList),tempItemC.partyList);
  62. partido.numVotes = partido.numVotes + 1;
  63. tempValidVotes = tempValidVotes + 1;
  64. updateValidVotesC(tempValidVotes,findItemC(NombreCentro,*ListC),ListC);
  65. printf("* Vote: center %s party %s numvotes %d\n",NombreCentro,NombrePartido,partido.numVotes);
  66. }else{
  67. tempNullVotes = tempNullVotes + 1;
  68. updateNullVotesC(tempNullVotes,findItemC(NombreCentro,*ListC),ListC);
  69. printf("+ Error: Vote not possible. Party %s not found in center %s. NULLVOTE\n",NombrePartido,
  70. NombreCentro);
  71. }
  72. } else{
  73. printf("+ Error: Vote not possible\n");
  74. }
  75. }
  76.  
  77. void R(tListC *ListC){
  78. tItemC tempItemC;
  79. tPosC primero,ultimo;
  80. primero = firstC(*ListC);
  81. ultimo = lastC(*ListC);
  82. while (primero != NULLC){
  83. tempItemC = getItemC(primero,*ListC);
  84. if(tempItemC.validVotes == 0){
  85. printf("* Remove: center %s\n", tempItemC.centerName);
  86. deleteAtPositionC(primero,ListC);
  87. primero = firstC(*ListC);
  88. } else{
  89. primero = nextC(primero,*ListC);
  90. }
  91. }
  92. if (primero == ultimo){
  93. printf("+ Remove: no centers removed\n");
  94. }
  95. /*printf("* Remove: center %s\n", tempItemC.centerName);
  96. printf("+ Remove: no centers removed\n");*/
  97. }
  98.  
  99. void S(tListC *ListC,tList *Lista){
  100. tNumVotes totalValidVotes = 0;
  101. float porcentajePartido, porcentajeTotal;
  102. for(tPosC pos = firstC(*ListC); pos != NULLC; pos = nextC(pos,*ListC)){
  103. totalValidVotes = ListC->data[pos].validVotes + totalValidVotes;
  104. }
  105. for(tPosC pos1 = firstC(*ListC); pos1 != NULLC; pos1 = nextC(pos1,*ListC)){
  106. printf("Center %s\n",ListC->data[pos1].centerName);
  107. tNumVotes centerTotalVotes = 0;
  108. tItemC tempItemC;
  109. tempItemC = getItemC(pos1,*ListC);
  110. tPosL primero;
  111. primero = first(tempItemC.partyList);
  112. while(primero != LNULL) {
  113. tItemL TempItem;
  114. TempItem = getItem(primero,tempItemC.partyList);
  115. if (totalValidVotes == 0){
  116. porcentajePartido = 0;
  117. } else{
  118. porcentajePartido = (float) 100 * TempItem.numVotes/(float) totalValidVotes;
  119. }
  120. printf("Party %s numvotes %d (%.2f%%)\n",TempItem.partyName,TempItem.numVotes,porcentajePartido);
  121. primero = next(primero,tempItemC.partyList);
  122. }
  123. printf("Null votes %d\n",ListC->data[pos1].nullVotes);
  124. centerTotalVotes = ListC->data[pos1].nullVotes + ListC->data[pos1].validVotes;
  125. porcentajeTotal = (float) 100 * centerTotalVotes/ListC->data[pos1].totalvoters;
  126. printf("Participation: %d votes from %d voters (%.2f%%)\n",centerTotalVotes,ListC->data[pos1].totalvoters,
  127. porcentajeTotal);
  128. }
  129. }
  130.  
  131. void processCommand(char commandNumber[CODE_LENGTH+1], char command,
  132. char param1[NAME_LENGTH_LIMIT+1], char param2[NAME_LENGTH_LIMIT+1],tListC *TempListC,tList *TempListL) {
  133.  
  134. /*printf("Read from input file: %s %c %s %s\n", commandNumber, command, param1, param2);*/
  135.  
  136. switch(command) {
  137. case 'C': {
  138. printf("********************\n");
  139. printf("%s %c: center %s totalvoters %s\n\n",commandNumber,command,param1,param2);
  140. C(param1,param2,TempListC,TempListL);
  141. break;
  142. }
  143. case 'N': {
  144. printf("********************\n");
  145. printf("%s %c: center %s party %s\n\n",commandNumber, command, param1, param2);
  146. N(param1,param2,TempListC,TempListL);
  147. break;
  148. }
  149. case 'V': {
  150. printf("********************\n");
  151. printf("%s %c: center %s party %s\n\n",commandNumber, command, param1, param2);
  152. V(param1,param2,TempListC,TempListL);
  153. break;
  154. }
  155. case 'R': {
  156. printf("********************\n");
  157. printf("%s %c\n\n",commandNumber,command);
  158. R(TempListC);
  159. break;
  160. }
  161. case 'S': {
  162. printf("********************\n");
  163. printf("%s %c\n\n",commandNumber,command);
  164. S(TempListC,TempListL);
  165. break;
  166. }
  167. default: {
  168. break;
  169. }
  170. }
  171. }
  172.  
  173. void readTasks(char *filename,tListC *TempListC,tList *TempListL) {
  174. FILE *df;
  175. char commandNumber[CODE_LENGTH+1], command, param1[NAME_LENGTH_LIMIT+1], param2[NAME_LENGTH_LIMIT+1];
  176.  
  177. df = fopen(filename, "r");
  178. if (df != NULL) {
  179. while (!feof(df)) {
  180. char format[16];
  181. sprintf(format, "%%%is %%c ", CODE_LENGTH);
  182. fscanf(df, format, commandNumber, &command);
  183. if (command == 'S' || command == 'R') {
  184. param1[0] = '\0';
  185. param2[0] = '\0';
  186. } else {
  187. sprintf(format, "%%%is %%%is", NAME_LENGTH_LIMIT, NAME_LENGTH_LIMIT);
  188. fscanf(df, format, param1, param2);
  189. }
  190. processCommand(commandNumber, command, param1, param2, TempListC, TempListL);
  191. }
  192. fclose(df);
  193. } else {
  194. printf("Cannot open file %s.\n", filename);
  195. }
  196. }
  197.  
  198. int main(int nargs, char **args) {
  199.  
  200. char *filename = "new.txt";
  201.  
  202. if (nargs > 1) {
  203. filename = args[1];
  204. } else {
  205. #ifdef INPUT_FILE
  206. filename = INPUT_FILE;
  207. #endif
  208. }
  209.  
  210. tList *ListaPartidos = malloc(sizeof(*ListaPartidos));
  211. createEmptyList(ListaPartidos);
  212. tListC *ListaCentros = malloc(sizeof(*ListaCentros));
  213. createEmptyListC(ListaCentros);
  214. readTasks(filename,ListaCentros,ListaPartidos);
  215.  
  216. return 0;
  217. }
  218.  
  219. //////////////////////////////////////////////////////////////////////////
  220.  
  221. #ifndef CENTER_LIST_H
  222. #define CENTER_LIST_H
  223.  
  224. #include "types.h"
  225. #include "party_list.h"
  226.  
  227. #include <stdbool.h>
  228. #include <stdio.h>
  229. #include <stdlib.h>
  230. #include <string.h>
  231. #define NULLC -1
  232. #define MAX 10
  233.  
  234. typedef struct tItemC {
  235. tCenterName centerName;
  236. tNumVotes totalvoters;
  237. tNumVotes validVotes;
  238. tNumVotes nullVotes;
  239. tList partyList;
  240. } tItemC;
  241.  
  242.  
  243. typedef int tPosC;
  244. typedef struct {
  245. tItemC data[MAX];
  246. tPosC lastPos;
  247. }tListC;
  248.  
  249.  
  250. /* Write your code here... */
  251. bool insertItemC(tItemC, tListC*);
  252. /*{Objetivo:
  253. * Entradas:
  254. * Salidas:
  255. * Precondición:
  256. * Postcondición:}*/
  257. void updateListC (tList, tPosC, tListC*);
  258. /*{Objetivo:
  259. * Entradas:
  260. * Salidas:
  261. * Precondición:
  262. * Postcondición:}*/
  263. void updateValidVotesC (tNumVotes, tPosC, tListC*);
  264. /*{Objetivo:
  265. * Entradas:
  266. * Salidas:
  267. * Precondición:
  268. * Postcondición:}*/
  269. void updateNullVotesC (tNumVotes, tPosC, tListC*);
  270. /*{Objetivo:
  271. * Entradas:
  272. * Salidas:
  273. * Precondición:
  274. * Postcondición:}*/
  275. tPosC findItemC (tCenterName, tListC);
  276. /*{Objetivo:
  277. * Entradas:
  278. * Salidas:
  279. * Precondición:
  280. * Postcondición:}*/
  281. tPosC firstC(tListC);
  282. /*{Objetivo:
  283. * Entradas:
  284. * Salidas:
  285. * Precondición:
  286. * Postcondición:}*/
  287. tPosC lastC(tListC);
  288. /*{Objetivo:
  289. * Entradas:
  290. * Salidas:
  291. * Precondición:
  292. * Postcondición:}*/
  293. tPosC nextC(tPosC pos, tListC ListC);
  294. /*{Objetivo:
  295. * Entradas:
  296. * Salidas:
  297. * Precondición:
  298. * Postcondición:}*/
  299. bool copyListC(tListC, tListC*);
  300. /*{Objetivo:
  301. * Entradas:
  302. * Salidas:
  303. * Precondición:
  304. * Postcondición:}*/
  305. void deleteAtPositionC(tPosC, tListC*);
  306. /*{Objetivo:
  307. * Entradas:
  308. * Salidas:
  309. * Precondición:
  310. * Postcondición:}*/
  311. tPosC previousC(tPosC pos, tListC);
  312. /*{Objetivo:
  313. * Entradas:
  314. * Salidas:
  315. * Precondición:
  316. * Postcondición:}*/
  317. void createEmptyListC(tListC*);
  318. /*{Objetivo:
  319. * Entradas:
  320. * Salidas:
  321. * Precondición:
  322. * Postcondición:}*/
  323. bool isEmptyListC(tListC);
  324. /*{Objetivo:
  325. * Entradas:
  326. * Salidas:
  327. * Precondición:
  328. * Postcondición:}*/
  329. tItemC getItemC(tPosC, tListC);
  330. /*{Objetivo:
  331. * Entradas:
  332. * Salidas:
  333. * Precondición:
  334. * Postcondición:}*/
  335. void deleteCenter(tPosC, tListC*);
  336. /*{Objetivo:
  337. * Entradas:
  338. * Salidas:
  339. * Precondición:
  340. * Postcondición:}*/
  341. bool equalListC(tListC,tListC*);
  342. /*{Objetivo:
  343. * Entradas:
  344. * Salidas:
  345. * Precondición:
  346. * Postcondición:}*/
  347. #endif
  348.  
  349.  
  350. /////////////////////////////////////////////////////////////
  351.  
  352. #include "types.h"
  353. #include "party_list.h"
  354. #include "center_list.h"
  355.  
  356. /* Write your code here... */
  357.  
  358. bool insertItemC(tItemC ItemC,tListC *ListC){
  359. if(ListC->lastPos == MAX - 1){
  360. return false;
  361. } else{
  362. if(isEmptyListC(*ListC) || strcmp(ItemC.centerName, ListC->data[ListC->lastPos].centerName) > 0){
  363. ListC->lastPos++;
  364. ListC->data[ListC->lastPos] = ItemC;
  365. } else{
  366. ListC->lastPos++;
  367. tPosC primero;
  368. primero = firstC(*ListC);
  369. if(strcmp(ItemC.centerName,ListC->data[primero].centerName) < 0){
  370. tPosC pos;
  371. pos = ListC->lastPos;
  372. while ((pos > 0) && strcmp(ItemC.centerName,ListC->data[pos-1].centerName) < 0){
  373. ListC->data[pos] = ListC->data[pos-1];
  374. pos--;
  375. } ListC->data[pos] = ItemC;
  376. }else{
  377. for(tPosC pos = ListC->lastPos; pos >= 0 && strcmp(ItemC.centerName, ListC->data[pos-1].centerName) < 0;
  378. pos--){
  379. ListC->data[pos] = ItemC;
  380. }
  381. }
  382. } return true;
  383. }
  384. }
  385.  
  386. tPosC firstC(tListC ListC){
  387. return 0;
  388. }
  389.  
  390. tPosC lastC(tListC ListC){
  391. return ListC.lastPos;
  392. }
  393.  
  394. tPosC nextC(tPosC pos, tListC ListC){
  395. if (pos == lastC(ListC)){
  396. return NULLC;
  397. } else{
  398. return ++pos;
  399. }
  400. }
  401.  
  402. bool copyListC(tListC ListC, tListC *ListC1){
  403. tPosC pos;
  404. for(pos = 0; pos == ListC.lastPos; pos++){
  405. ListC1->data[pos] = ListC.data[pos];
  406. }
  407. ListC1->lastPos = ListC.lastPos;
  408. return true;
  409. }
  410.  
  411. void deleteAtPositionC(tPosC pos, tListC *ListC){
  412. while (pos != lastC(*ListC)){
  413. ListC->data[pos] = ListC->data[nextC(pos,*ListC)];
  414. pos = nextC(pos,*ListC);
  415. }
  416. ListC->lastPos--;
  417. }
  418.  
  419. tPosC previousC(tPosC pos, tListC ListC){
  420. if(pos == firstC(ListC)){
  421. return NULLC;
  422. } else{
  423. pos = pos - 1;
  424. }
  425. return pos;
  426. }
  427.  
  428. void createEmptyListC(tListC *ListC){
  429. ListC->lastPos = NULLC;
  430. }
  431.  
  432. bool isEmptyListC(tListC ListC){
  433. if(ListC.lastPos == NULLC){
  434. return true;
  435. } else{
  436. return false;
  437. }
  438. }
  439.  
  440. void updateValidVotesC (tNumVotes ValidVotes, tPosC pos, tListC *ListC){
  441. ListC->data[pos].validVotes = ValidVotes;
  442. }
  443.  
  444. void updateNullVotesC (tNumVotes NullVotes, tPosC pos, tListC *ListC){
  445. ListC->data[pos].nullVotes = NullVotes;
  446. }
  447.  
  448. tPosC findItemC (tCenterName NombreCentro, tListC ListC){
  449. tPosC pos;
  450. if(ListC.lastPos == NULLC){
  451. return NULLC;
  452. } else{
  453. pos = firstC(ListC);
  454. while((pos < ListC.lastPos) && (strcmp(ListC.data[pos].centerName, NombreCentro) != 0)){
  455. pos = nextC(pos,ListC);
  456. }
  457. if(strcmp(ListC.data[pos].centerName,NombreCentro) == 0){
  458. return pos;
  459. } else{
  460. return NULLC;
  461. }
  462. }
  463. }
  464.  
  465. void updateListC (tList Lista, tPosC pos, tListC *ListC){
  466. ListC->data[pos].partyList = Lista;
  467. }
  468.  
  469. tItemC getItemC(tPosC pos, tListC ListC){
  470. return ListC.data[pos];
  471. }
  472.  
  473. void deleteCenter(tPosC pos, tListC *ListC){
  474. while (pos != lastC(*ListC)){
  475. ListC->data[pos] = ListC->data[nextC(pos,*ListC)];
  476. pos = nextC(pos,*ListC);
  477. }
  478. ListC->lastPos--;
  479. }
  480.  
  481. bool equalListC(tListC ListC1,tListC* ListC2){
  482. tPosC pos,ultimo;
  483. pos = firstC(*ListC2);
  484. ultimo = lastC(*ListC2);
  485. while(strcmp(ListC1.data[pos].centerName,ListC2->data[pos].centerName) == 0 && pos != ultimo){
  486. pos++;
  487. }
  488. if((pos == ultimo) && (strcmp(ListC1.data[pos].centerName,ListC2->data[pos].centerName) == 0)){
  489. return true;
  490. } else{
  491. return false;
  492. }
  493. }
  494.  
  495.  
  496.  
  497. /////////////////////////////////////////////////////////////
  498.  
  499. #ifndef PARTY_LIST_H
  500. #define PARTY_LIST_H
  501.  
  502. #include "types.h"
  503.  
  504. #include <stdio.h>
  505. #include <stdlib.h>
  506. #include <string.h>
  507. #include <stdbool.h>
  508. #define LNULL NULL
  509.  
  510. typedef struct tNode *tPosL;
  511. struct tNode{
  512. tItemL data;
  513. tPosL sig;
  514. };
  515.  
  516. typedef tPosL tList;
  517.  
  518. /* Write your code here... */
  519. void createEmptyList (tList*);
  520. /*{Objetivo: Crea una lista vacía.
  521. * Salidas: Lista inicializada.
  522. * Postcondición: La lista queda inicializada y no contiene elementos.}*/
  523. bool insertItem(tItemL, tList*);
  524. /*{Objetivo: Inserta un elemento de forma ordenada en función del campo partyname. Devuelve un valor true si el
  525. * elemento fue insertado y false en case contrario
  526. * Entradas: El elemento y la lista.
  527. * Salidas: Verdadero y la lista con el elemento si se pudo insertar, falso y la lista sin el elemento si no se pudo.
  528. * Postcondición: Las posiciones de los elementos de la lista posteriores al insertado pueden cambiar de valor.}*/
  529. bool copyList(tList, tList*);
  530. /*{Objetivo: Copia una lista en otra
  531. * Entradas: La lista en la que se quiere copiar y la lista a copiar.
  532. * Salidas: Verdadero y la lista copiada si se pudo copiar, falso y la lista sin copiar si no se pudo copiar.
  533. * Precondición: La lista que se quiere copiar existe.}*/
  534. void updateVotes(tNumVotes, tPosL, tList*);
  535. /*{Objetivo: Modifica el número de votos del elemento situado en la posición indicada.
  536. * Entradas: Cantidad de votos, posición en la que actualizarlos y la lista.
  537. * Salidas: La lista con los votos de la posición indicada actualizados.
  538. * Precondición: La posición indicada es una posición válida en la lista.
  539. * Postcondición: El orden de los elementos de la lista no se ve modificado. }*/
  540. void deleteAtPosition(tPosL, tList*);
  541. /*{Objetivo: Elimina de la lista el elemento que ocupa la posición indicada.
  542. * Entradas: Posición del elemento a eliminar y la lista.
  543. * Salidas: La lista sin el elemento que se quería eliminar.
  544. * Precondición: La posición indicada es una posición válida en la lista.
  545. * Postcondición: Tanto la posición del elemento eliminado como aquellas de los elementos de la lista a continuación
  546. * del mismo pueden cambiar de valor.}*/
  547. void deleteList(tList*);
  548. /*{Objetivo: Elimina una lista.
  549. * Entradas: Lista a eliminar.
  550. * Precondición: La lista indicada existe.}*/
  551. tPosL findItem(tPartyName, tList);
  552. /*{Objetivo:Devuelve la posición del primer elemento de la lista cuyo nombre de partido se corresponda con el indicado
  553. * (o LNULL si no existe tal elemento).
  554. * Entradas: Nombre del partido a buscar y la lista.
  555. * Salidas: Posición del elemento que se busca (LNULL si no existe).}*/
  556. bool isEmptyList(tList);
  557. /*{Objetivo: Determina si la lista está vacía.
  558. * Entradas: La lista.
  559. * Salidas: Verdadero si la lista está vacía, falso si no lo está.
  560. * Precondición: La lista existe.}*/
  561. tItemL getItem(tPosL, tList);
  562. /*{Objetivo: Devuelve el contenido del elemento de la lista que ocupa la posición indicada.
  563. * Entradas: La posición del elemento a buscar y la lista
  564. * Salidas: El contenido del elemento buscado
  565. * Precondición: La posición indicada es una posición válida en la lista.}*/
  566. tPosL first(tList);
  567. /*{Objetivo: Devuelve la posición del primer elemento de la lista.
  568. * Entradas: La lista.
  569. * Salidas: La posición del primer elemento de la lista.
  570. * Precondición: La lista no está vacía.}*/
  571. tPosL last(tList);
  572. /*{Objetivo: Devuelve la posición del último elemento de la lista.
  573. * Entradas: La lista.
  574. * Salidas: La posición del último elemento de la lista.
  575. * Precondición: La lista no está vacía.}*/
  576. tPosL previous(tPosL, tList);
  577. /*{Objetivo: Devuelve la posición en la lista del elemento anterior al de la posición indicada (o LNULL si la posición
  578. * no tiene anterior).
  579. * Entradas: La posición del elemento posterior al buscado y la lista.
  580. * Salidas: La posición del elemento anterior al de la posición indicada o LNULL si no existe una posición anterior.
  581. * Precondición: La posición indicada es una posición válida en la lista.}*/
  582. tPosL next(tPosL, tList);
  583. /*{Objetivo: Devuelve la posición en la lista del elemento siguiente al de la posición indicada (o LNULL si la posición
  584. * no tiene siguiente).
  585. * Entradas: La posición del elemento anterior al buscado y la lista.
  586. * Salidas: La posición del elemento posterior al de la posición indicada o LNULL si no existe una posición posterior.
  587. * Precondición: La posición indicada es una posición válida en la lista.}*/
  588. tPosL findPosition(tList,tItemL);
  589. #endif
  590.  
  591.  
  592. ///////////////////////////////////////////////////////
  593.  
  594. #include "party_list.h"
  595.  
  596. /* Write your code here... */
  597.  
  598. void createEmptyList (tList *List){
  599. *List = LNULL;
  600. }
  601.  
  602. bool isEmptyList(tList List){
  603. if (List == LNULL){
  604. return true;
  605. }
  606. }
  607.  
  608. tPosL first(tList List){
  609. return List;
  610. }
  611.  
  612. tPosL next(tPosL pos,tList List){
  613. return pos->sig;
  614. }
  615.  
  616. tPosL previous(tPosL pos, tList List){
  617. tPosL pos1;
  618. if (pos == List){
  619. return LNULL;
  620. } else {
  621. pos1 = List;
  622. while(pos1->sig != pos){
  623. pos1 = next(pos1,List);
  624. } return pos1;
  625. }
  626. }
  627.  
  628. tPosL last(tList List){
  629. tPosL pos;
  630. pos = List;
  631. while (pos->sig != LNULL){
  632. pos = next(pos,List);
  633. } return pos;
  634. }
  635.  
  636. tItemL getItem(tPosL pos, tList List){
  637. return pos->data;
  638. }
  639.  
  640. void updateVotes(tNumVotes nVote, tPosL pos, tList *List){
  641. pos->data.numVotes = nVote;
  642. }
  643.  
  644. tPosL findItem(tPartyName NameParty, tList List) {
  645. tPosL pos;
  646. pos = List;
  647. while ((pos != LNULL) && (strcmp(pos->data.partyName,NameParty) != 0)){
  648. pos = next(pos,List);
  649. }
  650. if(pos != LNULL && strcmp(pos->data.partyName,NameParty) == 0){
  651. return pos;
  652. } else{
  653. return LNULL;
  654. }
  655. }
  656.  
  657. void deleteList(tList *List){
  658. tPosL pos;
  659. while (*List != LNULL){
  660. pos = *List;
  661. *List = (*List)->sig;
  662. free(pos);
  663. }
  664. }
  665. bool createNode(tPosL *pos){
  666. *pos = malloc(sizeof(**pos));
  667. return *pos != LNULL;
  668. }
  669.  
  670. bool insertItem(tItemL Item, tList *List){
  671. tPosL pos,pos1;
  672. if(!createNode(&pos)){
  673. return false;
  674. } else{
  675. pos->data = Item;
  676. pos->sig = LNULL;
  677. if(*List == LNULL){
  678. *List = pos;
  679. } else if(strcmp(Item.partyName, (*List)->data.partyName) < 0){
  680. pos->sig = *List;
  681. *List = pos;
  682. } else{
  683. pos1 = findPosition(*List,Item);
  684. pos->sig = pos1->sig;
  685. pos1->sig = pos;
  686. }
  687. return true;
  688. }
  689. }
  690.  
  691. void deleteAtPosition(tPosL pos, tList *List) {
  692. tPosL pos1;
  693. if(pos == *List){ //borrar elemento primera posición
  694. *List = (*List)->sig;
  695. } else if(pos->sig == LNULL){ //borrar elemento última posición
  696. for(pos1 = *List; pos1->sig != pos; pos1 = pos1->sig){
  697. }
  698. pos1->sig = LNULL;
  699. } else{ //borrar elemento posición intermedia
  700. pos1 = pos->sig;
  701. pos->data = pos1->data;
  702. pos->sig = pos1->sig;
  703. pos = pos1;
  704. }
  705. free(pos);
  706. }
  707.  
  708. bool copyList(tList List, tList *List1){
  709. tPosL pos,pos1,pos2;
  710. bool temp;
  711. temp = true;
  712. createEmptyList(List1);
  713. if(!isEmptyList((List))){
  714. pos = List;
  715. while ((pos != LNULL) && (createNode(&pos2))){
  716. pos2->data = pos->data;
  717. pos2->sig = LNULL;
  718. if(pos == List){
  719. *List1 = pos2;
  720. pos1 = pos2;
  721. } else{
  722. pos1->sig = pos2;
  723. pos1 = pos2;
  724. }
  725. pos = pos->sig;
  726. }
  727. if(pos != LNULL){
  728. temp = false;
  729. }
  730. }
  731. return temp;
  732. }
  733.  
  734. tPosL findPosition(tList List, tItemL Item){
  735. tPosL pos;
  736. pos = List;
  737. while((pos->sig != LNULL) && (strcmp(pos->sig->data.partyName, Item.partyName) < 0)){
  738. pos = pos->sig;
  739. } return pos;
Advertisement
Add Comment
Please, Sign In to add comment