Advertisement
phelipemendes

Untitled

Apr 21st, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct no
  6. {
  7. char dispositivos[30];
  8. char estado [30];
  9. struct no *prox;
  10.  
  11.  
  12. };
  13. typedef struct no no;
  14.  
  15.  
  16. int tam;
  17.  
  18. void inicia(no *LISTA);
  19. int menu(void);
  20. void opcao(no *LISTA, int op);
  21. no *criaNo();
  22. void insereFim(no *LISTA);
  23. void insereInicio(no *LISTA);
  24. void exibe(no *LISTA);
  25. void libera(no *LISTA);
  26. void insere (no *LISTA);
  27. no *retiraInicio(no *LISTA);
  28. no *retiraFim(no *LISTA);
  29. no *retira(no *LISTA);
  30. void lista_ligado(no *LISTA);
  31. void muda_estado(no *LISTA);
  32.  
  33.  
  34.  
  35.  
  36. int main()
  37. {
  38. no *LISTA = (no*) malloc(sizeof(no));
  39. if(!LISTA)
  40. {
  41. printf("Sem memoria disponivel!\n");
  42. exit(1);
  43. }
  44. else
  45. {
  46. inicia(LISTA);
  47. int opt;
  48.  
  49. do
  50. {
  51. opt = menu();
  52. opcao(LISTA, opt);
  53. }
  54. while(opt);
  55.  
  56. free(LISTA);
  57.  
  58. }
  59. return 0;
  60. }
  61.  
  62. void inicia(no *LISTA)
  63. {
  64. LISTA -> prox = NULL;
  65. tam = 0;
  66. }
  67.  
  68. int menu(void)
  69. {
  70. int opt;
  71.  
  72. printf("0. Sair\n");
  73. printf("1. Zerar lista\n");
  74. printf("2. Exibir lista\n");
  75. printf("3. Adicionar no inicio\n");
  76. printf("4. Adicionar no final\n");
  77. printf("5. Escolher onde inserir\n");
  78. printf("6. Retirar do inicio\n");
  79. printf("7. Retirar do fim\n");
  80. printf("8. Escolher de onde tirar\n");
  81. printf("9. Listar Objetos Ligados\n");
  82. printf("10. Muda estado");
  83.  
  84. scanf("%d", &opt);
  85.  
  86. return opt;
  87. }
  88.  
  89. void opcao(no*LISTA, int op)
  90. {
  91. no *tmp;
  92. switch(op)
  93. {
  94. case 0:
  95. libera(LISTA);
  96. break;
  97.  
  98. case 1:
  99. libera(LISTA);
  100. inicia(LISTA);
  101. break;
  102.  
  103. case 2:
  104. exibe(LISTA);
  105. break;
  106.  
  107. case 3:
  108. insereInicio(LISTA);
  109. break;
  110.  
  111. case 4:
  112. insereFim(LISTA);
  113. break;
  114.  
  115. case 5:
  116. insere(LISTA);
  117. break;
  118.  
  119. case 6:
  120. tmp = retiraInicio(LISTA);
  121. printf("Retirado: %s\n\n", tmp->dispositivos);
  122. break;
  123.  
  124. case 7:
  125. tmp = retiraFim(LISTA);
  126. printf("Retirado: %s\n\n", tmp->dispositivos);
  127. break;
  128.  
  129. case 8:
  130. tmp = retira(LISTA);
  131. printf("Retirado: %s\n\n", tmp->dispositivos);
  132. break;
  133. case 9:
  134. lista_ligado(LISTA);
  135. break;
  136. case 10:
  137. muda_estado(LISTA);
  138. break;
  139. default:
  140. printf("Comando invalido\n\n");
  141. }
  142. }
  143.  
  144. int vazia(no *LISTA)
  145. {
  146. if(LISTA->prox == NULL)
  147. return 1;
  148. else
  149. return 0;
  150. }
  151.  
  152. no *aloca()
  153. {
  154. no *novo = (no *) malloc(sizeof(no));
  155. if(!novo)
  156. {
  157. printf("Sem memoria disponivel!\n");
  158. exit(1);
  159. }
  160. else
  161. {
  162. printf("Novo elemento: \n");
  163. scanf("%s", &novo->dispositivos);
  164. printf("Estado LIGADO/DESLIGADO\n");
  165. scanf("%s", &novo->estado);
  166.  
  167. return novo;
  168. }
  169. }
  170.  
  171.  
  172. void insereFim(no *LISTA)
  173. {
  174. no *novo = aloca();
  175. novo->prox = NULL;
  176.  
  177. if(vazia(LISTA))
  178. LISTA->prox = novo;
  179. else
  180. {
  181. no*tmp = LISTA->prox;
  182.  
  183. while(tmp->prox != NULL)
  184. tmp = tmp->prox;
  185.  
  186. tmp->prox = novo;
  187. }
  188. tam++;
  189. }
  190.  
  191. void insereInicio(no *LISTA)
  192. {
  193. no *novo = aloca();
  194. no *oldHead = LISTA->prox;
  195.  
  196. LISTA->prox = novo;
  197. novo->prox = oldHead;
  198.  
  199. tam++;
  200. }
  201.  
  202. void exibe(no *LISTA)
  203. {
  204.  
  205. if(vazia(LISTA))
  206. {
  207. printf("Lista vazia!\n\n");
  208. return ;
  209. }
  210.  
  211. no *tmp;
  212. tmp = LISTA->prox;
  213. while( tmp != NULL)
  214. {
  215. printf("%s", tmp->dispositivos);
  216. printf("%s", tmp->estado);
  217. tmp = tmp->prox;
  218. }
  219.  
  220. printf("\n\n");
  221. }
  222.  
  223. void libera(no *LISTA)
  224. {
  225. if(!vazia(LISTA))
  226. {
  227. no *proxNo,
  228. *atual;
  229.  
  230. atual = LISTA->prox;
  231. while(atual != NULL)
  232. {
  233. proxNo = atual->prox;
  234. free(atual);
  235. atual = proxNo;
  236. }
  237. }
  238. }
  239.  
  240. void insere(no *LISTA)
  241. {
  242. int pos,
  243. count;
  244. printf("Em que posicao, [de 1 ate %d] voce deseja inserir: ", tam);
  245. scanf("%d", &pos);
  246.  
  247. if(pos > 0 && pos <= tam)
  248. {
  249. if(pos == 1)
  250. insereInicio(LISTA);
  251. else
  252. {
  253. no *atual = LISTA->prox,
  254. *anterior = LISTA;
  255. no *novo = aloca();
  256.  
  257. for(count = 1 ; count < pos ; count++)
  258. {
  259. anterior = atual;
  260. atual = atual->prox;
  261. }
  262. anterior->prox = novo;
  263. novo->prox = atual;
  264. tam++;
  265. }
  266.  
  267. }
  268. else
  269. printf("Elemento invalido\n\n");
  270. }
  271.  
  272. no *retiraInicio(no *LISTA)
  273. {
  274. if(LISTA->prox == NULL)
  275. {
  276. printf("Lista ja esta vazia\n");
  277. return NULL;
  278. }
  279. else
  280. {
  281. no *tmp = LISTA->prox;
  282. LISTA->prox = tmp->prox;
  283. tam--;
  284. return tmp;
  285. }
  286.  
  287. }
  288.  
  289. no *retiraFim(no *LISTA)
  290. {
  291. if(LISTA->prox == NULL)
  292. {
  293. printf("Lista ja vazia\n\n");
  294. return NULL;
  295. }
  296. else
  297. {
  298. no *ultimo = LISTA->prox,
  299. *penultimo = LISTA;
  300.  
  301. while(ultimo->prox != NULL)
  302. {
  303. penultimo = ultimo;
  304. ultimo = ultimo->prox;
  305. }
  306.  
  307. penultimo->prox = NULL;
  308. tam--;
  309. return ultimo;
  310. }
  311. }
  312.  
  313. no *retira(no *LISTA)
  314. {
  315. int opt,
  316. count;
  317. printf("Que posicao, [de 1 ate %d] voce deseja retirar: ", tam);
  318. scanf("%d", &opt);
  319.  
  320. if(opt > 0 && opt <= tam)
  321. {
  322. if(opt == 1)
  323. return retiraInicio(LISTA);
  324. else
  325. {
  326. no *atual = LISTA->prox,
  327. *anterior = LISTA;
  328.  
  329. for(count = 1 ; count < opt ; count++)
  330. {
  331. anterior = atual;
  332. atual = atual->prox;
  333. }
  334.  
  335. anterior->prox = atual->prox;
  336. tam--;
  337. return atual;
  338. }
  339.  
  340. }
  341. else
  342. {
  343. printf("Elemento invalido\n\n");
  344. return NULL;
  345. }
  346.  
  347. }
  348.  
  349. void lista_ligado(no *LISTA)
  350. {
  351.  
  352.  
  353. if(vazia(LISTA))
  354. {
  355. printf("Lista vazia!\n\n");
  356. return ;
  357. }
  358.  
  359. no *tmp;
  360. tmp = LISTA->prox;
  361. printf("Lista:\n\n");
  362. while( tmp != NULL)
  363. {
  364. if(strcmp("ligado", tmp->estado) == 0)
  365. {
  366.  
  367.  
  368. printf("\n Dispositivo %s\n", tmp->dispositivos);
  369. printf("\n Estado %s\n", tmp->estado);
  370. tmp = tmp->prox;
  371.  
  372. }
  373. else
  374. {
  375. tmp = tmp->prox;
  376.  
  377. }
  378.  
  379. }
  380.  
  381. printf("\n\n");
  382. }
  383.  
  384.  
  385. void muda_estado(no *LISTA)
  386.  
  387. {
  388. if(vazia(LISTA))
  389. {
  390. printf("Lista vazia!\n\n");
  391. return ;
  392. }
  393. char op[30];
  394. printf("Digite o dispotivo para mudar estado");
  395. scanf("%s",&op);
  396. no *tmp;
  397. tmp = LISTA->prox;
  398. while( tmp != NULL)
  399. {
  400. if(strcmp(op, tmp->dispositivos) == 0)
  401. {
  402.  
  403. tmp->estado = "desligado";
  404. tmp = tmp->prox;
  405.  
  406. }
  407. else
  408. {
  409. tmp = tmp->prox;
  410.  
  411. }
  412.  
  413. }
  414.  
  415. printf("\n\n");
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement