Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. //#include "TADabas.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. typedef struct no_ NO;
  7. typedef struct lista_ LISTA;
  8. void lista_imprimir(LISTA *lista);
  9.  
  10. struct no_{
  11.     char nome[40];
  12.     char URL[512];
  13.     NO* prox;
  14. };
  15.  
  16. struct lista_{
  17.     NO* inicio;
  18.     NO* fim;
  19.     int size;
  20. };
  21.  
  22. LISTA *lista_criar(void){
  23.     LISTA *lista = (LISTA*)malloc(sizeof(LISTA));
  24.     lista->inicio = NULL;
  25.     lista->fim == NULL;
  26.     lista->size = 0;
  27.     return lista;
  28. }
  29.  
  30. void lista_inserir_fim(LISTA *lista, char *nome, char *URL){
  31.     NO *p = (NO*)malloc(sizeof(NO));
  32.     strcpy(p->nome, nome);
  33.     strcpy(p->URL, URL);
  34.     if (lista != NULL){
  35.         if (lista->inicio == NULL){ //se for o primeiro elemento a ser inserido
  36.             p->prox = NULL;
  37.             lista->inicio = p;
  38.         }
  39.         else {
  40.             lista->fim->prox = p;
  41.             p->prox = NULL;
  42.         }
  43.         lista->fim = p;
  44.         lista->size++;
  45.         return;
  46.     }
  47.     return;
  48. }
  49.  
  50. void lista_inserir_ordenado(LISTA *lista, NO* no, int i){
  51.     NO *n = lista->inicio;
  52.     int flag = 0;
  53.     int j = 0;
  54.     if (lista->inicio == NULL){
  55.         no->prox = NULL;
  56.         lista->inicio = no;
  57.         printf ("%s\n", lista->inicio->nome);
  58.         return;
  59.     }
  60.  
  61.     if (i == 1){
  62.         lista->inicio = no;
  63.         no->prox = n;
  64.         flag = 1;
  65.         printf ("%s\n", lista->inicio->nome);
  66.         lista->size++;
  67.         return;
  68.     }
  69.  
  70.     if (flag == 0){
  71.         for (j = 1; j < (lista->size); j++){
  72.             if (j == i-1){
  73.                 NO* p = n->prox;
  74.                 n->prox = no;
  75.                 no->prox = p;
  76.                 flag = 1;
  77.                 lista->size++;
  78.                 break;
  79.             }
  80.             n = n->prox;
  81.         }
  82.     }
  83.    
  84.     if (flag == 0){
  85.         lista->fim->prox = no;
  86.         no->prox = NULL;
  87.         lista->fim = no;
  88.         lista->size++;
  89.     }
  90.  
  91.     return;
  92. }
  93.  
  94. void lista_apagar(LISTA **ptr);
  95.  
  96. void lista_remover(LISTA *lista, char *nome){
  97.     NO *p, *q;
  98.     p = lista->inicio;
  99.     q = NULL;
  100.  
  101.     if (!strcmp(nome, p->nome)){
  102.         lista->inicio = p->prox;
  103.         lista->size--;
  104.         free(p);
  105.         return;
  106.     }
  107.  
  108.     while (p != NULL && strcmp(nome, p->nome)){
  109.         q = p;
  110.         p = p->prox;
  111.     }
  112.  
  113.     if (p==NULL){
  114.         return;
  115.     }
  116.  
  117.     q->prox = p->prox;
  118.     lista->size--;
  119.     free (p);
  120.     return;
  121. }
  122.  
  123. void encontrar_no(LISTA* lista, char *nome, NO* no){
  124.     NO *p, *q;
  125.     p = lista->inicio;
  126.     q = NULL;
  127.  
  128.     if (!strcmp(nome, p->nome)){
  129.         strcpy(no->nome, p->nome);
  130.         strcpy(no->URL, p->URL);
  131.         return;
  132.     }
  133.  
  134.     int count = 0;
  135.     while (p->prox != NULL && strcmp(nome, p->nome)){
  136.         q = p;
  137.         p = p->prox;
  138.         count++;
  139.     }
  140.  
  141.     if (p==NULL){
  142.         return;
  143.     }
  144.  
  145.     strcpy(no->nome, p->nome);
  146.     strcpy(no->URL, p->URL);
  147.     free (p);
  148.     return;
  149. }
  150.  
  151.  
  152. void trocar_posicao(LISTA *lista, char *nome, int i){
  153.     NO *aux = lista->inicio;
  154.     NO *p = (NO*)malloc(sizeof(NO));
  155.     encontrar_no(lista, nome, p);
  156.     printf ("\nNO encontrado: %s\n", p->nome);
  157.     lista_remover(lista, nome);
  158.     printf("\nLISTA (NO removido):\n");
  159.     lista_imprimir(lista);
  160.     printf("\n");
  161.     lista_inserir_ordenado(lista, p, i);
  162.     printf("LISTA (NO inserido):\n");
  163.     lista_imprimir(lista);
  164.     printf("\n");
  165.     return;
  166. }
  167.  
  168. void lista_imprimir(LISTA *lista){
  169.     NO* p = lista->inicio;
  170.     for (int j = 0; p!=NULL; j++){
  171.         printf("%s %s\n", p->nome, p->URL);
  172.         p = p->prox;
  173.     }
  174. }
  175.  
  176. int main(){
  177.     LISTA* l = lista_criar();
  178.     char nome1[40], URL[512], nome2[40];
  179.     int N;
  180.     printf("MENU:\nIncluir nova aba (1)\nAlterar posição da aba (2)\nImprimir (3)\nSair (4)\n");
  181.     scanf("%d", &N);
  182.     while (N!=4){
  183.         if (N == 1){
  184.             scanf("%s", nome1);
  185.             scanf("%s", URL);
  186.             lista_inserir_fim(l, nome1, URL);
  187.         }
  188.  
  189.         if (N == 2){
  190.             int i;
  191.             scanf("%s", nome2);
  192.             scanf("%d", &i);
  193.             trocar_posicao(l, nome2, i);
  194.         }
  195.  
  196.         if (N == 3){
  197.             lista_imprimir(l);
  198.         }
  199.         scanf("%d", &N);
  200.     }
  201.  
  202.     return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement