Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define TRUE 1
  5. #define FALSE 0
  6.  
  7. typedef struct node{
  8. int item;
  9. struct node *prox;
  10. struct node *ant;
  11.  
  12. } NO;
  13.  
  14. typedef struct lista{
  15. NO *ini;
  16. NO *fim;
  17. int tam;
  18.  
  19. } LISTA;
  20.  
  21. LISTA *criaLista(){
  22. LISTA *lista = NULL;
  23.  
  24. lista = (LISTA *) malloc(sizeof(LISTA));
  25. if(lista == NULL) printf("ERRO DE CRIACAO DE LISTA\n\tREINICIE O PROGRAMA\n\n");
  26. else{
  27. lista->ini = NULL;
  28. lista->fim = NULL;
  29. lista->tam = 0;
  30. }
  31.  
  32. return lista;
  33. }
  34.  
  35. void insere_no_inicio(LISTA *l){
  36. NO *p = NULL;
  37. int item;
  38.  
  39. //recebendo item
  40. scanf("%d", &item);
  41. fgetc(stdin);
  42.  
  43. p = (NO *) malloc(sizeof(NO));
  44. if(p != NULL){
  45. p->item = item;
  46. p->ant = NULL;
  47. p->prox = l->ini;
  48.  
  49. if(l->ini == NULL){
  50. l->fim = p;
  51. }
  52. else{
  53. l->ini->ant = p;
  54. }
  55.  
  56. l->ini = p;
  57. l->tam++;
  58. }
  59. }
  60.  
  61. void insere_no_fim(LISTA *l){
  62. NO *p = NULL;
  63. int item;
  64.  
  65. //recebendo item
  66. scanf("%d", &item);
  67. fgetc(stdin);
  68.  
  69. p = (NO *) malloc(sizeof(NO));
  70. if(p != NULL){
  71. p->item = item;
  72. p->prox = NULL;
  73. p->ant = l->fim;
  74.  
  75. if(l->ini == NULL){
  76. l->ini = p;
  77. }
  78. else{
  79. l->fim->prox = p;
  80. }
  81.  
  82. l->fim = p;
  83. l->tam++;
  84. }
  85. }
  86.  
  87. void remove_do_fim(LISTA *l){
  88. NO *p = NULL;
  89.  
  90. p = l->fim;
  91. if(p != NULL){
  92. l->fim = p->ant;
  93. if(p->ant != NULL) p->ant->prox = NULL;
  94. else l->ini = NULL;
  95. p->ant = NULL;
  96.  
  97. printf("%d\n", p->item);
  98.  
  99. free(p);
  100. l->tam--;
  101. }
  102. }
  103.  
  104. void remove_do_inicio(LISTA *l){
  105. NO *p = NULL;
  106.  
  107. p = l->ini;
  108. if(p != NULL){
  109. l->ini = p->prox;
  110. if(p->prox != NULL) p->prox->ant = NULL;
  111. else l->fim = NULL;
  112. p->prox = NULL;
  113.  
  114. printf("%d\n", p->item);
  115.  
  116. free(p);
  117. l->tam--;
  118. }
  119. }
  120.  
  121. int remove_valor(LISTA *l, int valor){
  122. NO *p = NULL;
  123. int ocorrencias;
  124.  
  125. ocorrencias = 0;
  126. p = l->ini;
  127. while(p != NULL){
  128. if(p->item == valor){
  129. if(p != l->ini) p->ant->prox = p->prox;
  130. else l->ini = p->prox;
  131.  
  132. if(p != l->fim) p->prox->ant = p->ant;
  133. else l->fim = p->ant;
  134.  
  135. l->tam--;
  136. free(p);
  137. ocorrencias++;
  138. }
  139. p = p->prox;
  140. }
  141.  
  142. return ocorrencias;
  143. }
  144.  
  145. void remove_por_valor(LISTA *l){
  146. int valor;
  147. int ocorrencias;
  148.  
  149. scanf("%d", &valor);
  150. fgetc(stdin);
  151.  
  152. ocorrencias = remove_valor(l, valor);
  153. printf("%d\n", ocorrencias);
  154. }
  155.  
  156. int remove_por_posicao(LISTA *l){
  157. NO *p = NULL;
  158. int valor = 0;
  159. int pos;
  160. int i;
  161.  
  162. scanf("%d", &pos);
  163. fgetc(stdin);
  164.  
  165. i = 1;
  166. p = l->ini;
  167. while(p != NULL){
  168. if(i == pos) break;
  169. i++;
  170. p = p->prox;
  171. }
  172.  
  173. if(p == NULL){
  174. return FALSE;
  175. }
  176.  
  177. valor = p->item;
  178.  
  179. if(p != l->ini) p->ant->prox = p->prox;
  180. else l->ini = p->prox;
  181.  
  182. if(p != l->fim) p->prox->ant = p->ant;
  183. else l->fim = p->ant;
  184. free(p);
  185. l->tam--;
  186. printf("%d\n", valor);
  187. return valor;
  188. }
  189.  
  190. void substitui_por_valor(LISTA *l){
  191. NO *p = NULL;
  192. int valor;
  193. int item;
  194.  
  195. scanf("%d %d", &item, &valor);
  196. fgetc(stdin);
  197.  
  198. p = l->ini;
  199. while(p != NULL){
  200. if(p->item == item) break;
  201. p = p->prox;
  202. }
  203.  
  204. if(p != NULL) p->item = valor;
  205. }
  206.  
  207. int ocorrencias_por_valor(LISTA *l){
  208. NO *p = NULL;
  209. int valor;
  210. int ocorrencias;
  211.  
  212. scanf("%d", &valor);
  213. fgetc(stdin);
  214.  
  215. ocorrencias = 0;
  216. p = l->ini;
  217. while(p != NULL){
  218. if(p->item == valor) ocorrencias++;
  219. p = p->prox;
  220. }
  221.  
  222. printf("%d\n", ocorrencias);
  223. return ocorrencias;
  224. }
  225.  
  226. void printL(LISTA *l){
  227. NO *p = NULL;
  228.  
  229. printf("\n");
  230.  
  231. p = l->ini;
  232. while(p != NULL){
  233. printf("%d\n", p->item);
  234. p = p->prox;
  235. }
  236. }
  237.  
  238. void apagarLista(LISTA *l){
  239. NO *p = NULL;
  240. NO *aux = NULL;
  241.  
  242. p = l->ini;
  243. while(p != NULL){
  244. aux = p;
  245. p = p->prox;
  246. free(aux);
  247. }
  248. free(l);
  249. }
  250.  
  251. int main (int argc, char *argv[]){
  252. LISTA *l = NULL;
  253. char opt;
  254.  
  255. l = criaLista();
  256. while(opt != 'X'){
  257. opt = getc(stdin);
  258. fgetc(stdin);
  259.  
  260. if(opt == 'I'){
  261. insere_no_inicio(l);
  262. continue;
  263. }
  264.  
  265. if(opt == 'F'){
  266. insere_no_fim(l);
  267. continue;
  268. }
  269.  
  270. if(opt == 'P'){
  271. remove_do_fim(l);
  272. continue;
  273. }
  274.  
  275. if(opt == 'D'){
  276. remove_do_inicio(l);
  277. continue;
  278. }
  279.  
  280. if(opt == 'V'){
  281. remove_por_valor(l);
  282. continue;
  283. }
  284.  
  285. if(opt == 'E'){
  286. remove_por_posicao(l);
  287. continue;
  288. }
  289.  
  290. if(opt == 'T'){
  291. substitui_por_valor(l);
  292. continue;
  293. }
  294.  
  295. if(opt == 'C'){
  296. ocorrencias_por_valor(l);
  297. continue;
  298. }
  299. }
  300.  
  301. printL(l);
  302. apagarLista(l);
  303. return 0;
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement